diff options
author | Lee Bousfield <ljbousfield@gmail.com> | 2017-10-02 13:22:33 -0600 |
---|---|---|
committer | Lee Bousfield <ljbousfield@gmail.com> | 2017-10-02 13:22:33 -0600 |
commit | a8d0c99d991889125340c5e09c32bae8776189b9 (patch) | |
tree | cfea38afa6426a8f52d138595506696e1fb5f2b2 /src/ops | |
parent | 51c6b1e6a365112865dd579fd604d18d97d32c55 (diff) |
Add filter map functions
Diffstat (limited to 'src/ops')
-rw-r--r-- | src/ops/map.rs | 35 | ||||
-rw-r--r-- | src/ops/mod.rs | 1 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/ops/map.rs b/src/ops/map.rs new file mode 100644 index 0000000..b7a2028 --- /dev/null +++ b/src/ops/map.rs @@ -0,0 +1,35 @@ +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// + +//! Map implementation. +//! +//! Will be automatically included when incluing `filter::Filter`, so importing this module +//! shouldn't be necessary. +//! +use std::marker::PhantomData; +use std::borrow::Borrow; + +use filter::Filter; + +#[must_use = "filters are lazy and do nothing unless consumed"] +#[derive(Clone)] +pub struct MapInput<F, M, FT, B>(F, M, PhantomData<FT>, PhantomData<B>); + +impl<F, M, FT, B> MapInput<F, M, FT, B> { + pub fn new(a: F, m: M) -> MapInput<F, M, FT, B> { + MapInput(a, m, PhantomData, PhantomData) + } +} + +impl<FT, F, T, B, M> Filter<T> for MapInput<F, M, FT, B> + where F: Filter<FT>, + B: Borrow<FT> + Sized, + M: Fn(&T) -> B +{ + fn filter(&self, e: &T) -> bool { + self.0.filter(self.1(e).borrow()) + } +} diff --git a/src/ops/mod.rs b/src/ops/mod.rs index d152cce..a5dd6b6 100644 --- a/src/ops/mod.rs +++ b/src/ops/mod.rs @@ -9,3 +9,4 @@ pub mod bool; pub mod not; pub mod or; pub mod xor; +pub mod map; |