chain_tools/
xray.rs

1/// A placeholder trait intended for depreciation once [result_option_inspect](https://github.com/rust-lang/rust/issues/91345) becomes stable
2pub trait XRay {
3    type Item;
4    fn xray<F: FnOnce(&Self::Item)>(self, f: F) -> Self;
5}
6
7impl<T> XRay for Option<T> {
8    type Item = T;
9    fn xray<F: FnOnce(&T)>(self, f: F) -> Self {
10        if let Some(x) = &self {
11            f(x)
12        }
13
14        self
15    }
16}
17
18impl<T, E> XRay for Result<T, E> {
19    type Item = T;
20    fn xray<F: FnOnce(&Self::Item)>(self, f: F) -> Self {
21        if let Ok(x) = &self {
22            f(x)
23        }
24
25        self
26    }
27}
28
29pub trait XRayErr {
30    type ErrItem;
31    fn xray_err<F: FnOnce(&Self::ErrItem)>(self, f: F) -> Self;
32}
33
34impl<T, E> XRayErr for Result<T, E> {
35    type ErrItem = E;
36    fn xray_err<F: FnOnce(&Self::ErrItem)>(self, f: F) -> Self {
37        if let Err(x) = &self {
38            f(x)
39        }
40
41        self
42    }
43}