Skip to content

Commit

Permalink
use BOOL instead of *mut Object for boolean ivars. fixes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
schell authored and dfrankland committed Mar 11, 2023
1 parent aa886ad commit ffba9e2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
10 changes: 3 additions & 7 deletions src/peripheral/corebluetooth/events.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use objc::{
msg_send,
runtime::{Object, Sel, NO, YES},
sel, sel_impl,
};
use objc::{msg_send, runtime::{BOOL, NO, Object, Sel, YES}, sel, sel_impl};
use objc_foundation::{INSArray, INSString, NSArray, NSObject, NSString};

use super::{
Expand Down Expand Up @@ -37,11 +33,11 @@ pub extern "C" fn peripheral_manager_did_update_state(
}
CBManagerState::CBManagerStatePoweredOff => {
println!("CBManagerStatePoweredOff");
delegate.set_ivar::<*mut Object>(POWERED_ON_IVAR, NO as *mut Object);
delegate.set_ivar::<BOOL>(POWERED_ON_IVAR, NO);
}
CBManagerState::CBManagerStatePoweredOn => {
println!("CBManagerStatePoweredOn");
delegate.set_ivar::<*mut Object>(POWERED_ON_IVAR, YES as *mut Object);
delegate.set_ivar(POWERED_ON_IVAR, YES);
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/peripheral/corebluetooth/into_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ impl IntoBool for BOOL {

impl IntoBool for *mut Object {
fn into_bool(self) -> bool {
(self as BOOL).into_bool()
let nil = 0 as *mut Object;
nil != self
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/peripheral/corebluetooth/peripheral_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ use std::{
sync::{Once, ONCE_INIT},
};

use objc::{
class,
declare::ClassDecl,
msg_send,
runtime::{Class, Object, Protocol, Sel, NO, YES},
sel, sel_impl,
};
use objc::{class, declare::ClassDecl, msg_send, runtime::{BOOL, Class, NO, Object, Protocol, Sel, YES}, sel, sel_impl};
use objc_foundation::{
INSArray, INSData, INSDictionary, INSString, NSArray, NSData, NSDictionary, NSObject, NSString,
};
Expand Down Expand Up @@ -50,7 +44,7 @@ impl PeripheralManager {
decl.add_protocol(Protocol::get("CBPeripheralManagerDelegate").unwrap());

decl.add_ivar::<*mut Object>(PERIPHERAL_MANAGER_IVAR);
decl.add_ivar::<*mut Object>(POWERED_ON_IVAR);
decl.add_ivar::<BOOL>(POWERED_ON_IVAR);

unsafe {
decl.add_method(
Expand Down Expand Up @@ -103,7 +97,7 @@ impl PeripheralManager {
unsafe {
let powered_on = *self
.peripheral_manager_delegate
.get_ivar::<*mut Object>(POWERED_ON_IVAR);
.get_ivar::<BOOL>(POWERED_ON_IVAR);
powered_on.into_bool()
}
}
Expand Down Expand Up @@ -227,7 +221,7 @@ extern "C" fn init(delegate: &mut Object, _cmd: Sel) -> *mut Object {
queue:queue];
delegate.set_ivar::<*mut Object>(PERIPHERAL_MANAGER_IVAR, obj);

delegate.set_ivar::<*mut Object>(POWERED_ON_IVAR, NO as *mut Object);
delegate.set_ivar::<BOOL>(POWERED_ON_IVAR, NO);

delegate
}
Expand Down

0 comments on commit ffba9e2

Please sign in to comment.