Skip to content

Commit 063d612

Browse files
committed
Improve NSComparisonResult
1 parent 7826ca8 commit 063d612

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

objc2_foundation/src/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub unsafe trait INSMutableArray: INSArray {
288288
// ownership, and that method only runs one function at a time.
289289
let closure: &mut F = unsafe { &mut *(context as *mut F) };
290290

291-
NSComparisonResult::from_ordering((*closure)(obj1, obj2))
291+
NSComparisonResult::from((*closure)(obj1, obj2))
292292
}
293293

294294
let f: extern "C" fn(_, _, _) -> _ = compare_with_closure::<Self::Item, F>;
+24-12
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
use core::cmp::Ordering;
22

3-
use objc2::{Encode, Encoding};
3+
use objc2::{Encode, Encoding, RefEncode};
44

55
#[repr(isize)]
6-
#[derive(Clone, Copy)]
6+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
77
pub enum NSComparisonResult {
88
Ascending = -1,
99
Same = 0,
1010
Descending = 1,
1111
}
1212

13+
impl Default for NSComparisonResult {
14+
fn default() -> Self {
15+
Self::Same
16+
}
17+
}
18+
1319
unsafe impl Encode for NSComparisonResult {
1420
const ENCODING: Encoding<'static> = isize::ENCODING;
1521
}
1622

17-
impl NSComparisonResult {
18-
pub fn from_ordering(order: Ordering) -> NSComparisonResult {
23+
unsafe impl RefEncode for NSComparisonResult {
24+
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING);
25+
}
26+
27+
impl From<Ordering> for NSComparisonResult {
28+
fn from(order: Ordering) -> Self {
1929
match order {
20-
Ordering::Less => NSComparisonResult::Ascending,
21-
Ordering::Equal => NSComparisonResult::Same,
22-
Ordering::Greater => NSComparisonResult::Descending,
30+
Ordering::Less => Self::Ascending,
31+
Ordering::Equal => Self::Same,
32+
Ordering::Greater => Self::Descending,
2333
}
2434
}
35+
}
2536

26-
pub fn as_ordering(&self) -> Ordering {
27-
match *self {
28-
NSComparisonResult::Ascending => Ordering::Less,
29-
NSComparisonResult::Same => Ordering::Equal,
30-
NSComparisonResult::Descending => Ordering::Greater,
37+
impl From<NSComparisonResult> for Ordering {
38+
fn from(comparison_result: NSComparisonResult) -> Self {
39+
match comparison_result {
40+
NSComparisonResult::Ascending => Self::Less,
41+
NSComparisonResult::Same => Self::Equal,
42+
NSComparisonResult::Descending => Self::Greater,
3143
}
3244
}
3345
}

0 commit comments

Comments
 (0)