refactor: rem impl_stream_for_wrapper and merge into impl_stream

This commit is contained in:
Shunsuke Shibayama 2023-02-06 04:11:25 +09:00
parent d4d239bd4a
commit a2a55b0645
6 changed files with 57 additions and 72 deletions

View file

@ -322,7 +322,44 @@ macro_rules! impl_displayable_stream_for_wrapper {
}
#[macro_export]
macro_rules! impl_stream_for_wrapper {
macro_rules! impl_stream {
($Strc: ident, $Inner: ident, $field: ident) => {
impl $crate::traits::Stream<$Inner> for $Strc {
#[inline]
fn payload(self) -> Vec<$Inner> {
self.$field
}
#[inline]
fn ref_payload(&self) -> &Vec<$Inner> {
&self.$field
}
#[inline]
fn ref_mut_payload(&mut self) -> &mut Vec<$Inner> {
&mut self.$field
}
}
impl std::ops::Index<usize> for $Strc {
type Output = $Inner;
fn index(&self, idx: usize) -> &Self::Output {
erg_common::traits::Stream::get(self, idx).unwrap()
}
}
impl From<$Strc> for Vec<$Inner> {
fn from(item: $Strc) -> Vec<$Inner> {
item.payload()
}
}
impl IntoIterator for $Strc {
type Item = $Inner;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.payload().into_iter()
}
}
};
($Strc: ident, $Inner: ident) => {
impl $Strc {
pub const fn new(v: Vec<$Inner>) -> $Strc {
@ -388,47 +425,6 @@ macro_rules! impl_stream_for_wrapper {
};
}
#[macro_export]
macro_rules! impl_stream {
($Strc: ident, $Inner: ident, $field: ident) => {
impl $crate::traits::Stream<$Inner> for $Strc {
#[inline]
fn payload(self) -> Vec<$Inner> {
self.$field
}
#[inline]
fn ref_payload(&self) -> &Vec<$Inner> {
&self.$field
}
#[inline]
fn ref_mut_payload(&mut self) -> &mut Vec<$Inner> {
&mut self.$field
}
}
impl std::ops::Index<usize> for $Strc {
type Output = $Inner;
fn index(&self, idx: usize) -> &Self::Output {
erg_common::traits::Stream::get(self, idx).unwrap()
}
}
impl From<$Strc> for Vec<$Inner> {
fn from(item: $Strc) -> Vec<$Inner> {
item.payload()
}
}
impl IntoIterator for $Strc {
type Item = $Inner;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.payload().into_iter()
}
}
};
}
pub trait ImmutableStream<T>: Sized {
fn ref_payload(&self) -> &[T];
fn capacity(&self) -> usize;

View file

@ -21,8 +21,7 @@ use erg_common::traits::{Locational, Stream};
use erg_common::vis::Visibility;
use erg_common::Str;
use erg_common::{
debug_power_assert, enum_unwrap, fn_name, fn_name_full, impl_stream_for_wrapper, log,
switch_unreachable,
debug_power_assert, enum_unwrap, fn_name, fn_name_full, impl_stream, log, switch_unreachable,
};
use erg_parser::ast::{DefId, DefKind};
use CommonOpcode::*;
@ -147,7 +146,7 @@ impl PyCodeGenUnit {
#[derive(Debug, Clone)]
pub struct PyCodeGenStack(Vec<PyCodeGenUnit>);
impl_stream_for_wrapper!(PyCodeGenStack, PyCodeGenUnit);
impl_stream!(PyCodeGenStack, PyCodeGenUnit);
#[derive(Debug, Default)]
pub struct PyCodeGenerator {

View file

@ -10,7 +10,7 @@ use erg_common::error::{
};
use erg_common::style::{Attribute, Color, StyledStr, StyledString, StyledStrings, Theme, THEME};
use erg_common::traits::{Locational, Stream};
use erg_common::{impl_display_and_error, impl_stream_for_wrapper, switch_lang};
use erg_common::{impl_display_and_error, impl_stream, switch_lang};
use erg_parser::error::{ParserRunnerError, ParserRunnerErrors};
@ -472,7 +472,7 @@ pub struct CompileErrors(Vec<CompileError>);
impl std::error::Error for CompileErrors {}
impl_stream_for_wrapper!(CompileErrors, CompileError);
impl_stream!(CompileErrors, CompileError);
impl From<ParserRunnerErrors> for CompileErrors {
fn from(err: ParserRunnerErrors) -> Self {

View file

@ -7,12 +7,12 @@ use erg_common::error::Location;
use erg_common::log;
use erg_common::traits::{Locational, NestedDisplay, NoTypeDisplay, Stream};
use erg_common::vis::{Field, Visibility};
use erg_common::Str;
use erg_common::{
enum_unwrap, fmt_option, fmt_vec, impl_display_for_enum, impl_display_from_nested,
impl_locational, impl_locational_for_enum, impl_nested_display_for_chunk_enum,
impl_nested_display_for_enum, impl_stream_for_wrapper,
impl_nested_display_for_enum, impl_no_type_display_for_enum, impl_stream,
};
use erg_common::{impl_no_type_display_for_enum, Str};
use erg_parser::ast::{
fmt_lines, DefId, DefKind, NonDefaultParamSignature, OperationKind, TypeSpec, VarName,
@ -1094,7 +1094,7 @@ impl NoTypeDisplay for RecordAttrs {
}
impl_display_from_nested!(RecordAttrs);
impl_stream_for_wrapper!(RecordAttrs, Def);
impl_stream!(RecordAttrs, Def);
impl Locational for RecordAttrs {
fn loc(&self) -> Location {
@ -1485,7 +1485,7 @@ impl NoTypeDisplay for Block {
}
impl_display_from_nested!(Block);
impl_stream_for_wrapper!(Block, Expr);
impl_stream!(Block, Expr);
impl Locational for Block {
fn loc(&self) -> Location {
@ -1537,7 +1537,7 @@ impl NoTypeDisplay for Dummy {
}
impl_display_from_nested!(Dummy);
impl_stream_for_wrapper!(Dummy, Expr);
impl_stream!(Dummy, Expr);
impl Locational for Dummy {
fn loc(&self) -> Location {
@ -2437,7 +2437,7 @@ impl Locational for Module {
}
}
impl_stream_for_wrapper!(Module, Expr);
impl_stream!(Module, Expr);
/// High-level Intermediate Representation
/// AST with type information added

View file

@ -12,7 +12,7 @@ use erg_common::{
fmt_option, fmt_vec, impl_display_for_enum, impl_display_for_single_struct,
impl_display_from_nested, impl_displayable_stream_for_wrapper, impl_locational,
impl_locational_for_enum, impl_nested_display_for_chunk_enum, impl_nested_display_for_enum,
impl_stream, impl_stream_for_wrapper, option_enum_unwrap,
impl_stream, option_enum_unwrap,
};
use erg_common::{fmt_vec_split_with, Str};
@ -804,6 +804,8 @@ impl IntoIterator for ClassAttrs {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RecordAttrs(Vec<Def>);
impl_stream!(RecordAttrs, Def);
impl NestedDisplay for RecordAttrs {
fn fmt_nest(&self, f: &mut fmt::Formatter<'_>, level: usize) -> fmt::Result {
fmt_lines(self.0.iter(), f, level)?;
@ -824,10 +826,6 @@ impl From<Vec<Def>> for RecordAttrs {
}
impl RecordAttrs {
pub const fn new(attrs: Vec<Def>) -> Self {
Self(attrs)
}
pub fn iter(&self) -> impl Iterator<Item = &Def> {
self.0.iter()
}
@ -837,14 +835,6 @@ impl RecordAttrs {
}
}
impl IntoIterator for RecordAttrs {
type Item = Def;
type IntoIter = <Vec<Def> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct NormalRecord {
pub l_brace: Token,
@ -1217,7 +1207,7 @@ impl Locational for Block {
}
}
impl_stream_for_wrapper!(Block, Expr);
impl_stream!(Block, Expr);
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Dummy(Vec<Expr>);
@ -1241,7 +1231,7 @@ impl Locational for Dummy {
}
}
impl_stream_for_wrapper!(Dummy, Expr);
impl_stream!(Dummy, Expr);
pub type ConstIdentifier = Identifier;

View file

@ -9,7 +9,7 @@ use erg_common::error::{
};
use erg_common::style::{Attribute, Color, StyledStr, StyledString, StyledStrings, THEME};
use erg_common::traits::Stream;
use erg_common::{fmt_iter, impl_display_and_error, impl_stream_for_wrapper, switch_lang};
use erg_common::{fmt_iter, impl_display_and_error, impl_stream, switch_lang};
#[derive(Debug)]
pub struct LexError(Box<ErrorCore>); // ErrorCore is large, so use Box
@ -37,7 +37,7 @@ impl From<LexError> for ErrorCore {
#[derive(Debug)]
pub struct LexErrors(Vec<LexError>);
impl_stream_for_wrapper!(LexErrors, LexError);
impl_stream!(LexErrors, LexError);
impl fmt::Display for LexErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -319,7 +319,7 @@ impl DesugaringError {
#[derive(Debug)]
pub struct DesugaringErrors(Vec<DesugaringError>);
impl_stream_for_wrapper!(DesugaringErrors, DesugaringError);
impl_stream!(DesugaringErrors, DesugaringError);
pub type DesugaringResult<T> = Result<T, DesugaringError>;
@ -357,7 +357,7 @@ pub struct ParserRunnerErrors(Vec<ParserRunnerError>);
impl std::error::Error for ParserRunnerErrors {}
impl_stream_for_wrapper!(ParserRunnerErrors, ParserRunnerError);
impl_stream!(ParserRunnerErrors, ParserRunnerError);
impl MultiErrorDisplay<ParserRunnerError> for ParserRunnerErrors {}