summaryrefslogtreecommitdiffstats
path: root/src/ops/failable.rs
blob: 5a92ba758d851b3428169477750b8bce23d1411c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// 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/.
//

//! Filter -> FailableFilter implementations
//!
//! Will be automatically included when incluing `filter::Filter`, so importing this module
//! shouldn't be necessary.
//!
use std::marker::PhantomData;

use filter::Filter;
use failable::filter::FailableFilter;

#[must_use = "filters are lazy and do nothing unless consumed"]
#[derive(Clone)]
pub struct IntoFailable<F, E>(F, PhantomData<E>);

impl<F, E> IntoFailable<F, E> {
    pub fn new(a: F) -> IntoFailable<F, E> {
        IntoFailable(a, PhantomData)
    }
}

impl<F, N, E> FailableFilter<N, E> for IntoFailable<F, E>
    where F: Filter<N>,
{
    fn filter(&self, e: &N) -> Result<bool, E> {
        Ok(self.0.filter(e))
    }
}

#[must_use = "filters are lazy and do nothing unless consumed"]
#[derive(Clone)]
pub struct AsFailable<'a, F: 'a + ?Sized, E>(&'a F, PhantomData<E>);

impl<'a, F: 'a + ?Sized, E> AsFailable<'a, F, E> {
    pub fn new(a: &'a F) -> AsFailable<F, E> {
        AsFailable(a, PhantomData)
    }
}

impl<'a, F, N, E> FailableFilter<N, E> for AsFailable<'a, F, E>
    where F: Filter<N> + 'a + ?Sized,
{
    fn filter(&self, e: &N) -> Result<bool, E> {
        Ok(self.0.filter(e))
    }
}