From a48273415a63f407a0aed8582a63a3a738b0af6a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 10 Aug 2024 21:58:41 -0700 Subject: [PATCH] Embed Generics iterator implementations into method bodies --- src/generics.rs | 216 ++++++++++++++++++++++++------------------------ 1 file changed, 108 insertions(+), 108 deletions(-) diff --git a/src/generics.rs b/src/generics.rs index 6e700ea96..f0ebd7fe2 100644 --- a/src/generics.rs +++ b/src/generics.rs @@ -105,31 +105,139 @@ impl Default for Generics { impl Generics { /// Iterator over the lifetime parameters in `self.params`. pub fn lifetimes(&self) -> impl Iterator { + struct Lifetimes<'a>(Iter<'a, GenericParam>); + + impl<'a> Iterator for Lifetimes<'a> { + type Item = &'a LifetimeParam; + + fn next(&mut self) -> Option { + let next = match self.0.next() { + Some(item) => item, + None => return None, + }; + if let GenericParam::Lifetime(lifetime) = next { + Some(lifetime) + } else { + self.next() + } + } + } + Lifetimes(self.params.iter()) } /// Iterator over the lifetime parameters in `self.params`. pub fn lifetimes_mut(&mut self) -> impl Iterator { + struct LifetimesMut<'a>(IterMut<'a, GenericParam>); + + impl<'a> Iterator for LifetimesMut<'a> { + type Item = &'a mut LifetimeParam; + + fn next(&mut self) -> Option { + let next = match self.0.next() { + Some(item) => item, + None => return None, + }; + if let GenericParam::Lifetime(lifetime) = next { + Some(lifetime) + } else { + self.next() + } + } + } + LifetimesMut(self.params.iter_mut()) } /// Iterator over the type parameters in `self.params`. pub fn type_params(&self) -> impl Iterator { + struct TypeParams<'a>(Iter<'a, GenericParam>); + + impl<'a> Iterator for TypeParams<'a> { + type Item = &'a TypeParam; + + fn next(&mut self) -> Option { + let next = match self.0.next() { + Some(item) => item, + None => return None, + }; + if let GenericParam::Type(type_param) = next { + Some(type_param) + } else { + self.next() + } + } + } + TypeParams(self.params.iter()) } /// Iterator over the type parameters in `self.params`. pub fn type_params_mut(&mut self) -> impl Iterator { + struct TypeParamsMut<'a>(IterMut<'a, GenericParam>); + + impl<'a> Iterator for TypeParamsMut<'a> { + type Item = &'a mut TypeParam; + + fn next(&mut self) -> Option { + let next = match self.0.next() { + Some(item) => item, + None => return None, + }; + if let GenericParam::Type(type_param) = next { + Some(type_param) + } else { + self.next() + } + } + } + TypeParamsMut(self.params.iter_mut()) } /// Iterator over the constant parameters in `self.params`. pub fn const_params(&self) -> impl Iterator { + struct ConstParams<'a>(Iter<'a, GenericParam>); + + impl<'a> Iterator for ConstParams<'a> { + type Item = &'a ConstParam; + + fn next(&mut self) -> Option { + let next = match self.0.next() { + Some(item) => item, + None => return None, + }; + if let GenericParam::Const(const_param) = next { + Some(const_param) + } else { + self.next() + } + } + } + ConstParams(self.params.iter()) } /// Iterator over the constant parameters in `self.params`. pub fn const_params_mut(&mut self) -> impl Iterator { + struct ConstParamsMut<'a>(IterMut<'a, GenericParam>); + + impl<'a> Iterator for ConstParamsMut<'a> { + type Item = &'a mut ConstParam; + + fn next(&mut self) -> Option { + let next = match self.0.next() { + Some(item) => item, + None => return None, + }; + if let GenericParam::Const(const_param) = next { + Some(const_param) + } else { + self.next() + } + } + } + ConstParamsMut(self.params.iter_mut()) } @@ -142,114 +250,6 @@ impl Generics { } } -pub struct Lifetimes<'a>(Iter<'a, GenericParam>); - -impl<'a> Iterator for Lifetimes<'a> { - type Item = &'a LifetimeParam; - - fn next(&mut self) -> Option { - let next = match self.0.next() { - Some(item) => item, - None => return None, - }; - if let GenericParam::Lifetime(lifetime) = next { - Some(lifetime) - } else { - self.next() - } - } -} - -pub struct LifetimesMut<'a>(IterMut<'a, GenericParam>); - -impl<'a> Iterator for LifetimesMut<'a> { - type Item = &'a mut LifetimeParam; - - fn next(&mut self) -> Option { - let next = match self.0.next() { - Some(item) => item, - None => return None, - }; - if let GenericParam::Lifetime(lifetime) = next { - Some(lifetime) - } else { - self.next() - } - } -} - -pub struct TypeParams<'a>(Iter<'a, GenericParam>); - -impl<'a> Iterator for TypeParams<'a> { - type Item = &'a TypeParam; - - fn next(&mut self) -> Option { - let next = match self.0.next() { - Some(item) => item, - None => return None, - }; - if let GenericParam::Type(type_param) = next { - Some(type_param) - } else { - self.next() - } - } -} - -pub struct TypeParamsMut<'a>(IterMut<'a, GenericParam>); - -impl<'a> Iterator for TypeParamsMut<'a> { - type Item = &'a mut TypeParam; - - fn next(&mut self) -> Option { - let next = match self.0.next() { - Some(item) => item, - None => return None, - }; - if let GenericParam::Type(type_param) = next { - Some(type_param) - } else { - self.next() - } - } -} - -pub struct ConstParams<'a>(Iter<'a, GenericParam>); - -impl<'a> Iterator for ConstParams<'a> { - type Item = &'a ConstParam; - - fn next(&mut self) -> Option { - let next = match self.0.next() { - Some(item) => item, - None => return None, - }; - if let GenericParam::Const(const_param) = next { - Some(const_param) - } else { - self.next() - } - } -} - -pub struct ConstParamsMut<'a>(IterMut<'a, GenericParam>); - -impl<'a> Iterator for ConstParamsMut<'a> { - type Item = &'a mut ConstParam; - - fn next(&mut self) -> Option { - let next = match self.0.next() { - Some(item) => item, - None => return None, - }; - if let GenericParam::Const(const_param) = next { - Some(const_param) - } else { - self.next() - } - } -} - /// Returned by `Generics::split_for_impl`. #[cfg(feature = "printing")] #[cfg_attr(