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
pub trait Apply: Sized {
    #[inline(always)]
    fn apply(self, block: impl FnOnce(&Self)) -> Self {
        block(&self);
        self
    }

    #[inline(always)]
    fn apply_mut(mut self, block: impl FnOnce(&mut Self)) -> Self {
        block(&mut self);
        self
    }
}

impl<T: Sized> Apply for T {}

pub trait Run: Sized {
    #[inline(always)]
    fn run<R>(self, block: impl FnOnce(Self) -> R) -> R {
        block(self)
    }
}

impl<T: Sized> Run for T {}

pub trait TakeIf: Sized {
    #[inline(always)]
    fn take_if(self, predicate: impl FnOnce(&Self) -> bool) -> Option<Self> {
        if predicate(&self) {
            Some(self)
        } else {
            None
        }
    }

    #[inline(always)]
    fn take_unless(self, predicate: impl FnOnce(&Self) -> bool) -> Option<Self> {
        self.take_if(
            #[inline(always)]
            |x| !predicate(x),
        )
    }
}

impl<T: Sized> TakeIf for T {}