Skip to content

Commit

Permalink
refactor: forall
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Sep 15, 2024
1 parent bedf97e commit 5048cf9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
16 changes: 0 additions & 16 deletions src/rule/for_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ pub trait Iterable<'a> {
fn into_iterator(self) -> Box<dyn Iterator<Item = Self::Item> + 'a>;
}

impl<'a, T: 'a> Iterable<'a> for Vec<T> {
type Item = T;

fn into_iterator(self) -> Box<dyn Iterator<Item = Self::Item> + 'a> {
Box::new(self.into_iter())
}
}

impl<'a> Iterable<'a> for String {
type Item = char;

fn into_iterator(self) -> Box<dyn Iterator<Item = Self::Item> + 'a> {
Box::new(self.chars().collect::<Vec<_>>().into_iter())
}
}

/// Rule where all the data in the collection satisfies the condition
pub struct ForAllRule<RULE, ITERABLE>
where
Expand Down
17 changes: 17 additions & 0 deletions src/rule/for_all/collection.rs
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
use crate::rule::Iterable;
use std::collections::{HashSet, VecDeque};

macro_rules! declare_iterable_for_collection {
($($t:ty),*) => {
$(
impl<'a, T: 'a> Iterable<'a> for $t {
type Item = T;

fn into_iterator(self) -> Box<dyn Iterator<Item = Self::Item> + 'a> {
Box::new(self.into_iter())
}
}
)*
};
}

declare_iterable_for_collection![Vec<T>, VecDeque<T>, HashSet<T>];
16 changes: 16 additions & 0 deletions src/rule/for_all/string.rs
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
use crate::rule::Iterable;

impl<'a> Iterable<'a> for String {
type Item = char;

fn into_iterator(self) -> Box<dyn Iterator<Item = Self::Item> + 'a> {
Box::new(self.chars().collect::<Vec<_>>().into_iter())
}
}

impl<'a> Iterable<'a> for &'a str {
type Item = char;

fn into_iterator(self) -> Box<dyn Iterator<Item = Self::Item> + 'a> {
Box::new(self.chars())
}
}

0 comments on commit 5048cf9

Please sign in to comment.