devela/code/result/opt_res/
ext_result.rs

1// devela::code::result::ext
2//
3//!
4//
5
6/// Marker trait to prevent downstream implementations of the [`ExtResult`] trait.
7trait Sealed {}
8impl<T, E> Sealed for Result<T, E> {}
9
10/// Extension trait providing additional methods for [`Result`].
11///
12/// This trait is sealed and cannot be implemented for any other type.
13///
14/// See also [`ExtOption`][crate::ExtOption].
15///
16/// Based on work from:
17/// - <https://github.com/rust-lang/rust/issues/62358> (contains).
18#[cfg_attr(feature = "nightly_doc", doc(notable_trait))]
19#[expect(private_bounds, reason = "Sealed")]
20pub trait ExtResult<T, E>: Sealed {
21    /// Returns `true` if the result is an [`Ok`] value containing the given value.
22    ///
23    /// # Examples
24    /// ```
25    /// # use devela::ExtResult;
26    /// assert_eq!(Ok::<_, ()>(1).contains(&1), true);
27    /// assert_eq!(Ok::<_, ()>(1).contains(&2), false);
28    /// assert_eq!(Err::<u8, _>("err").contains(&1), false);
29    /// ```
30    #[must_use]
31    fn contains<U: PartialEq<T>>(&self, x: &U) -> bool;
32
33    /// Returns `true` if the result is an [`Err`] value containing the given value.
34    ///
35    /// # Examples
36    /// ```
37    /// # use devela::ExtResult;
38    /// assert_eq!(Ok::<_, &str>(1).contains_err(&"Some error message"), false);
39    /// assert_eq!(Err::<u8, _>("err").contains_err(&"err"), true);
40    /// assert_eq!(Err::<u8, _>("err2").contains_err(&"err"), false);
41    /// ```
42    #[must_use]
43    fn contains_err<F: PartialEq<E>>(&self, f: &F) -> bool;
44
45    // WIP
46    // /// Merges `self` with another `Result`.
47    // ///
48    // /// Returns
49    // /// - `Ok(f(l, r))` if both options are `Ok(_)`.
50    // /// - `Err((le, re))` if any or both options are `Err(_)`.
51    // ///
52    // /// # Examples
53    // /// ```
54    // /// # use devela::ExtOption;
55    // /// # use core::{cmp::min, ops::Add};
56    // /// let x = Some(2);
57    // /// let y = Some(4);
58    // ///
59    // /// assert_eq!(x.reduce(y, Add::add), Some(6));
60    // /// assert_eq!(x.reduce(y, min), Some(2));
61    // ///
62    // /// assert_eq!(x.reduce(None, Add::add), x);
63    // /// assert_eq!(None.reduce(y, min), y);
64    // ///
65    // /// assert_eq!(None.reduce(None, i32::add), None);
66    // /// ```
67    // fn reduce<F: FnOnce(T, T) -> T>(self, other: Result<T, E>, f: F) -> Result<T, E>;
68}
69
70impl<T, E> ExtResult<T, E> for Result<T, E> {
71    fn contains<U: PartialEq<T>>(&self, x: &U) -> bool {
72        self.as_ref().is_ok_and(|y| x == y)
73    }
74
75    fn contains_err<F: PartialEq<E>>(&self, f: &F) -> bool {
76        self.as_ref().err().is_some_and(|e| f == e)
77    }
78
79    // // WIP
80    // fn reduce<F: FnOnce(T, T) -> T>(self, other: Result<T, E>, f: F) -> Result<T, E> {
81    //     match (self, other) {
82    //         (Some(l), Some(r)) => Some(f(l, r)),
83    //         (x @ Some(_), None) | (None, x @ Some(_)) => x,
84    //         (None, None) => None,
85    //     }
86    // }
87}