Skip to main content

orx_concurrent_option/common_traits/
ord.rs

1use crate::ConcurrentOption;
2use core::cmp::Ordering::*;
3
4impl<T: PartialOrd> PartialOrd for ConcurrentOption<T> {
5    /// Returns an ordering between `self` and `other` with the default ordering.
6    ///
7    /// You may call [`partial_cmp_with_order`] to use the desired ordering.
8    ///
9    /// [`partial_cmp_with_order`]: ConcurrentOption::partial_cmp_with_order
10    ///
11    /// ```rust
12    /// use orx_concurrent_option::*;
13    /// use core::cmp::Ordering::*;
14    ///
15    /// let x = ConcurrentOption::some(3);
16    /// let y = ConcurrentOption::some(7);
17    /// let z = ConcurrentOption::<i32>::none();
18    ///
19    /// assert_eq!(x.partial_cmp(&x), Some(Equal));
20    /// assert_eq!(x.partial_cmp(&y), Some(Less));
21    /// assert_eq!(x.partial_cmp(&z), Some(Greater));
22    ///
23    /// assert_eq!(y.partial_cmp(&x), Some(Greater));
24    /// assert_eq!(y.partial_cmp(&y), Some(Equal));
25    /// assert_eq!(y.partial_cmp(&z), Some(Greater));
26    ///
27    /// assert_eq!(z.partial_cmp(&x), Some(Less));
28    /// assert_eq!(z.partial_cmp(&y), Some(Less));
29    /// assert_eq!(z.partial_cmp(&z), Some(Equal));
30    /// ```
31    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
32        self.locked_compare(other, Some(Equal), Some(Greater), Some(Less), |l, r| {
33            l.partial_cmp(r)
34        })
35    }
36}
37
38impl<T: Ord> Ord for ConcurrentOption<T> {
39    /// Returns an ordering between `self` and `other` with the default ordering.
40    ///
41    /// You may call [`cmp_with_order`] to use the desired ordering.
42    ///
43    /// [`cmp_with_order`]: ConcurrentOption::cmp_with_order
44    ///
45    /// ```rust
46    /// use orx_concurrent_option::*;
47    /// use core::cmp::Ordering::*;
48    ///
49    /// let x = ConcurrentOption::some(3);
50    /// let y = ConcurrentOption::some(7);
51    /// let z = ConcurrentOption::<i32>::none();
52    ///
53    /// assert_eq!(x.cmp(&x), Equal);
54    /// assert_eq!(x.cmp(&y), Less);
55    /// assert_eq!(x.cmp(&z), Greater);
56    ///
57    /// assert_eq!(y.cmp(&x), Greater);
58    /// assert_eq!(y.cmp(&y), Equal);
59    /// assert_eq!(y.cmp(&z), Greater);
60    ///
61    /// assert_eq!(z.cmp(&x), Less);
62    /// assert_eq!(z.cmp(&y), Less);
63    /// assert_eq!(z.cmp(&z), Equal);
64    /// ```
65    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
66        self.locked_compare(other, Equal, Greater, Less, |l, r| l.cmp(r))
67    }
68}