diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/failable/filter.rs | 119 | ||||
-rw-r--r-- | src/failable/mod.rs | 1 | ||||
-rw-r--r-- | src/failable/ops/and.rs | 10 | ||||
-rw-r--r-- | src/failable/ops/bool.rs | 7 | ||||
-rw-r--r-- | src/failable/ops/map.rs | 16 | ||||
-rw-r--r-- | src/failable/ops/mod.rs | 4 | ||||
-rw-r--r-- | src/failable/ops/not.rs | 7 | ||||
-rw-r--r-- | src/failable/ops/or.rs | 10 | ||||
-rw-r--r-- | src/failable/ops/xor.rs | 11 | ||||
-rw-r--r-- | src/filter.rs | 237 | ||||
-rw-r--r-- | src/impl_traits.rs | 1 | ||||
-rw-r--r-- | src/iter.rs | 135 | ||||
-rw-r--r-- | src/lib.rs | 8 | ||||
-rw-r--r-- | src/ops/and.rs | 4 | ||||
-rw-r--r-- | src/ops/bool.rs | 6 | ||||
-rw-r--r-- | src/ops/failable.rs | 10 | ||||
-rw-r--r-- | src/ops/map.rs | 11 | ||||
-rw-r--r-- | src/ops/mod.rs | 4 | ||||
-rw-r--r-- | src/ops/not.rs | 4 | ||||
-rw-r--r-- | src/ops/or.rs | 4 | ||||
-rw-r--r-- | src/ops/xor.rs | 4 |
21 files changed, 334 insertions, 279 deletions
diff --git a/src/failable/filter.rs b/src/failable/filter.rs index 3917a61..fef191c 100644 --- a/src/failable/filter.rs +++ b/src/failable/filter.rs @@ -6,12 +6,12 @@ use std::borrow::Borrow; -pub use failable::ops::and::FailableAnd; -pub use failable::ops::bool::FailableBool; -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 crate::failable::ops::and::FailableAnd; +pub use crate::failable::ops::bool::FailableBool; +pub use crate::failable::ops::map::{FailableMapErr, FailableMapInput}; +pub use crate::failable::ops::not::FailableNot; +pub use crate::failable::ops::or::FailableOr; +pub use crate::failable::ops::xor::FailableXOr; /// Trait for converting something into a Filter pub trait IntoFailableFilter<N> { @@ -33,7 +33,7 @@ pub trait FailableFilter<N> { type Error: Sized; /// The function which is used to filter something - fn filter(&self, &N) -> Result<bool, Self::Error>; + fn filter(&self, _: &N) -> Result<bool, Self::Error>; /// Helper to invert a filter. /// @@ -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() { @@ -393,33 +419,30 @@ mod tests { #[test] fn test_both_filter_types() { - use filter::Filter; + use crate::filter::Filter; let a = |_: &i32| -> Result<bool, StupError> { Ok(true) }; let b = |_: &i32| -> bool { true }; 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 crate::failable::filter::FailableFilter; + use crate::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..f8d4636 100644 --- a/src/failable/ops/and.rs +++ b/src/failable/ops/and.rs @@ -10,23 +10,22 @@ //! shouldn't be necessary. //! -use failable::filter::FailableFilter; +use crate::failable::filter::FailableFilter; #[must_use = "filters are lazy and do nothing unless consumed"] #[derive(Clone)] 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..e0186f1 100644 --- a/src/failable/ops/bool.rs +++ b/src/failable/ops/bool.rs @@ -10,26 +10,22 @@ //! shouldn't be necessary. //! -use failable::filter::FailableFilter; +use crate::failable::filter::FailableFilter; #[must_use = "filters are lazy and do nothing unless consumed"] #[derive(Clone)] 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..5b28c03 100644 --- a/src/failable/ops/map.rs +++ b/src/failable/ops/map.rs @@ -9,10 +9,10 @@ //! 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; +use crate::failable::filter::FailableFilter; #[must_use = "filters are lazy and do nothing unless consumed"] #[derive(Clone)] @@ -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..3297605 100644 --- a/src/failable/ops/not.rs +++ b/src/failable/ops/not.rs @@ -10,22 +10,21 @@ //! shouldn't be necessary. //! -use failable::filter::FailableFilter; +use crate::failable::filter::FailableFilter; #[must_use = "filters are lazy and do nothing unless consumed"] #[derive(Clone)] 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..6927af9 100644 --- a/src/failable/ops/or.rs +++ b/src/failable/ops/or.rs @@ -10,23 +10,22 @@ //! shouldn't be necessary. //! -use failable::filter::FailableFilter; +use crate::failable::filter::FailableFilter; #[must_use = "filters are lazy and do nothing unless consumed"] #[derive(Clone)] 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..8b8dbbd 100644 --- a/src/failable/ops/xor.rs +++ b/src/failable/ops/xor.rs @@ -10,23 +10,22 @@ //! shouldn't be necessary. //! -use failable::filter::FailableFilter; +use crate::failable::filter::FailableFilter; #[must_use = "filters are lazy and do nothing unless consumed"] #[derive(Clone)] 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..70e8b65 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -8,13 +8,13 @@ //! use std::borrow::Borrow; -pub use ops::and::And; -pub use ops::bool::Bool; -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 crate::ops::and::And; +pub use crate::ops::bool::Bool; +pub use crate::ops::failable::{AsFailable, IntoFailable}; +pub use crate::ops::map::MapInput; +pub use crate::ops::not::Not; +pub use crate::ops::or::Or; +pub use crate::ops::xor::XOr; /// Trait for converting something into a Filter pub trait IntoFilter<N> { @@ -41,9 +41,8 @@ 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; + fn filter(&self, _: &N) -> bool; /// Helper to invert a filter. /// @@ -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,93 +323,95 @@ macro_rules! make_filter { $expression(self, element) } } - } + }; } - #[cfg(test)] mod test { - use filter::Filter; - use ops::and::And; - use ops::bool::Bool; + use crate::filter::Filter; + use crate::ops::and::And; + use crate::ops::bool::Bool; #[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); - assert_eq!(a.filter(&3), false); + assert!(!a.filter(&0)); + assert!(a.filter(&2)); + assert!(!a.filter(&3)); } #[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(&0), false); + assert!(a.filter(&3)); + assert!(!a.filter(&5)); + assert!(!a.filter(&0)); } #[test] fn xor_filter() { let a = (|&a: &usize| a == 0).xor(|&a: &usize| a == 3); - assert_eq!(a.filter(&3), true); - assert_eq!(a.filter(&5), false); - assert_eq!(a.filter(&0), true); + assert!(a.filter(&3)); + assert!(!a.filter(&5)); + assert!(a.filter(&0)); } #[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); - assert_eq!(a.filter(&10), true); - assert_eq!(a.filter(&11), false); - assert_eq!(a.filter(&5), false); + assert!(a.filter(&21)); + assert!(a.filter(&10)); + assert!(!a.filter(&11)); + assert!(!a.filter(&5)); } #[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); - assert_eq!(a.filter(&10), true); - assert_eq!(a.filter(&11), false); - assert_eq!(a.filter(&5), false); + assert!(a.filter(&21)); + assert!(a.filter(&10)); + assert!(!a.filter(&11)); + assert!(!a.filter(&5)); } #[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 - assert_eq!(a.filter(&21), true); - assert_eq!(a.filter(&10), true); - assert_eq!(a.filter(&1), false); - assert_eq!(a.filter(&3), false); + assert!(a.filter(&21)); + assert!(a.filter(&10)); + assert!(!a.filter(&1)); + assert!(!a.filter(&3)); } #[test] fn filter_with_bool() { - let eq = |&a: &usize| { a == 1 }; - assert_eq!(eq.and(Bool::new(true)).filter(&0), false); + let eq = |&a: &usize| a == 1; + assert!(!eq.and(Bool::new(true)).filter(&0)); - let eq = |&a: &usize| { a == 1 }; - assert_eq!(eq.and(Bool::new(true)).filter(&1), true); + let eq = |&a: &usize| a == 1; + assert!(eq.and(Bool::new(true)).filter(&1)); - let eq = |&a: &usize| { a == 1 }; - assert_eq!(eq.xor(Bool::new(true)).filter(&1), false); + let eq = |&a: &usize| a == 1; + assert!(!eq.xor(Bool::new(true)).filter(&1)); - let eq = |&a: &usize| { a == 1 }; - assert_eq!(eq.or(Bool::new(true)).filter(&42), true); + let eq = |&a: &usize| a == 1; + assert!(eq.or(Bool::new(true)).filter(&42)); } struct EqTo { @@ -414,29 +427,31 @@ 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(&17), false); - assert_eq!(eq.filter(&42), false); + assert!(eq.filter(&0)); + assert!(!eq.filter(&1)); + assert!(!eq.filter(&17)); + assert!(!eq.filter(&42)); } #[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(&17), false); + assert!(aeq.filter(&0)); + assert!(!aeq.filter(&1)); + assert!(aeq.filter(&2)); + assert!(!aeq.filter(&17)); } #[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(&17), false); - assert_eq!(lt.filter(&42), false); + assert!(lt.filter(&0)); + assert!(lt.filter(&1)); + assert!(!lt.filter(&17)); + assert!(!lt.filter(&42)); } } - #[cfg(test)] #[cfg(feature = "unstable-filter-as-fn")] mod test_unstable { @@ -465,55 +479,58 @@ 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); - assert_eq!(a(&3), false); + assert!(!a(&0)); + assert!(a(&2)); + assert!(!a(&3)); } #[test] fn xor_filter() { let a = (|&a: &usize| a == 0).xor(|&a: &usize| a == 3); - assert_eq!(a(&3), true); - assert_eq!(a(&5), false); - assert_eq!(a(&0), true); + assert!(a(&3)); + assert!(!a(&5)); + assert!(a(&0)); } #[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); - assert_eq!(a(&11), false); + assert!(a(&21)); + assert!(!a(&11)); } #[test] fn filter_with_bool() { - let eq = |&a: &usize| { a == 1 }; - assert_eq!(eq.and(Bool::new(true))(&0), false); + let eq = |&a: &usize| a == 1; + assert!(!eq.and(Bool::new(true))(&0)); - let eq = |&a: &usize| { a == 1 }; - assert_eq!(eq.and(Bool::new(true))(&1), true); + let eq = |&a: &usize| a == 1; + assert!(eq.and(Bool::new(true))(&1)); - let eq = |&a: &usize| { a == 1 }; - assert_eq!(eq.xor(Bool::new(true))(&1), false); + let eq = |&a: &usize| a == 1; + assert!(!eq.xor(Bool::new(true))(&1 |