Skip to main content

mtl_foundation/
objc_runtime.rs

1//! Objective-C runtime types.
2//!
3//! Corresponds to `Foundation/NSObjCRuntime.hpp`.
4//!
5//! # C++ Equivalent
6//!
7//! ```cpp
8//! namespace NS {
9//! _NS_ENUM(Integer, ComparisonResult) {
10//!     OrderedAscending = -1L,
11//!     OrderedSame,
12//!     OrderedDescending
13//! };
14//!
15//! const Integer NotFound = IntegerMax;
16//! }
17//! ```
18
19use crate::types::{INTEGER_MAX, Integer};
20
21/// Result of a comparison operation.
22///
23/// C++ equivalent: `NS::ComparisonResult`
24#[repr(transparent)]
25#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)]
26pub struct ComparisonResult(pub Integer);
27
28impl ComparisonResult {
29    /// Left operand is smaller than right operand.
30    ///
31    /// C++ equivalent: `NS::OrderedAscending`
32    pub const ORDERED_ASCENDING: Self = Self(-1);
33
34    /// Both operands are equal.
35    ///
36    /// C++ equivalent: `NS::OrderedSame`
37    pub const ORDERED_SAME: Self = Self(0);
38
39    /// Left operand is greater than right operand.
40    ///
41    /// C++ equivalent: `NS::OrderedDescending`
42    pub const ORDERED_DESCENDING: Self = Self(1);
43
44    /// Returns the raw integer value.
45    #[inline]
46    pub const fn raw(&self) -> Integer {
47        self.0
48    }
49
50    /// Creates from a raw integer value.
51    #[inline]
52    pub const fn from_raw(value: Integer) -> Self {
53        Self(value)
54    }
55}
56
57impl From<Integer> for ComparisonResult {
58    #[inline]
59    fn from(value: Integer) -> Self {
60        Self(value)
61    }
62}
63
64impl From<ComparisonResult> for Integer {
65    #[inline]
66    fn from(value: ComparisonResult) -> Self {
67        value.0
68    }
69}
70
71/// Sentinel value indicating "not found".
72///
73/// C++ equivalent: `NS::NotFound`
74pub const NOT_FOUND: Integer = INTEGER_MAX;
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_comparison_result_values() {
82        assert_eq!(ComparisonResult::ORDERED_ASCENDING.raw(), -1);
83        assert_eq!(ComparisonResult::ORDERED_SAME.raw(), 0);
84        assert_eq!(ComparisonResult::ORDERED_DESCENDING.raw(), 1);
85    }
86
87    #[test]
88    fn test_not_found() {
89        assert_eq!(NOT_FOUND, isize::MAX);
90    }
91}