use crate::constant; use crate::fold::Fold; use crate::StrRef; pub(crate) trait Foldable { type Mapped; fn fold + ?Sized>( self, folder: &mut F, ) -> Result; } impl Foldable for Vec where X: Foldable, { type Mapped = Vec; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { self.into_iter().map(|x| x.fold(folder)).collect() } } impl Foldable for Option where X: Foldable, { type Mapped = Option; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { self.map(|x| x.fold(folder)).transpose() } } impl Foldable for Box where X: Foldable, { type Mapped = Box; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { (*self).fold(folder).map(Box::new) } } macro_rules! simple_fold { ($($t:ty),+$(,)?) => { $(impl $crate::fold_helpers::Foldable for $t { type Mapped = Self; #[inline] fn fold + ?Sized>( self, _folder: &mut F, ) -> Result { Ok(self) } })+ }; } simple_fold!(usize, String, bool, StrRef, constant::Constant, constant::ConversionFlag);