el_macro/bind/
into_result.rs

1/// Tells the [`crate::bind!`] macro whether the given expression has a bindable value or an error.
2///
3/// Enables the [`crate::bind!`] macro to determine by representing the value as a [`Result`]
4/// whether to create a variable and bind it to the value, or to call the optional error handler
5/// and evaluate the execution flow control block.
6///
7/// Implemented by default for [`Result`] and [`Option`], with `()` as `Error` for the latter.
8///
9/// For the usage example, refer to the [`crate::bind!`] macro documentation, which includes
10/// an example of using it with user-defined types.
11pub trait IntoResult {
12
13    /// Type of the value that the [`crate::bind!`] macro binds the created variable to.
14    type Value;
15    /// Type of the error that the [`crate::bind!`] macro passes as the only argument
16    /// to the optional error handler.
17    type Error;
18
19    /// Represents the expression value as [`Result`]
20    fn into_result(self) -> Result<Self::Value, Self::Error>;
21
22}
23
24
25impl<T> IntoResult for Option<T> {
26
27    type Value = T;
28    type Error = ();
29
30    fn into_result(self) -> Result<Self::Value, Self::Error> {
31        self.ok_or(())
32    }
33
34}
35
36
37impl<T, E> IntoResult for Result<T, E> {
38
39    type Value = T;
40    type Error = E;
41
42    fn into_result(self) -> Result<Self::Value, Self::Error> {
43        self
44    }
45
46}
47
48
49impl<'a, T> IntoResult for &'a std::sync::Mutex<T> {
50
51    type Value = std::sync::MutexGuard<'a, T>;
52    type Error = std::sync::PoisonError<Self::Value>;
53
54    fn into_result(self) -> Result<Self::Value, Self::Error> {
55        self.lock()
56    }
57
58}