diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2022-08-09 12:59:20 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2022-08-09 13:27:13 +0200 |
commit | 334a838d78435b1aa40496dbfccd4e8b986f11a7 (patch) | |
tree | f9012e90b2c01def39a19e92694b956f472c521a | |
parent | 07a9b55ab739adfac59a57a978c5aa0e7406a40e (diff) |
Format code
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/failable/filter.rs | 105 | ||||
-rw-r--r-- | src/failable/mod.rs | 1 | ||||
-rw-r--r-- | src/failable/ops/and.rs | 8 | ||||
-rw-r--r-- | src/failable/ops/bool.rs | 5 | ||||
-rw-r--r-- | src/failable/ops/map.rs | 14 | ||||
-rw-r--r-- | src/failable/ops/mod.rs | 4 | ||||
-rw-r--r-- | src/failable/ops/not.rs | 5 | ||||
-rw-r--r-- | src/failable/ops/or.rs | 8 | ||||
-rw-r--r-- | src/failable/ops/xor.rs | 9 | ||||
-rw-r--r-- | src/filter.rs | 141 | ||||
-rw-r--r-- | src/impl_traits.rs | 1 | ||||
-rw-r--r-- | src/iter.rs | 123 | ||||
-rw-r--r-- | src/lib.rs | 8 | ||||
-rw-r--r-- | src/ops/and.rs | 2 | ||||
-rw-r--r-- | src/ops/bool.rs | 4 | ||||
-rw-r--r-- | src/ops/failable.rs | 8 | ||||
-rw-r--r-- | src/ops/map.rs | 9 | ||||
-rw-r--r-- | src/ops/mod.rs | 4 | ||||
-rw-r--r-- | src/ops/not.rs | 2 | ||||
-rw-r--r-- | src/ops/or.rs | 2 | ||||
-rw-r--r-- | src/ops/xor.rs | 2 |
21 files changed, 260 insertions, 205 deletions
diff --git a/src/failable/filter.rs b/src/failable/filter.rs index 3917a61..63e9ff0 100644 --- a/src/failable/filter.rs +++ b/src/failable/filter.rs @@ -8,10 +8,10 @@ use std::borrow::Borrow; pub use failable::ops::and::FailableAnd; pub use failable::ops::bool::FailableBool; +pub use failable::ops::map::{FailableMapErr, FailableMapInput}; pub use failable::ops::not::FailableNot; -pub use failable::ops::xor::FailableXOr; pub use failable::ops::or::FailableOr; -pub use failable::ops::map::{FailableMapInput, FailableMapErr}; +pub use failable::ops::xor::FailableXOr; /// Trait for converting something into a Filter pub trait IntoFailableFilter<N> { @@ -48,7 +48,8 @@ pub trait FailableFilter<N> { /// assert!(f.filter(&2).unwrap()); /// ``` fn not(self) -> FailableNot<Self> - where Self: Sized + where + Self: Sized, { FailableNot::new(self) } @@ -70,8 +71,9 @@ pub trait FailableFilter<N> { /// assert!(!c.filter(&7).unwrap()); /// ``` fn or<F>(self, other: F) -> FailableOr<Self, F::IntoFilt> - where Self: Sized, - F: IntoFailableFilter<N> + Sized + where + Self: Sized, + F: IntoFailableFilter<N> + Sized, { FailableOr::new(self, other.into_failable_filter()) } @@ -93,8 +95,9 @@ pub trait FailableFilter<N> { /// assert!(c.filter(&7).unwrap()); /// ``` fn or_not<F>(self, other: F) -> FailableOr<Self, FailableNot<F::IntoFilt>> - where Self: Sized, - F: IntoFailableFilter<N> + Sized, + where + Self: Sized, + F: IntoFailableFilter<N> + Sized, { self.or(FailableNot::new(other.into_failable_filter())) } @@ -117,12 +120,20 @@ pub trait FailableFilter<N> { /// assert!(d.filter(&3).unwrap()); /// assert!(!d.filter(&4).unwrap()); /// ``` - fn or3<F, F2>(self, other: F, other2: F2) -> FailableOr<Self, FailableOr<F::IntoFilt, F2::IntoFilt>> - where Self: Sized, - F: IntoFailableFilter<N> + Sized, - F2: IntoFailableFilter<N> + Sized + fn or3<F, F2>( + self, + other: F, + other2: F2, + ) -> FailableOr<Self, FailableOr<F::IntoFilt, F2::IntoFilt>> + where + Self: Sized, + F: IntoFailableFilter<N> + Sized, + F2: IntoFailableFilter<N> + Sized, { - FailableOr::new(self, FailableOr::new(other.into_failable_filter(), other2.into_failable_filter())) + FailableOr::new( + self, + FailableOr::new(other.into_failable_filter(), other2.into_failable_filter()), + ) } /// Helper to connect two filters via logical NOR @@ -143,7 +154,8 @@ pub trait FailableFilter<N> { /// assert!(c.filter(&4).unwrap()); /// ``` fn nor<F>(self, other: F) -> FailableNot<FailableOr<Self, F>> - where Self: Sized, + where + Self: Sized, { FailableNot::new(FailableOr::new(self, other)) } @@ -167,7 +179,8 @@ pub trait FailableFilter<N> { /// assert!(c.filter(&9).unwrap()); /// ``` fn xor<F>(self, other: F) -> FailableXOr<Self, F> - where Self: Sized, + where + Self: Sized, { FailableXOr::new(self, other) } @@ -191,8 +204,9 @@ pub trait FailableFilter<N> { /// assert!(!c.filter(&9).unwrap()); /// ``` fn and<F>(self, other: F) -> FailableAnd<Self, F::IntoFilt> - where Self: Sized, - F: IntoFailableFilter<N> + Sized + where + Self: Sized, + F: IntoFailableFilter<N> + Sized, { FailableAnd::new(self, other.into_failable_filter()) } @@ -218,12 +232,20 @@ pub trait FailableFilter<N> { /// assert!(!d.filter(&15).unwrap()); /// assert!(!d.filter(&19).unwrap()); /// ``` - fn and3<F, F2>(self, other: F, other2: F2) -> FailableAnd<Self, FailableAnd<F::IntoFilt, F2::IntoFilt>> - where Self: Sized, - F: IntoFailableFilter<N> + Sized, - F2: IntoFailableFilter<N> + Sized + fn and3<F, F2>( + self, + other: F, + other2: F2, + ) -> FailableAnd<Self, FailableAnd<F::IntoFilt, F2::IntoFilt>> + where + Self: Sized, + F: IntoFailableFilter<N> + Sized, + F2: IntoFailableFilter<N> + Sized, { - FailableAnd::new(self, FailableAnd::new(other.into_failable_filter(), other2.into_failable_filter())) + FailableAnd::new( + self, + FailableAnd::new(other.into_failable_filter(), other2.into_failable_filter()), + ) } /// Helper to connect two filters via logical AND and NOT @@ -247,8 +269,9 @@ pub trait FailableFilter<N> { /// assert!(c.filter(&29).unwrap()); /// ``` fn and_not<F>(self, other: F) -> FailableAnd<Self, FailableNot<F::IntoFilt>> - where Self: Sized, - F: IntoFailableFilter<N> + Sized + where + Self: Sized, + F: IntoFailableFilter<N> + Sized, { self.and(FailableNot::new(other.into_failable_filter())) } @@ -274,7 +297,8 @@ pub trait FailableFilter<N> { /// assert!(c.filter(&29).unwrap()); /// ``` fn nand<F>(self, other: F) -> FailableNot<FailableAnd<Self, F>> - where Self: Sized, + where + Self: Sized, { FailableNot::new(FailableAnd::new(self, other)) } @@ -299,9 +323,10 @@ pub trait FailableFilter<N> { /// assert!(!c.filter(&9).unwrap()); /// ``` fn map_input<O, B, T, M>(self, map: M) -> FailableMapInput<Self, M, O, B> - where Self: Sized, - M: Fn(&T) -> N, - B: Borrow<O> + Sized + where + Self: Sized, + M: Fn(&T) -> N, + B: Borrow<O> + Sized, { FailableMapInput::new(self, map) } @@ -327,17 +352,18 @@ pub trait FailableFilter<N> { /// assert!(!c.filter(&9).unwrap()); /// ``` fn map_err<M, OE>(self, map: M) -> FailableMapErr<Self, M, OE> - where Self: Sized, - M: Fn(Self::Error) -> OE + where + Self: Sized, + M: Fn(Self::Error) -> OE, { FailableMapErr::new(self, map) } - } /// All closures that take a ref to something and return Result<bool, E> are failable filters impl<I, E, T> FailableFilter<I> for T - where T: Fn(&I) -> Result<bool, E> +where + T: Fn(&I) -> Result<bool, E>, { type Error = E; @@ -351,7 +377,7 @@ mod tests { use super::*; #[derive(Debug)] - struct StupError { } + struct StupError {} #[test] fn compile_test() { @@ -400,26 +426,23 @@ mod tests { let c = |_: &i32| -> Result<bool, StupError> { Ok(true) }; let d = |_: &i32| -> bool { false }; - let e = a // true - .and(b.into_failable().map_err(|_| StupError {})) // true - .xor(c) // true - .or(d.into_failable().map_err(|_| StupError {})); // true + let e = a // true + .and(b.into_failable().map_err(|_| StupError {})) // true + .xor(c) // true + .or(d.into_failable().map_err(|_| StupError {})); // true assert!(!e.filter(&1).unwrap()); } - #[test] fn test_both_filter_types_in_one_scope() { - use filter::Filter; use failable::filter::FailableFilter; + use filter::Filter; - let failable = |_: &i32| -> Result<bool, StupError> { Ok(true) }; + let failable = |_: &i32| -> Result<bool, StupError> { Ok(true) }; let unfailable = |_: &i32| -> bool { true }; assert!(failable.filter(&1).unwrap()); assert!(unfailable.filter(&1)); - } } - diff --git a/src/failable/mod.rs b/src/failable/mod.rs index 2ae44a6..6380a7b 100644 --- a/src/failable/mod.rs +++ b/src/failable/mod.rs @@ -6,4 +6,3 @@ pub mod filter; pub mod ops; - diff --git a/src/failable/ops/and.rs b/src/failable/ops/and.rs index f0a1584..977a405 100644 --- a/src/failable/ops/and.rs +++ b/src/failable/ops/and.rs @@ -17,16 +17,15 @@ use failable::filter::FailableFilter; pub struct FailableAnd<T, U>(T, U); impl<T, U> FailableAnd<T, U> { - pub fn new(a: T, b: U) -> FailableAnd<T, U> { FailableAnd(a, b) } - } impl<N, T, U, E> FailableFilter<N> for FailableAnd<T, U> - where T: FailableFilter<N, Error = E>, - U: FailableFilter<N, Error = E> +where + T: FailableFilter<N, Error = E>, + U: FailableFilter<N, Error = E>, { type Error = E; @@ -34,4 +33,3 @@ impl<N, T, U, E> FailableFilter<N> for FailableAnd<T, U> Ok(self.0.filter(e)? && self.1.filter(e)?) } } - diff --git a/src/failable/ops/bool.rs b/src/failable/ops/bool.rs index ad68f63..ae6fc45 100644 --- a/src/failable/ops/bool.rs +++ b/src/failable/ops/bool.rs @@ -17,19 +17,15 @@ use failable::filter::FailableFilter; pub struct FailableBool(bool); impl FailableBool { - pub fn new(b: bool) -> FailableBool { FailableBool(b) } - } impl From<bool> for FailableBool { - fn from(b: bool) -> FailableBool { FailableBool::new(b) } - } impl<N> FailableFilter<N> for FailableBool { @@ -39,4 +35,3 @@ impl<N> FailableFilter<N> for FailableBool { Ok(self.0) } } - diff --git a/src/failable/ops/map.rs b/src/failable/ops/map.rs index 8dd3cc5..751c2e6 100644 --- a/src/failable/ops/map.rs +++ b/src/failable/ops/map.rs @@ -9,8 +9,8 @@ //! Will be automatically included when including `filter::Filter`, so importing this module //! shouldn't be necessary. //! -use std::marker::PhantomData; use std::borrow::Borrow; +use std::marker::PhantomData; use failable::filter::FailableFilter; @@ -25,9 +25,10 @@ impl<F, M, FT, B> FailableMapInput<F, M, FT, B> { } impl<FT, F, T, B, M> FailableFilter<T> for FailableMapInput<F, M, FT, B> - where F: FailableFilter<FT>, - B: Borrow<FT> + Sized, - M: Fn(&T) -> B +where + F: FailableFilter<FT>, + B: Borrow<FT> + Sized, + M: Fn(&T) -> B, { type Error = F::Error; @@ -47,8 +48,9 @@ impl<F, M, E> FailableMapErr<F, M, E> { } impl<E, F, T, M> FailableFilter<T> for FailableMapErr<F, M, E> - where F: FailableFilter<T>, - M: Fn(F::Error) -> E +where + F: FailableFilter<T>, + M: Fn(F::Error) -> E, { type Error = E; diff --git a/src/failable/ops/mod.rs b/src/failable/ops/mod.rs index 618c0e8..c8c2c28 100644 --- a/src/failable/ops/mod.rs +++ b/src/failable/ops/mod.rs @@ -6,7 +6,7 @@ pub mod and; pub mod bool; +pub mod map; pub mod not; -pub mod xor; pub mod or; -pub mod map; +pub mod xor; diff --git a/src/failable/ops/not.rs b/src/failable/ops/not.rs index bc55230..1108205 100644 --- a/src/failable/ops/not.rs +++ b/src/failable/ops/not.rs @@ -17,15 +17,14 @@ use failable::filter::FailableFilter; pub struct FailableNot<T>(T); impl<T> FailableNot<T> { - pub fn new(a: T) -> FailableNot<T> { FailableNot(a) } - } impl<N, T> FailableFilter<N> for FailableNot<T> - where T: FailableFilter<N> +where + T: FailableFilter<N>, { type Error = T::Error; diff --git a/src/failable/ops/or.rs b/src/failable/ops/or.rs index 9cebc75..0ccb469 100644 --- a/src/failable/ops/or.rs +++ b/src/failable/ops/or.rs @@ -17,16 +17,15 @@ use failable::filter::FailableFilter; pub struct FailableOr<T, U>(T, U); impl<T, U> FailableOr<T, U> { - pub fn new(a: T, b: U) -> FailableOr<T, U> { FailableOr(a, b) } - } impl<N, T, U, E> FailableFilter<N> for FailableOr<T, U> - where T: FailableFilter<N, Error = E>, - U: FailableFilter<N, Error = E> +where + T: FailableFilter<N, Error = E>, + U: FailableFilter<N, Error = E>, { type Error = E; @@ -34,4 +33,3 @@ impl<N, T, U, E> FailableFilter<N> for FailableOr<T, U> Ok(self.0.filter(e)? || self.1.filter(e)?) } } - diff --git a/src/failable/ops/xor.rs b/src/failable/ops/xor.rs index d8a83bb..74084ae 100644 --- a/src/failable/ops/xor.rs +++ b/src/failable/ops/xor.rs @@ -17,16 +17,15 @@ use failable::filter::FailableFilter; pub struct FailableXOr<T, U>(T, U); impl<T, U> FailableXOr<T, U> { - pub fn new(a: T, b: U) -> FailableXOr<T, U> { FailableXOr(a, b) } - } impl<N, T, U, E> FailableFilter<N> for FailableXOr<T, U> - where T: FailableFilter<N, Error = E>, - U: FailableFilter<N, Error = E> +where + T: FailableFilter<N, Error = E>, + U: FailableFilter<N, Error = E>, { type Error = E; @@ -34,5 +33,3 @@ impl<N, T, U, E> FailableFilter<N> for FailableXOr<T, U> Ok(self.0.filter(e)? ^ self.1.filter(e)?) } } - - diff --git a/src/filter.rs b/src/filter.rs index d4f2efd..78c9e9c 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -10,11 +10,11 @@ use std::borrow::Borrow; pub use ops::and::And; pub use ops::bool::Bool; +pub use ops::failable::{AsFailable, IntoFailable}; +pub use ops::map::MapInput; pub use ops::not::Not; -pub use ops::xor::XOr; pub use ops::or::Or; -pub use ops::map::MapInput; -pub use ops::failable::{IntoFailable, AsFailable}; +pub use ops::xor::XOr; /// Trait for converting something into a Filter pub trait IntoFilter<N> { @@ -41,7 +41,6 @@ impl<I, T: Fn(&I) -> bool> Filter<I> for T { /// The filter trait pub trait Filter<N> { - /// The function which is used to filter something fn filter(&self, &N) -> bool; @@ -55,7 +54,8 @@ pub trait Filter<N> { /// assert!(f.filter(&2)); /// ``` fn not(self) -> Not<Self> - where Self: Sized + where + Self: Sized, { Not::new(self) } @@ -74,8 +74,9 @@ pub trait Filter<N> { /// assert!(!c.filter(&7)); /// ``` fn or<F>(self, other: F) -> Or<Self, F::IntoFilt> - where Self: Sized, - F: IntoFilter<N> + Sized + where + Self: Sized, + F: IntoFilter<N> + Sized, { Or::new(self, other.into_filter()) } @@ -94,8 +95,9 @@ pub trait Filter<N> { /// assert!(c.filter(&7)); /// ``` fn or_not<F>(self, other: F) -> Or<Self, Not<F::IntoFilt>> - where Self: Sized, - F: IntoFilter<N> + Sized, + where + Self: Sized, + F: IntoFilter<N> + Sized, { self.or(Not::new(other.into_filter())) } @@ -116,9 +118,10 @@ pub trait Filter<N> { /// assert!(!d.filter(&4)); /// ``` fn or3<F, F2>(self, other: F, other2: F2) -> Or<Self, Or<F::IntoFilt, F2::IntoFilt>> - where Self: Sized, - F: IntoFilter<N> + Sized, - F2: IntoFilter<N> + Sized + where + Self: Sized, + F: IntoFilter<N> + Sized, + F2: IntoFilter<N> + Sized, { Or::new(self, Or::new(other.into_filter(), other2.into_filter())) } @@ -138,7 +141,8 @@ pub trait Filter<N> { /// assert!(c.filter(&4)); /// ``` fn nor<F>(self, other: F) -> Not<Or<Self, F>> - where Self: Sized, + where + Self: Sized, { Not::new(Or::new(self, other)) } @@ -159,7 +163,8 @@ pub trait Filter<N> { /// assert!(c.filter(&9)); /// ``` fn xor<F>(self, other: F) -> XOr<Self, F> - where Self: Sized, + where + Self: Sized, { XOr::new(self, other) } @@ -180,8 +185,9 @@ pub trait Filter<N> { /// assert!(!c.filter(&9)); /// ``` fn and<F>(self, other: F) -> And<Self, F::IntoFilt> - where Self: Sized, - F: IntoFilter<N> + Sized + where + Self: Sized, + F: IntoFilter<N> + Sized, { And::new(self, other.into_filter()) } @@ -205,9 +211,10 @@ pub trait Filter<N> { /// assert!(!d.filter(&19)); /// ``` fn and3<F, F2>(self, other: F, other2: F2) -> And<Self, And<F::IntoFilt, F2::IntoFilt>> - where Self: Sized, - F: IntoFilter<N> + Sized, - F2: IntoFilter<N> + Sized + where + Self: Sized, + F: IntoFilter<N> + Sized, + F2: IntoFilter<N> + Sized, { And::new(self, And::new(other.into_filter(), other2.into_filter())) } @@ -230,8 +237,9 @@ pub trait Filter<N> { /// assert!(c.filter(&29)); /// ``` fn and_not<F>(self, other: F) -> And<Self, Not<F::IntoFilt>> - where Self: Sized, - F: IntoFilter<N> + Sized + where + Self: Sized, + F: IntoFilter<N> + Sized, { self.and(Not::new(other.into_filter())) } @@ -254,7 +262,8 @@ pub trait Filter<N> { /// assert!(c.filter(&29)); /// ``` fn nand<F>(self, other: F) -> Not<And<Self, F>> - where Self: Sized, + where + Self: Sized, { Not::new(And::new(self, other)) } @@ -276,9 +285,10 @@ pub trait Filter<N> { /// assert!(!c.filter(&9)); /// ``` fn map_input<O, B, T, M>(self, map: M) -> MapInput<Self, M, O, B> - where Self: Sized, - M: Fn(&T) -> N, - B: Borrow<O> + Sized + where + Self: Sized, + M: Fn(&T) -> N, + B: Borrow<O> + Sized, { MapInput::new(self, map) } @@ -291,14 +301,15 @@ pub trait Filter<N> { /// /// let a = (|&a: &usize| { a > 5 }); /// let a = a.into_failable(); - /// + /// /// assert_eq!(a.filter(&3), Ok(false)); /// assert_eq!(a.filter(&5), Ok(false)); /// assert_eq!(a.filter(&7), Ok(true)); /// assert_eq!(a.filter(&9), Ok(true)); /// ``` fn into_failable(self) -> IntoFailable<Self> - where Self: Sized + where + Self: Sized, { IntoFailable::new(self) } @@ -312,10 +323,9 @@ macro_rules! make_filter { $expression(self, element) } } - } + }; } - #[cfg(test)] mod test { use filter::Filter; @@ -324,7 +334,7 @@ mod test { #[test] fn closures() { - let a = (|&a: &usize|{ a < 3 }).and(|&a: &usize| a > 1); + let a = (|&a: &usize| a < 3).and(|&a: &usize| a > 1); assert_eq!(a.filter(&0), false); assert_eq!(a.filter(&2), true); @@ -335,8 +345,8 @@ mod test { fn and_filter() { let a = And::new(|&a: &usize| a > 0, |&a: &usize| a == 3); - assert_eq!(a.filter(&3), true); - assert_eq!(a.filter(&5), false); + assert_eq!(a.filter(&3), true); + assert_eq!(a.filter(&5), false); assert_eq!(a.filter(&0), false); } @@ -351,7 +361,9 @@ mod test { #[test] fn complex_filter() { - let a = (|&a: &usize|{ a > 5 }).and_not(|&a: &usize| a < 20).or(|&a: &usize| a == 10); + let a = (|&a: &usize| a > 5) + .and_not(|&a: &usize| a < 20) + .or(|&a: &usize| a == 10); // We now have ((a > 5) && !(a < 20) ) || a == 10 assert_eq!(a.filter(&21), true); @@ -362,7 +374,8 @@ mod test { #[test] fn complex_filter_closured() { - let a = (|&a: &usize| (|&a: &usize|{ a > 5 }).and_not(|&a: &usize| a < 20).filter(&a)).or(|&a: &usize| a == 10); + let a = (|&a: &usize| (|&a: &usize| a > 5).and_not(|&a: &usize| a < 20).filter(&a)) + .or(|&a: &usize| a == 10); // We now have ((a > 5) && !(a < 20)) || a == 10 assert_eq!(a.filter(&21), true); @@ -373,9 +386,9 @@ mod test { #[test] fn complex_filter_named_closures() { - let not_eq_to_one = |&a: &usize| { a != 1 }; - let not_eq_to_two = |&a: &usize| { a != 2 }; - let not_eq_to_three = |&a: &usize| { a != 3 }; + let not_eq_to_one = |&a: &usize| a != 1; + let not_eq_to_two = |&a: &usize| a != 2; + let not_eq_to_three = |&a: &usize| a != 3; let a = not_eq_to_one.and(not_eq_to_two).and(not_eq_to_three); // We now have ((a > 5) && !(a < 20)) || a == 10 @@ -388,16 +401,16 @@ mod test { #[test] fn filter_with_bool() { - let eq = |&a: &usize| { a == 1 }; + let eq = |&a: &usize| a == 1; assert_eq!(eq.and(Bool::new(true)).filter(&0), false); - let eq = |&a: &usize| { a == 1 }; + let eq = |&a: &usize| a == 1; assert_eq!(eq.and(Bool::new(true)).filter(&1), true); - let eq = |&a: &usize| { a == 1 }; + let eq = |&a: &usize| a == 1; assert_eq!(eq.xor(Bool::new(true)).filter(&1), false); - let eq = |&a: &usize| { a == 1 }; + let eq = |&a: &usize| a == 1; assert_eq!(eq.or(Bool::new(true)).filter(&42), true); } @@ -414,8 +427,8 @@ mod test { #[test] fn filter_with_eqto() { let eq = EqTo { i: 0 }; - assert_eq!(eq.filter(&0), true); - assert_eq!(eq.filter(&1), false); + assert_eq!(eq.filter(&0), true); + assert_eq!(eq.filter(&1), false); assert_eq!(eq.filter(&17), false); assert_eq!(eq.filter(&42), false); } @@ -424,19 +437,21 @@ mod test { fn filter_with_combined_eqto() { let aeq = EqTo { i: 1 }.not().and_not(EqTo { i: 17 }); - assert_eq!(aeq.filter(&0), true); - assert_eq!(aeq.filter(&1), false); - assert_eq!(aeq.filter(&2), true); + assert_eq!(aeq.filter(&0), true); + assert_eq!(aeq.filter(&1), false); + assert_eq!(aeq.filter(&2), true); assert_eq!(aeq.filter(&17), false); } #[test] fn filter_iterator() { - let v = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; + let v = vec![ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + ]; - let inrange = (|&a: &usize| { a > 5 }).and(|&a: &usize| { a < 15 }); + let inrange = (|&a: &usize| a > 5).and(|&a: &usize| a < 15); - let r : Vec<usize> = v.into_iter().filter(|x| inrange.filter(x)).collect(); + let r: Vec<usize> = v.into_iter().filter(|x| inrange.filter(x)).collect(); assert_eq!(r, vec![6, 7, 8, 9, 10, 11, 12, 13, 14]); } @@ -449,14 +464,13 @@ mod test { }; let lt = LowerThan(10); - assert_eq!(lt.filter(&0), true); - assert_eq!(lt.filter(&1), true); + assert_eq!(lt.filter(&0), true); + assert_eq!(lt.filter(&1), true); assert_eq!(lt.filter(&17), false); assert_eq!(lt.filter(&42), false); } } - #[cfg(test)] #[cfg(feature = "unstable-filter-as-fn")] mod test_unstable { @@ -465,7 +479,7 @@ mod test_unstable { #[test] fn closures() { - let a = (|&a: &usize|{ a < 3 }).and(|&a: &usize| a > 1); + let a = (|&a: &usize| a < 3).and(|&a: &usize| a > 1); assert_eq!(a(&0), false); assert_eq!(a(&2), true); @@ -483,7 +497,9 @@ mod test_unstable { #[test] fn complex_filter() { - let a = (|&a: &usize|{ a > 5 }).and_not(|&a: &usize| a < 20).or(|&a: &usize| a == 10); + let a = (|&a: &usize| a > 5) + .and_not(|&a: &usize| a < 20) + .or(|&a: &usize| a == 10); // We now have ((a > 5) && !(a < 20) ) || a == 10 assert_eq!(a(&21), true); @@ -492,28 +508,29 @@ mod test_unstable { #[test] fn filter_with_bool() { - let eq = |&a: &usize| { a == 1 }; + let eq = |&a: &usize| a == 1; assert_eq!(eq.and(Bool::new(true))(&0), false); - let eq = |&a: &usize| { a == 1 }; + let eq = |&a: &usize| a == 1; assert_eq!(eq.and(Bool::new(true))(&1), true); - let eq = |&a: &usize| { a == 1 }; + let eq = |&a: &usize| a == 1; assert_eq!(eq.xor(Bool::new(true))(&1), false); - let eq = |&a: &usize| { a == 1 }; + let eq = |&a: &usize| a == 1; assert_eq!(eq.or(Bool::new(true))(&42), true); } #[test] fn filter_iterator() { - let v = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; + let v = vec![ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + ]; - let inrange = (|&a: &usize| { a > 5 }).and(|&a: &usize| { a < 15 }); + let inrange = (|&a: &usize| a > 5).and(|&a: &usize| a < 15); - let r : Vec<usize> = v.into_iter().filter(inrange).collect(); + let r: Vec<usize> = v.into_iter().filter(inrange).collect(); assert_eq!(r, vec![6, 7, 8, 9, 10, 11, 12, 13, 14]); } } - diff --git a/src/impl_traits.rs b/src/impl_traits.rs index 03e616e..770e44f 100644 --- a/src/impl_traits.rs +++ b/src/impl_traits.rs @@ -103,4 +103,3 @@ macro_rules! impl_failable_operators { impl_operators!($struct_ident, $self_var $arg_var $filter_impl, ); }; } - diff --git a/src/iter.rs b/src/iter.rs index 14b64a8..5ac77c2 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -7,12 +7,14 @@ use filter::Filter; pub struct FilteredIterator<T, F, I>(F, I) - where F: Filter<T>, - I: Iterator<Item = T>; +where + F: Filter<T>, + I: Iterator<Item = T>; impl<T, F, I> Iterator for FilteredIterator<T, F, I> - where F: Filter<T>, - I: Iterator<Item = T> +where + F: Filter<T>, + I: Iterator<Item = T>, { type Item = T; @@ -27,26 +29,28 @@ impl<T, F, I> Iterator for FilteredIterator<T, F, I> } } -pub trait FilterWith<T, F: Filter<T>> : Iterator<Item = T> + Sized { +pub trait FilterWith<T, F: Filter<T>>: Iterator<Item = T> + Sized { fn filter_with(self, f: F) -> FilteredIterator<T, F, Self>; } impl<I, T, F: Filter<T>> FilterWith<T, F> for I - where I: Iterator<Item = T> +where + I: Iterator<Item = T>, { fn filter_with(self, f: F) -> FilteredIterator<T, F, Self> { FilteredIterator(f, self) } } - pub struct FilterOksIter<T, E, I, F>(I, F) - where F: Filter<T>, - I: Iterator<Item = Result<T, E>>; +where + F: Filter<T>, + I: Iterator<Item = Result<T, E>>; impl<T, E, I, F> Iterator for FilterOksIter<T, E, I, F> - where F: Filter<T>, - I: Iterator<Item = Result<T, E>> +where + F: Filter<T>, + I: Iterator<Item = Result<T, E>>, { type Item = Result<T, E>; @@ -54,8 +58,10 @@ impl<T, E, I, F> Iterator for FilterOksIter<T, E, I, F> while let Some(next) = self.0.next() { match next { Err(e) => return Some(Err(e)), - Ok(t) => if self.1.filter(&t) { - return Some(Ok(t)); + Ok(t) => { + if self.1.filter(&t) { + return Some(Ok(t)); + } } } } @@ -64,16 +70,18 @@ impl<T, E, I, F> Iterator for FilterOksIter<T, E, I, F> } } -pub trait FilterOks<T, E, I, F> : Iterator<Item = Result<T, E>> - where I: Iterator<Item = Result<T, E>>, - F: Filter<T> +pub trait FilterOks<T, E, I, F>: Iterator<Item = Result<T, E>> +where + I: Iterator<Item = Result<T, E>>, + F: Filter<T>, { fn filter_oks(self, F) -> FilterOksIter<T, E, I, F>; } impl<T, E, I, F> FilterOks<T, E, I, F> for I - where I: Iterator<Item = Result<T, E>>, - F: Filter<T> +where |