summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 705fecd555ffb451bfe2816627a1c73b4809d4bf (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
//! This crate adds the missing Result::inspect function via an extension trait
//!

pub trait ResultInspect<F, T>
    where F: FnOnce(&T),
          T: Sized
{
    fn inspect(self, f: F) -> Self;
}

pub trait ResultInspectRef<F, T>
    where F: FnOnce(&T),
          T: Sized
{
    fn inspect(&self, f: F);
}

impl<F, T, E> ResultInspect<F, T> for Result<T, E>
    where F: FnOnce(&T),
          T: Sized
{
    fn inspect(self, f: F) -> Self {
        if let Ok(ref o) = self.as_ref() {
            (f)(&o);
        }

        self
    }
}

impl<F, T, E> ResultInspectRef<F, T> for Result<T, E>
    where F: FnOnce(&T),
          T: Sized
{
    fn inspect(&self, f: F) {
        if let Ok(ref o) = self {
            (f)(&o);
        }
    }
}