File tree Expand file tree Collapse file tree 6 files changed +23
-7
lines changed Expand file tree Collapse file tree 6 files changed +23
-7
lines changed Original file line number Diff line number Diff line change @@ -14,13 +14,13 @@ Objective-C objects can be messaged using the `msg_send!` macro:
14
14
15
15
``` rust , no_run
16
16
use objc2 :: {class, msg_send};
17
- use objc2 :: runtime :: {BOOL , Object };
17
+ use objc2 :: runtime :: {Bool , Object };
18
18
19
19
let cls = class! (NSObject );
20
20
unsafe {
21
21
let obj : * mut Object = msg_send! [cls , new ];
22
22
let hash : usize = msg_send! [obj , hash ];
23
- let is_kind : BOOL = msg_send! [obj , isKindOfClass: cls ];
23
+ let is_kind : Bool = msg_send! [obj , isKindOfClass: cls ];
24
24
// Even void methods must have their return type annotated
25
25
let _ : () = msg_send! [obj , release ];
26
26
}
Original file line number Diff line number Diff line change @@ -99,10 +99,21 @@ impl fmt::Debug for Bool {
99
99
}
100
100
}
101
101
102
+ // SAFETY: `Bool` is `repr(transparent)`.
102
103
unsafe impl Encode for Bool {
103
104
const ENCODING : Encoding < ' static > = objc2_sys:: BOOL :: ENCODING ;
104
105
}
105
106
107
+ // Note that we shouldn't delegate to `BOOL`'s `ENCODING_REF` since `BOOL` is
108
+ // sometimes `i8`/`u8`, and their `ENCODING_REF`s are `Encoding::String`,
109
+ // which is incorrect for `BOOL`:
110
+ //
111
+ // ```objc
112
+ // @encode(BOOL); // -> "c", "C" or "B"
113
+ // @encode(BOOL*); // -> "^c", "^C" or "^B"
114
+ // @encode(char); // -> "c" or "C"
115
+ // @encode(char*); // -> "*"
116
+ // ```
106
117
unsafe impl RefEncode for Bool {
107
118
const ENCODING_REF : Encoding < ' static > = Encoding :: Pointer ( & Self :: ENCODING ) ;
108
119
}
Original file line number Diff line number Diff line change @@ -7,12 +7,12 @@ Objective-C objects can be messaged using the [`msg_send!`](macro.msg_send!.html
7
7
8
8
``` no_run
9
9
# use objc2::{class, msg_send};
10
- # use objc2::runtime::{BOOL , Class, Object};
10
+ # use objc2::runtime::{Bool , Class, Object};
11
11
# unsafe {
12
12
let cls = class!(NSObject);
13
13
let obj: *mut Object = msg_send![cls, new];
14
14
let hash: usize = msg_send![obj, hash];
15
- let is_kind: BOOL = msg_send![obj, isKindOfClass: cls];
15
+ let is_kind: Bool = msg_send![obj, isKindOfClass: cls];
16
16
// Even void methods must have their return type annotated
17
17
let _: () = msg_send![obj, release];
18
18
# }
Original file line number Diff line number Diff line change @@ -165,13 +165,13 @@ pub unsafe trait MessageReceiver: private::Sealed {
165
165
/// # Example
166
166
/// ``` no_run
167
167
/// # use objc2::{class, msg_send, sel};
168
- /// # use objc2::runtime::{BOOL , Class, Object};
168
+ /// # use objc2::runtime::{Bool , Class, Object};
169
169
/// # use objc2::MessageReceiver;
170
170
/// let obj: &Object;
171
171
/// # obj = unsafe { msg_send![class!(NSObject), new] };
172
172
/// let sel = sel!(isKindOfClass:);
173
173
/// // Verify isKindOfClass: takes one Class and returns a BOOL
174
- /// let result = obj.verify_message::<(&Class,), BOOL >(sel);
174
+ /// let result = obj.verify_message::<(&Class,), Bool >(sel);
175
175
/// assert!(result.is_ok());
176
176
/// ```
177
177
fn verify_message < A , R > ( & self , sel : Sel ) -> Result < ( ) , MessageError >
Original file line number Diff line number Diff line change @@ -182,7 +182,7 @@ unsafe impl Encode for () {
182
182
/// Using this directly is heavily discouraged, since the type of BOOL differs
183
183
/// across platforms.
184
184
///
185
- /// Use `objc2_sys::BOOL::ENCODING` or ` objc2::runtime::Bool` instead.
185
+ /// Use `objc2::runtime::Bool::ENCODING ` instead.
186
186
unsafe impl Encode for bool {
187
187
const ENCODING : Encoding < ' static > = Encoding :: Bool ;
188
188
}
Original file line number Diff line number Diff line change @@ -65,6 +65,11 @@ mod inner {
65
65
///
66
66
/// The type of this varies across platforms, so to convert an it into a Rust
67
67
/// [`bool`], always compare it with [`YES`][`crate::YES`] or [`NO`][`crate::NO`].
68
+ ///
69
+ /// Note that this type implements `objc2_encode::Encode` and
70
+ /// `objc2_encode::RefEncode`, but the `RefEncode` implementation is wrong
71
+ /// on some platforms! You should only use this on FFI boundaries, otherwise
72
+ /// prefer `objc2::runtime::Bool`.
68
73
pub type BOOL = inner:: BOOL ;
69
74
70
75
/// An immutable pointer to a selector.
You can’t perform that action at this time.
0 commit comments