objc2_foundation/
comparison_result.rs

1use core::cmp::Ordering;
2
3use objc2::encode::{Encode, Encoding, RefEncode};
4
5/// Constants that indicate sort order.
6///
7/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nscomparisonresult?language=objc).
8#[repr(isize)] // NSInteger
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
10pub enum NSComparisonResult {
11    /// The left operand is smaller than the right operand.
12    Ascending = -1,
13    /// The two operands are equal.
14    Same = 0,
15    /// The left operand is greater than the right operand.
16    Descending = 1,
17}
18
19impl Default for NSComparisonResult {
20    #[inline]
21    fn default() -> Self {
22        Self::Same
23    }
24}
25
26unsafe impl Encode for NSComparisonResult {
27    const ENCODING: Encoding = isize::ENCODING;
28}
29
30unsafe impl RefEncode for NSComparisonResult {
31    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
32}
33
34impl From<Ordering> for NSComparisonResult {
35    #[inline]
36    fn from(order: Ordering) -> Self {
37        match order {
38            Ordering::Less => Self::Ascending,
39            Ordering::Equal => Self::Same,
40            Ordering::Greater => Self::Descending,
41        }
42    }
43}
44
45impl From<NSComparisonResult> for Ordering {
46    #[inline]
47    fn from(comparison_result: NSComparisonResult) -> Self {
48        match comparison_result {
49            NSComparisonResult::Ascending => Self::Less,
50            NSComparisonResult::Same => Self::Equal,
51            NSComparisonResult::Descending => Self::Greater,
52        }
53    }
54}