Skip to content

Commit

Permalink
Support arbitrary containers by IntoIterator blanket impl
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki committed Sep 8, 2022
1 parent 2f18cb3 commit bc07168
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ pub mod ext {
use super::RepInterp;
use super::{HasIterator as HasIter, ThereIsNoIteratorInRepetition as DoesNotHaveIter};
use crate::ToTokens;
use core::slice;
use std::collections::btree_set::{self, BTreeSet};

/// Extension trait providing the `quote_into_iter` method on iterators.
pub trait RepIteratorExt: Iterator + Sized {
Expand All @@ -60,6 +58,16 @@ pub mod ext {

impl<T: Iterator> RepIteratorExt for T {}

/// Extension trait providing the `quote_into_iter` method on containers
/// implementing IntoIterator.
pub trait RepIntoIteratorExt: IntoIterator + Copy + Sized {
fn quote_into_iter(self) -> (Self::IntoIter, HasIter) {
(self.into_iter(), HasIter)
}
}

impl<T: IntoIterator + Copy> RepIntoIteratorExt for T {}

/// Extension trait providing the `quote_into_iter` method for
/// non-iterable types. These types interpolate the same value in each
/// iteration of the repetition.
Expand Down Expand Up @@ -102,30 +110,6 @@ pub mod ext {
}
}

impl<'q, T: 'q> RepAsIteratorExt<'q> for [T] {
type Iter = slice::Iter<'q, T>;

fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) {
(self.iter(), HasIter)
}
}

impl<'q, T: 'q> RepAsIteratorExt<'q> for Vec<T> {
type Iter = slice::Iter<'q, T>;

fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) {
(self.iter(), HasIter)
}
}

impl<'q, T: 'q> RepAsIteratorExt<'q> for BTreeSet<T> {
type Iter = btree_set::Iter<'q, T>;

fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) {
(self.iter(), HasIter)
}
}

impl<'q, T: RepAsIteratorExt<'q>> RepAsIteratorExt<'q> for RepInterp<T> {
type Iter = T::Iter;

Expand Down

0 comments on commit bc07168

Please sign in to comment.