1
1
use core:: ops:: Range ;
2
2
3
- use objc2:: { Encode , Encoding } ;
3
+ use objc2:: { Encode , Encoding , RefEncode } ;
4
4
5
5
#[ repr( C ) ]
6
- #[ derive( Clone , Copy ) ]
6
+ // PartialEq is same as NSEqualRanges
7
+ #[ derive( Clone , Copy , Debug , Default , PartialEq , Eq , Hash ) ]
7
8
pub struct NSRange {
8
9
pub location : usize ,
9
10
pub length : usize ,
10
11
}
11
12
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 {
16
36
location : range. start ,
17
- length : range . end - range . start ,
37
+ length,
18
38
}
19
39
}
40
+ }
20
41
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 ,
25
47
}
26
48
}
27
49
}
@@ -30,3 +52,7 @@ unsafe impl Encode for NSRange {
30
52
const ENCODING : Encoding < ' static > =
31
53
Encoding :: Struct ( "_NSRange" , & [ usize:: ENCODING , usize:: ENCODING ] ) ;
32
54
}
55
+
56
+ unsafe impl RefEncode for NSRange {
57
+ const ENCODING_REF : Encoding < ' static > = Encoding :: Pointer ( & Self :: ENCODING ) ;
58
+ }
0 commit comments