Skip to content

Commit 81256dd

Browse files
committed
Support arbitrary containers by IntoIterator blanket impl
1 parent 2f18cb3 commit 81256dd

1 file changed

Lines changed: 14 additions & 62 deletions

File tree

src/runtime.rs

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,8 @@ impl BitOr<HasIterator> for HasIterator {
4545
/// whichever impl happens to be applicable. Calling that method repeatedly on
4646
/// the returned value should be idempotent.
4747
pub mod ext {
48-
use super::RepInterp;
4948
use super::{HasIterator as HasIter, ThereIsNoIteratorInRepetition as DoesNotHaveIter};
5049
use crate::ToTokens;
51-
use core::slice;
52-
use std::collections::btree_set::{self, BTreeSet};
5350

5451
/// Extension trait providing the `quote_into_iter` method on iterators.
5552
pub trait RepIteratorExt: Iterator + Sized {
@@ -60,6 +57,16 @@ pub mod ext {
6057

6158
impl<T: Iterator> RepIteratorExt for T {}
6259

60+
/// Extension trait providing the `quote_into_iter` method on containers
61+
/// implementing IntoIterator.
62+
pub trait RepIntoIteratorExt: IntoIterator + Copy + Sized {
63+
fn quote_into_iter(self) -> (Self::IntoIter, HasIter) {
64+
(self.into_iter(), HasIter)
65+
}
66+
}
67+
68+
impl<T: IntoIterator + Copy> RepIntoIteratorExt for T {}
69+
6370
/// Extension trait providing the `quote_into_iter` method for
6471
/// non-iterable types. These types interpolate the same value in each
6572
/// iteration of the repetition.
@@ -77,62 +84,6 @@ pub mod ext {
7784
}
7885

7986
impl<T: ToTokens + ?Sized> RepToTokensExt for T {}
80-
81-
/// Extension trait providing the `quote_into_iter` method for types that
82-
/// can be referenced as an iterator.
83-
pub trait RepAsIteratorExt<'q> {
84-
type Iter: Iterator;
85-
86-
fn quote_into_iter(&'q self) -> (Self::Iter, HasIter);
87-
}
88-
89-
impl<'q, 'a, T: RepAsIteratorExt<'q> + ?Sized> RepAsIteratorExt<'q> for &'a T {
90-
type Iter = T::Iter;
91-
92-
fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) {
93-
<T as RepAsIteratorExt>::quote_into_iter(*self)
94-
}
95-
}
96-
97-
impl<'q, 'a, T: RepAsIteratorExt<'q> + ?Sized> RepAsIteratorExt<'q> for &'a mut T {
98-
type Iter = T::Iter;
99-
100-
fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) {
101-
<T as RepAsIteratorExt>::quote_into_iter(*self)
102-
}
103-
}
104-
105-
impl<'q, T: 'q> RepAsIteratorExt<'q> for [T] {
106-
type Iter = slice::Iter<'q, T>;
107-
108-
fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) {
109-
(self.iter(), HasIter)
110-
}
111-
}
112-
113-
impl<'q, T: 'q> RepAsIteratorExt<'q> for Vec<T> {
114-
type Iter = slice::Iter<'q, T>;
115-
116-
fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) {
117-
(self.iter(), HasIter)
118-
}
119-
}
120-
121-
impl<'q, T: 'q> RepAsIteratorExt<'q> for BTreeSet<T> {
122-
type Iter = btree_set::Iter<'q, T>;
123-
124-
fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) {
125-
(self.iter(), HasIter)
126-
}
127-
}
128-
129-
impl<'q, T: RepAsIteratorExt<'q>> RepAsIteratorExt<'q> for RepInterp<T> {
130-
type Iter = T::Iter;
131-
132-
fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) {
133-
self.0.quote_into_iter()
134-
}
135-
}
13687
}
13788

13889
// Helper type used within interpolations to allow for repeated binding names.
@@ -150,11 +101,12 @@ impl<T> RepInterp<T> {
150101
}
151102
}
152103

153-
impl<T: Iterator> Iterator for RepInterp<T> {
104+
impl<T: IntoIterator> IntoIterator for RepInterp<T> {
154105
type Item = T::Item;
106+
type IntoIter = T::IntoIter;
155107

156-
fn next(&mut self) -> Option<Self::Item> {
157-
self.0.next()
108+
fn into_iter(self) -> Self::IntoIter {
109+
self.0.into_iter()
158110
}
159111
}
160112

0 commit comments

Comments
 (0)