summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-08-15 18:47:09 +0200
committerMatthias Beyer <mail@beyermatthias.de>2016-08-15 19:02:28 +0200
commit0982c485cace07457e69076fb9b21bdb78d13e97 (patch)
tree6dd87f863a6c28c0100ed9540c2720d4ca8a00be
parentb83a4b44300a3fe96b7335b2697c32f69c00b580 (diff)
Add bool helper for filter construction
-rw-r--r--src/filter.rs17
-rw-r--r--src/ops/bool.rs37
-rw-r--r--src/ops/mod.rs1
3 files changed, 55 insertions, 0 deletions
diff --git a/src/filter.rs b/src/filter.rs
index c2b887e..388c975 100644
--- a/src/filter.rs
+++ b/src/filter.rs
@@ -2,6 +2,7 @@
//!
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;
@@ -189,6 +190,7 @@ pub trait Filter<N> {
mod test {
use filter::Filter;
use ops::and::And;
+ use ops::bool::Bool;
#[test]
fn closures() {
@@ -252,6 +254,21 @@ mod test {
assert_eq!(a.filter(&3), false);
}
+ #[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!(eq.and(Bool::new(true)).filter(&1), true);
+
+ let eq = |&a: &usize| { a == 1 };
+ assert_eq!(eq.or(Bool::new(true)).filter(&17), true);
+
+ let eq = |&a: &usize| { a == 1 };
+ assert_eq!(eq.or(Bool::new(true)).filter(&42), true);
+ }
+
struct EqTo {
pub i: usize,
}
diff --git a/src/ops/bool.rs b/src/ops/bool.rs
new file mode 100644
index 0000000..fa33da2
--- /dev/null
+++ b/src/ops/bool.rs
@@ -0,0 +1,37 @@
+//! Bool Filter implementation, so we can insert this in filter construction
+//!
+//! Will be automatically included when incluing `filter::Filter`, so importing this module
+//! shouldn't be necessary.
+//!
+use filter::Filter;
+
+#[must_use = "filters are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct Bool {
+ b: bool
+}
+
+impl Bool {
+
+ pub fn new(b: bool) -> Bool {
+ Bool { b: b }
+ }
+
+}
+
+impl<I> Filter<I> for Bool {
+
+ fn filter(&self, _: &I) -> bool {
+ self.b
+ }
+
+}
+
+impl From<bool> for Bool {
+
+ fn from(b: bool) -> Bool {
+ Bool::new(b)
+ }
+
+}
+
diff --git a/src/ops/mod.rs b/src/ops/mod.rs
index e5881af..4349b89 100644
--- a/src/ops/mod.rs
+++ b/src/ops/mod.rs
@@ -1,4 +1,5 @@
pub mod and;
+pub mod bool;
pub mod not;
pub mod or;
pub mod xor;