ps_util/conversions/
result.rs

1pub trait ToResult {
2    #[inline(always)]
3    fn ok<Err>(self) -> Result<Self, Err>
4    where
5        Self: Sized,
6    {
7        Ok(self)
8    }
9
10    #[inline(always)]
11    fn err<Any>(self) -> Result<Any, Self>
12    where
13        Self: Sized,
14    {
15        Err(self)
16    }
17
18    #[inline(always)]
19    fn some(self) -> Option<Self>
20    where
21        Self: Sized,
22    {
23        Some(self)
24    }
25}
26
27impl<T> ToResult for T {}
28
29pub trait ResConv<T, E> {
30    fn into_option(self) -> Option<T>;
31    fn into_result(self) -> Result<T, E>;
32}
33
34impl<Input, Output, Err> ResConv<Output, Err> for Result<Input, Err>
35where
36    Input: Into<Output>,
37{
38    #[inline(always)]
39    fn into_option(self) -> Option<Output> {
40        match self {
41            Ok(value) => value.into().some(),
42            _ => None,
43        }
44    }
45
46    #[inline(always)]
47    fn into_result(self) -> Result<Output, Err> {
48        match self {
49            Ok(value) => value.into().ok(),
50            Err(err) => err.err(),
51        }
52    }
53}
54
55impl<Input, Output, Err: Default> ResConv<Output, Err> for Option<Input>
56where
57    Input: Into<Output>,
58{
59    #[inline(always)]
60    fn into_option(self) -> Option<Output> {
61        match self {
62            Some(value) => value.into().some(),
63            _ => None,
64        }
65    }
66
67    #[inline(always)]
68    fn into_result(self) -> Result<Output, Err> {
69        match self {
70            Some(value) => value.into().ok(),
71            _ => Err::default().err(),
72        }
73    }
74}