1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Contains the ffi-safe equivalent of `std::cmp::Ordering`.

use std::cmp::Ordering;

/// Ffi-safe equivalent of `std::cmp::Ordering`.
///
/// # Example
///
/// This defines an extern function, which compares a slice to another.
///
/// ```rust
///
/// use abi_stable::{
///     sabi_extern_fn,
///     std_types::{RCmpOrdering, RSlice},
/// };
/// use std::cmp::Ord;
///
/// #[sabi_extern_fn]
/// pub fn compare_slices<T>(l: RSlice<'_, T>, r: RSlice<'_, T>) -> RCmpOrdering
/// where
///     T: Ord,
/// {
///     l.cmp(&r).into()
/// }
///
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
#[repr(u8)]
#[derive(StableAbi)]
pub enum RCmpOrdering {
    ///
    Less,
    ///
    Equal,
    ///
    Greater,
}

impl RCmpOrdering {
    /// Converts this `RCmpOrdering` into a `std::cmp::Ordering`;
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RCmpOrdering;
    /// use std::cmp::Ordering;
    ///
    /// assert_eq!(RCmpOrdering::Less.to_ordering(), Ordering::Less);
    /// assert_eq!(RCmpOrdering::Equal.to_ordering(), Ordering::Equal);
    /// assert_eq!(RCmpOrdering::Greater.to_ordering(), Ordering::Greater);
    ///
    /// ```
    #[inline]
    pub const fn to_ordering(self) -> Ordering {
        match self {
            RCmpOrdering::Less => Ordering::Less,
            RCmpOrdering::Equal => Ordering::Equal,
            RCmpOrdering::Greater => Ordering::Greater,
        }
    }

    /// Converts a [`std::cmp::Ordering`] to [`RCmpOrdering`];
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RCmpOrdering;
    /// use std::cmp::Ordering;
    ///
    /// assert_eq!(RCmpOrdering::from_ordering(Ordering::Less), RCmpOrdering::Less);
    /// assert_eq!(RCmpOrdering::from_ordering(Ordering::Equal), RCmpOrdering::Equal);
    /// assert_eq!(RCmpOrdering::from_ordering(Ordering::Greater), RCmpOrdering::Greater);
    ///
    /// ```
    #[inline]
    pub const fn from_ordering(ordering: Ordering) -> RCmpOrdering {
        match ordering {
            Ordering::Less => RCmpOrdering::Less,
            Ordering::Equal => RCmpOrdering::Equal,
            Ordering::Greater => RCmpOrdering::Greater,
        }
    }
}

impl_from_rust_repr! {
    impl From<Ordering> for RCmpOrdering {
        fn(this){
            RCmpOrdering::from_ordering(this)
        }
    }
}

impl_into_rust_repr! {
    impl Into<Ordering> for RCmpOrdering {
        fn(this){
            this.to_ordering()
        }
    }
}