Skip to content

Commit

Permalink
Improve NSComparisonResult
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Oct 31, 2021
1 parent 7826ca8 commit 063d612
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion objc2_foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub unsafe trait INSMutableArray: INSArray {
// ownership, and that method only runs one function at a time.
let closure: &mut F = unsafe { &mut *(context as *mut F) };

NSComparisonResult::from_ordering((*closure)(obj1, obj2))
NSComparisonResult::from((*closure)(obj1, obj2))
}

let f: extern "C" fn(_, _, _) -> _ = compare_with_closure::<Self::Item, F>;
Expand Down
36 changes: 24 additions & 12 deletions objc2_foundation/src/comparison_result.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
use core::cmp::Ordering;

use objc2::{Encode, Encoding};
use objc2::{Encode, Encoding, RefEncode};

#[repr(isize)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum NSComparisonResult {
Ascending = -1,
Same = 0,
Descending = 1,
}

impl Default for NSComparisonResult {
fn default() -> Self {
Self::Same
}
}

unsafe impl Encode for NSComparisonResult {
const ENCODING: Encoding<'static> = isize::ENCODING;
}

impl NSComparisonResult {
pub fn from_ordering(order: Ordering) -> NSComparisonResult {
unsafe impl RefEncode for NSComparisonResult {
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING);
}

impl From<Ordering> for NSComparisonResult {
fn from(order: Ordering) -> Self {
match order {
Ordering::Less => NSComparisonResult::Ascending,
Ordering::Equal => NSComparisonResult::Same,
Ordering::Greater => NSComparisonResult::Descending,
Ordering::Less => Self::Ascending,
Ordering::Equal => Self::Same,
Ordering::Greater => Self::Descending,
}
}
}

pub fn as_ordering(&self) -> Ordering {
match *self {
NSComparisonResult::Ascending => Ordering::Less,
NSComparisonResult::Same => Ordering::Equal,
NSComparisonResult::Descending => Ordering::Greater,
impl From<NSComparisonResult> for Ordering {
fn from(comparison_result: NSComparisonResult) -> Self {
match comparison_result {
NSComparisonResult::Ascending => Self::Less,
NSComparisonResult::Same => Self::Equal,
NSComparisonResult::Descending => Self::Greater,
}
}
}

0 comments on commit 063d612

Please sign in to comment.