Skip to content

Commit 3fe939e

Browse files
committed
Add an example of Bool usage
1 parent 1ffa8ee commit 3fe939e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

objc2/src/bool.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ use core::fmt;
33

44
/// The Objective-C `BOOL` type.
55
///
6-
/// To convert an Objective-C `BOOL` into a Rust [`bool`], call the one of the
6+
/// Usually, you would convert this into a Rust [`bool`] with the
77
/// [`Bool::is_false`] or [`Bool::is_true`] methods.
88
///
99
/// This is FFI-safe and can be used in directly with
1010
/// [`msg_send!`][`crate::msg_send`].
11+
///
12+
/// # Example
13+
///
14+
/// ```no_run
15+
/// use objc2::{class, msg_send};
16+
/// use objc2::runtime::{Object, Bool};
17+
/// let ns_value: *mut Object = unsafe { msg_send![class!(NSValue), initWithBool: Bool::YES] };
18+
/// let rtn: Bool = unsafe { msg_send![ns_value, boolValue] };
19+
/// assert!(rtn.is_true());
20+
/// ```
1121
#[repr(transparent)]
1222
// TODO: Might have to implement some of these manually, in case someone puts
1323
// something that is not 0 or 1 into the Bool?

objc2/src/runtime.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl Class {
293293

294294
/// Checks whether this class conforms to the specified protocol.
295295
pub fn conforms_to(&self, proto: &Protocol) -> bool {
296-
unsafe { Bool::from_raw(class_conformsToProtocol(self.as_ptr(), proto.as_ptr())).into() }
296+
unsafe { Bool::from_raw(class_conformsToProtocol(self.as_ptr(), proto.as_ptr())).is_true() }
297297
}
298298

299299
/// Get a list of the protocols to which this class conforms.
@@ -368,7 +368,9 @@ impl Protocol {
368368

369369
/// Checks whether this protocol conforms to the specified protocol.
370370
pub fn conforms_to(&self, proto: &Protocol) -> bool {
371-
unsafe { Bool::from_raw(protocol_conformsToProtocol(self.as_ptr(), proto.as_ptr())).into() }
371+
unsafe {
372+
Bool::from_raw(protocol_conformsToProtocol(self.as_ptr(), proto.as_ptr())).is_true()
373+
}
372374
}
373375

374376
/// Returns the name of self.

0 commit comments

Comments
 (0)