@@ -17,18 +17,18 @@ use super::{INSCopying, INSObject};
17
17
pub unsafe trait INSValue : INSObject {
18
18
type Value : ' static + Copy + Encode ;
19
19
20
+ // Default / empty new is not provided because `-init` returns `nil` on
21
+ // Apple and GNUStep throws an exception on all other messages to this
22
+ // invalid instance.
23
+
20
24
/// TODO.
21
- ///
22
- /// If the value was created using [`INSObject::new`], the value may not
23
- /// be initialized. In that case, this will return [`None`].
24
- fn get ( & self ) -> Option < Self :: Value > {
25
- // If encoding is `None`, this was created using INSObject::new
25
+ fn get ( & self ) -> Self :: Value {
26
26
if let Some ( encoding) = self . encoding ( ) {
27
27
// TODO: This can be a debug assertion (?)
28
- assert ! ( & Self :: Value :: ENCODING == encoding) ;
29
- Some ( unsafe { self . get_unchecked ( ) } )
28
+ assert ! ( & Self :: Value :: ENCODING == encoding, "Wrong encoding" ) ;
29
+ unsafe { self . get_unchecked ( ) }
30
30
} else {
31
- None
31
+ panic ! ( "Missing NSValue encoding" ) ;
32
32
}
33
33
}
34
34
@@ -44,7 +44,7 @@ pub unsafe trait INSValue: INSObject {
44
44
result. map ( |s| unsafe { CStr :: from_ptr ( s. as_ptr ( ) ) } . to_str ( ) . unwrap ( ) )
45
45
}
46
46
47
- fn from_value ( value : Self :: Value ) -> Id < Self , Self :: Ownership > {
47
+ fn new ( value : Self :: Value ) -> Id < Self , Self :: Ownership > {
48
48
let cls = Self :: class ( ) ;
49
49
let bytes = & value as * const Self :: Value as * const c_void ;
50
50
let encoding = CString :: new ( Self :: Value :: ENCODING . to_string ( ) ) . unwrap ( ) ;
@@ -70,31 +70,7 @@ unsafe impl<T: 'static> INSObject for NSValue<T> {
70
70
type Ownership = Shared ;
71
71
72
72
fn class ( ) -> & ' static Class {
73
- #[ cfg( not( gnustep) ) ]
74
- return class ! ( NSValue ) ;
75
-
76
- #[ cfg( gnustep) ]
77
- // We can't use NSValue directly, because its `new` method throws an
78
- // exception (instead of just becoming an invalid NSValue). Luckily,
79
- // the `GSValue` subclass has the desired behaviour, so we can just
80
- // use that. Unfortunately, this is less efficient for the following:
81
- // ```
82
- // match T::ENCODING {
83
- // Encoding::Object => ...,
84
- // Encoding::Struct("_NSPoint", _) => ...,
85
- // Encoding::Pointer(&Encoding::Void) => ...,
86
- // Encoding::Struct("_NSRange", _) => ...,
87
- // Encoding::Struct("_NSRect", _) => ...,
88
- // Encoding::Struct("_NSSize", _) => ...,
89
- // }
90
- // ```
91
- //
92
- // See GNUStep's `NSValue +valueClassWithObjCType` and
93
- // `GSConcreteValueTemplate.m` for more, though we can't use the
94
- // classes in there either, because their `new` methods return valid
95
- // objects (and so `<NSValue<NSRange>>::new()` would work differently
96
- // on GNUStep).
97
- return class ! ( GSValue ) ;
73
+ class ! ( NSValue )
98
74
}
99
75
}
100
76
@@ -108,31 +84,20 @@ unsafe impl<T: 'static> INSCopying for NSValue<T> {
108
84
109
85
#[ cfg( test) ]
110
86
mod tests {
111
- use crate :: { INSObject , INSValue , NSRange , NSValue } ;
87
+ use crate :: { INSValue , NSRange , NSValue } ;
112
88
use objc2:: Encode ;
113
89
114
90
#[ test]
115
91
fn test_value ( ) {
116
- let val = NSValue :: from_value ( 13u32 ) ;
117
- assert_eq ! ( val. get( ) . unwrap ( ) , 13 ) ;
92
+ let val = NSValue :: new ( 13u32 ) ;
93
+ assert_eq ! ( val. get( ) , 13 ) ;
118
94
assert ! ( & u32 :: ENCODING == val. encoding( ) . unwrap( ) ) ;
119
95
}
120
96
121
- #[ test]
122
- fn test_value_new ( ) {
123
- let val = <NSValue < u8 > >:: new ( ) ;
124
- assert ! ( val. encoding( ) . is_none( ) ) ;
125
- assert ! ( val. get( ) . is_none( ) ) ;
126
- }
127
-
128
97
#[ test]
129
98
fn test_value_nsrange ( ) {
130
- let val = NSValue :: from_value ( NSRange :: from ( 1 ..2 ) ) ;
99
+ let val = NSValue :: new ( NSRange :: from ( 1 ..2 ) ) ;
131
100
assert ! ( & NSRange :: ENCODING == val. encoding( ) . unwrap( ) ) ;
132
- assert_eq ! ( val. get( ) . unwrap( ) , NSRange :: from( 1 ..2 ) ) ;
133
-
134
- let val = <NSValue < NSRange > >:: new ( ) ;
135
- assert ! ( val. encoding( ) . is_none( ) ) ;
136
- assert ! ( val. get( ) . is_none( ) ) ;
101
+ assert_eq ! ( val. get( ) , NSRange :: from( 1 ..2 ) ) ;
137
102
}
138
103
}
0 commit comments