diff options
author | David Tolnay <dtolnay@gmail.com> | 2019-07-27 17:00:08 -0700 |
---|---|---|
committer | David Tolnay <dtolnay@gmail.com> | 2019-07-27 17:00:08 -0700 |
commit | d5fce2fafb190c038809eb79bbe945b037d8a18f (patch) | |
tree | 58d927e23ec2275a69a76abac935072e47990bd1 /src/runtime.rs | |
parent | eb7f69d96920b1c251cfe8c8f137586ecbcb5c6b (diff) | |
download | platform_external_rust_crates_quote-d5fce2fafb190c038809eb79bbe945b037d8a18f.tar.gz platform_external_rust_crates_quote-d5fce2fafb190c038809eb79bbe945b037d8a18f.tar.bz2 platform_external_rust_crates_quote-d5fce2fafb190c038809eb79bbe945b037d8a18f.zip |
Generalize RepSliceExt
Diffstat (limited to 'src/runtime.rs')
-rw-r--r-- | src/runtime.rs | 75 |
1 files changed, 34 insertions, 41 deletions
diff --git a/src/runtime.rs b/src/runtime.rs index 15b17fc..2178e40 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -42,6 +42,7 @@ impl BitOr<HasIterator> for HasIterator { /// whichever impl happens to be applicable. Calling that method repeatedly on /// the returned value should be idempotent. pub mod ext { + use super::RepInterp; use super::{HasIterator as HasIter, ThereIsNoIteratorInRepetition as DoesNotHaveIter}; use crate::ToTokens; use std::slice; @@ -73,62 +74,54 @@ pub mod ext { impl<T: ToTokens + ?Sized> RepToTokensExt for T {} - /// Extension trait providing the `quote_into_iter` method for types - /// convertable into slices. - /// - /// NOTE: This is implemented manually, rather than by using a blanket impl - /// over `AsRef<[T]>` to reduce the chance of ambiguity conflicts with other - /// `quote_into_iter` methods from this module. - pub trait RepSliceExt { - type Item; + /// Extension trait providing the `quote_into_iter` method for types that + /// can be referenced as an iterator. + pub trait RepAsIteratorExt<'q> { + type Iter: Iterator; - fn as_slice(&self) -> &[Self::Item]; - - fn quote_into_iter(&self) -> (slice::Iter<Self::Item>, HasIter) { - (self.as_slice().iter(), HasIter) - } + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter); } - impl<'a, T: RepSliceExt + ?Sized> RepSliceExt for &'a T { - type Item = T::Item; + impl<'q, 'a, T: RepAsIteratorExt<'q> + ?Sized> RepAsIteratorExt<'q> for &'a T { + type Iter = T::Iter; - fn as_slice(&self) -> &[Self::Item] { - <T as RepSliceExt>::as_slice(*self) + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { + <T as RepAsIteratorExt>::quote_into_iter(*self) } } - impl<'a, T: RepSliceExt + ?Sized> RepSliceExt for &'a mut T { - type Item = T::Item; + impl<'q, 'a, T: RepAsIteratorExt<'q> + ?Sized> RepAsIteratorExt<'q> for &'a mut T { + type Iter = T::Iter; - fn as_slice(&self) -> &[Self::Item] { - <T as RepSliceExt>::as_slice(*self) + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { + <T as RepAsIteratorExt>::quote_into_iter(*self) } } - impl<T> RepSliceExt for [T] { - type Item = T; + impl<'q, T: 'q> RepAsIteratorExt<'q> for [T] { + type Iter = slice::Iter<'q, T>; - fn as_slice(&self) -> &[Self::Item] { - self + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { + (self.iter(), HasIter) } } - impl<T> RepSliceExt for Vec<T> { - type Item = T; + impl<'q, T: 'q> RepAsIteratorExt<'q> for Vec<T> { + type Iter = slice::Iter<'q, T>; - fn as_slice(&self) -> &[Self::Item] { - &self[..] + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { + (self.iter(), HasIter) } } macro_rules! array_rep_slice { ($($l:tt)*) => { $( - impl<T> RepSliceExt for [T; $l] { - type Item = T; + impl<'q, T: 'q> RepAsIteratorExt<'q> for [T; $l] { + type Iter = slice::Iter<'q, T>; - fn as_slice(&self) -> &[Self::Item] { - &self[..] + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { + (self.iter(), HasIter) } } )* @@ -139,6 +132,14 @@ pub mod ext { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ); + + impl<'q, T: RepAsIteratorExt<'q>> RepAsIteratorExt<'q> for RepInterp<T> { + type Iter = T::Iter; + + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { + self.0.quote_into_iter() + } + } } // Helper type used within interpolations to allow for repeated binding names. @@ -156,14 +157,6 @@ impl<T> RepInterp<T> { } } -impl<T: ext::RepSliceExt> ext::RepSliceExt for RepInterp<T> { - type Item = T::Item; - - fn as_slice(&self) -> &[Self::Item] { - self.0.as_slice() - } -} - impl<T: Iterator> Iterator for RepInterp<T> { type Item = T::Item; |