// // 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 including `filter::Filter`, so importing this module //! shouldn't be necessary. //! use std::marker::PhantomData; use std::borrow::Borrow; use failable::filter::FailableFilter; #[must_use = "filters are lazy and do nothing unless consumed"] #[derive(Clone)] pub struct FailableMapInput(F, M, PhantomData, PhantomData); impl FailableMapInput { pub fn new(a: F, m: M) -> FailableMapInput { FailableMapInput(a, m, PhantomData, PhantomData) } } impl FailableFilter for FailableMapInput where F: FailableFilter, B: Borrow + Sized, M: Fn(&T) -> B { type Error = F::Error; fn filter(&self, e: &T) -> Result { self.0.filter(self.1(e).borrow()) } } #[must_use = "filters are lazy and do nothing unless consumed"] #[derive(Clone)] pub struct FailableMapErr(F, M, PhantomData); impl FailableMapErr { pub fn new(a: F, m: M) -> FailableMapErr { FailableMapErr(a, m, PhantomData) } } impl FailableFilter for FailableMapErr where F: FailableFilter, M: Fn(F::Error) -> E { type Error = E; fn filter(&self, e: &T) -> Result { self.0.filter(e).map_err(&self.1) } }