Skip to content

Commit 7826ca8

Browse files
committed
Improve NSRange
1 parent 5cc31bd commit 7826ca8

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

objc2_foundation/src/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub unsafe trait INSArray: INSObject {
107107
}
108108

109109
fn objects_in_range(&self, range: Range<usize>) -> Vec<&Self::Item> {
110-
let range = NSRange::from_range(range);
110+
let range = NSRange::from(range);
111111
let mut vec = Vec::with_capacity(range.length);
112112
unsafe {
113113
let _: () = msg_send![self, getObjects: vec.as_ptr(), range: range];

objc2_foundation/src/data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub unsafe trait INSMutableData: INSData {
128128
}
129129

130130
fn replace_range(&mut self, range: Range<usize>, bytes: &[u8]) {
131-
let range = NSRange::from_range(range);
131+
let range = NSRange::from(range);
132132
let bytes_ptr = bytes.as_ptr() as *const c_void;
133133
unsafe {
134134
let _: () = msg_send![

objc2_foundation/src/range.rs

+37-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
11
use core::ops::Range;
22

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

55
#[repr(C)]
6-
#[derive(Clone, Copy)]
6+
// PartialEq is same as NSEqualRanges
7+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
78
pub struct NSRange {
89
pub location: usize,
910
pub length: usize,
1011
}
1112

12-
impl NSRange {
13-
pub fn from_range(range: Range<usize>) -> NSRange {
14-
assert!(range.end >= range.start);
15-
NSRange {
13+
// impl NSRange {
14+
// pub fn contains(&self, index: usize) -> bool {
15+
// // Same as NSLocationInRange
16+
// <Self as RangeBounds<usize>>::contains(self, &index)
17+
// }
18+
// }
19+
20+
// impl RangeBounds<usize> for NSRange {
21+
// fn start_bound(&self) -> Bound<&usize> {
22+
// Bound::Included(&self.location)
23+
// }
24+
// fn end_bound(&self) -> Bound<&usize> {
25+
// Bound::Excluded(&(self.location + self.length))
26+
// }
27+
// }
28+
29+
impl From<Range<usize>> for NSRange {
30+
fn from(range: Range<usize>) -> Self {
31+
let length = range
32+
.end
33+
.checked_sub(range.start)
34+
.expect("Range end < start");
35+
Self {
1636
location: range.start,
17-
length: range.end - range.start,
37+
length,
1838
}
1939
}
40+
}
2041

21-
pub fn as_range(&self) -> Range<usize> {
22-
Range {
23-
start: self.location,
24-
end: self.location + self.length,
42+
impl From<NSRange> for Range<usize> {
43+
fn from(nsrange: NSRange) -> Self {
44+
Self {
45+
start: nsrange.location,
46+
end: nsrange.location + nsrange.length,
2547
}
2648
}
2749
}
@@ -30,3 +52,7 @@ unsafe impl Encode for NSRange {
3052
const ENCODING: Encoding<'static> =
3153
Encoding::Struct("_NSRange", &[usize::ENCODING, usize::ENCODING]);
3254
}
55+
56+
unsafe impl RefEncode for NSRange {
57+
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING);
58+
}

0 commit comments

Comments
 (0)