Skip to content

Commit

Permalink
Upgrade objc2
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 29, 2022
1 parent 87fe0e3 commit 3fdc19f
Show file tree
Hide file tree
Showing 47 changed files with 86 additions and 105 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ default-target = "x86_64-apple-darwin"
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
objc = { version = "=0.3.0-beta.1", package = "objc2" }
block = { version = "=0.2.0-alpha.5", package = "block2" }
objc = { version = "=0.3.0-beta.2", package = "objc2" }
block = { version = "=0.2.0-alpha.6", package = "block2" }
# Temporary: Patched versions that implement `Encode` for common types
# Branch: `objc2`
core-foundation = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "c506e7d70da010c0070048e9471ff0b441506e65" }
core-graphics = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "c506e7d70da010c0070048e9471ff0b441506e65" }
core-foundation = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "036c3de1c9fa3212bd7123141df4dce11b340a28" }
core-graphics = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "036c3de1c9fa3212bd7123141df4dce11b340a28" }
dispatch = "0.2.0"
infer = { version = "0.9", optional = true }
lazy_static = "1.4.0"
Expand Down
2 changes: 1 addition & 1 deletion src/appkit/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Alert {
let ok = NSString::new("OK");

Alert(unsafe {
let mut alert = msg_send_id![class!(NSAlert), new].unwrap();
let mut alert = msg_send_id![class!(NSAlert), new];
let _: () = msg_send![&mut alert, setMessageText: &*title];
let _: () = msg_send![&mut alert, setInformativeText: &*message];
let _: () = msg_send![&mut alert, addButtonWithTitle: &*ok];
Expand Down
4 changes: 2 additions & 2 deletions src/appkit/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ where

let pool = AutoReleasePool::new();

let objc = unsafe { msg_send_id![register_app_class(), sharedApplication].unwrap() };
let objc: Id<_, _> = unsafe { msg_send_id![register_app_class(), sharedApplication] };

let app_delegate = Box::new(delegate);

let objc_delegate = unsafe {
let delegate_class = register_app_delegate_class::<T>();
let mut delegate: Id<Object, Owned> = msg_send_id![delegate_class, new].unwrap();
let mut delegate: Id<Object, Owned> = msg_send_id![delegate_class, new];
let delegate_ptr: *const T = &*app_delegate;
delegate.set_ivar(APP_PTR, delegate_ptr as usize);
let _: () = msg_send![&*objc, setDelegate: &*delegate];
Expand Down
1 change: 0 additions & 1 deletion src/appkit/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ impl Event {
addLocalMonitorForEventsMatchingMask: 1024,
handler: &*block,
]
.unwrap()
})
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/appkit/menu/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn make_menu_item<S: AsRef<str>>(
// `NSMenuItem`s. If there's no custom ones, we use our subclass that has a slot to store a
// handler pointer.
let alloc = msg_send_id![register_menu_item_class(), alloc];
let item = match action {
let item: Id<_, _> = match action {
Some(a) => msg_send_id![
alloc,
initWithTitle: &*title,
Expand All @@ -67,8 +67,7 @@ fn make_menu_item<S: AsRef<str>>(
action: sel!(fireBlockAction:),
keyEquivalent: &*key,
]
}
.unwrap();
};

if let Some(modifiers) = modifiers {
let mut key_mask: NSUInteger = 0;
Expand Down Expand Up @@ -218,7 +217,7 @@ impl MenuItem {

Self::Separator => {
let cls = class!(NSMenuItem);
msg_send_id![cls, separatorItem].unwrap()
msg_send_id![cls, separatorItem]
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/appkit/menu/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Menu {
let cls = class!(NSMenu);
let alloc = msg_send_id![cls, alloc];
let title = NSString::new(title);
let mut menu = msg_send_id![alloc, initWithTitle: &*title].unwrap();
let mut menu = msg_send_id![alloc, initWithTitle: &*title];

for item in items.into_iter() {
let objc = item.to_objc();
Expand Down
2 changes: 1 addition & 1 deletion src/appkit/toolbar/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ToolbarItem {
let objc = unsafe {
let identifer = NSString::new(&identifier);
let alloc = msg_send_id![class!(NSToolbarItem), alloc];
msg_send_id![alloc, initWithItemIdentifier: &*identifer].unwrap()
msg_send_id![alloc, initWithItemIdentifier: &*identifer]
};

ToolbarItem {
Expand Down
8 changes: 4 additions & 4 deletions src/appkit/toolbar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ where
let (objc, objc_delegate) = unsafe {
let alloc = msg_send_id![class!(NSToolbar), alloc];
let identifier = NSString::new(&identifier);
let mut toolbar: Id<Object, Owned> = msg_send_id![alloc, initWithIdentifier: &*identifier].unwrap();
let mut objc_delegate: Id<Object, Owned> = msg_send_id![cls, new].unwrap(); //WithIdentifier:identifier];
let mut toolbar: Id<Object, Owned> = msg_send_id![alloc, initWithIdentifier: &*identifier];
let mut objc_delegate: Id<Object, Owned> = msg_send_id![cls, new]; //WithIdentifier:identifier];

let ptr: *const T = &*delegate;
objc_delegate.set_ivar(TOOLBAR_PTR, ptr as usize);

let objc_delegate: Id<Object, Shared> = Id::from_owned(objc_delegate);
let objc_delegate: Id<Object, Shared> = Id::into_shared(objc_delegate);
let _: () = msg_send![&mut toolbar, setDelegate: &*objc_delegate];

(Id::from_owned(toolbar), objc_delegate)
(Id::into_shared(toolbar), objc_delegate)
};

let _ret = &mut delegate.did_load(Toolbar {
Expand Down
2 changes: 1 addition & 1 deletion src/appkit/window/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ where
let objc = unsafe {
let window_controller_class = register_window_controller_class::<T>();
let controller_alloc = msg_send_id![window_controller_class, alloc];
let mut controller: Id<Object, Owned> = msg_send_id![controller_alloc, initWithWindow: &*window.objc].unwrap();
let mut controller: Id<Object, Owned> = msg_send_id![controller_alloc, initWithWindow: &*window.objc];

if let Some(delegate) = &window.delegate {
let ptr: *const T = &**delegate;
Expand Down
8 changes: 3 additions & 5 deletions src/appkit/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Window {
// NeXTSTEP era, and are outright deprecated... so we don't allow setting them.
let buffered: NSUInteger = 2;
let dimensions: CGRect = config.initial_dimensions.into();
let window = msg_send_id![
let window: Id<_, _> = msg_send_id![
msg_send_id![class!(NSWindow), alloc],
initWithContentRect: dimensions,
styleMask: config.style,
Expand All @@ -84,8 +84,7 @@ impl Window {
true => YES,
false => NO
},
]
.unwrap();
];

let _: () = msg_send![&*window, autorelease];

Expand Down Expand Up @@ -145,8 +144,7 @@ where
true => YES,
false => NO
},
]
.unwrap();
];

let delegate_ptr: *const T = &*delegate;
window.set_ivar(WINDOW_DELEGATE_PTR, delegate_ptr as usize);
Expand Down
2 changes: 1 addition & 1 deletion src/button/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl Button {
/// best just to message pass or something.
pub fn set_action<F: Fn() + Send + Sync + 'static>(&mut self, action: F) {
// @TODO: This probably isn't ideal but gets the job done for now; needs revisiting.
let this: Id<Object, Shared> = self.objc.get(|obj| unsafe { msg_send_id![obj, self].unwrap() });
let this: Id<Object, Shared> = self.objc.get(|obj| unsafe { msg_send_id![obj, self] });
let handler = TargetActionHandler::new(&*this, action);
self.handler = Some(handler);
}
Expand Down
13 changes: 6 additions & 7 deletions src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ impl Color {
let b = blue as CGFloat / 255.0;
let a = alpha as CGFloat / 255.0;
#[cfg(feature = "appkit")]
let ptr = unsafe { msg_send_id![class!(NSColor), colorWithCalibratedRed: r, green: g, blue: b, alpha: a].unwrap() };
let ptr = unsafe { msg_send_id![class!(NSColor), colorWithCalibratedRed: r, green: g, blue: b, alpha: a] };
#[cfg(all(feature = "uikit", not(feature = "appkit")))]
let ptr = unsafe { msg_send_id![class!(UIColor), colorWithRed: r, green: g, blue: b, alpha: a].unwrap() };
let ptr = unsafe { msg_send_id![class!(UIColor), colorWithRed: r, green: g, blue: b, alpha: a] };

Color::Custom(Arc::new(RwLock::new(ptr)))
}
Expand Down Expand Up @@ -279,12 +279,11 @@ impl Color {
brightness: b,
alpha: a
]
.unwrap()
}

#[cfg(all(feature = "uikit", not(feature = "appkit")))]
{
msg_send_id![class!(UIColor), colorWithHue: h, saturation: s, brightness: b, alpha: a].unwrap()
msg_send_id![class!(UIColor), colorWithHue: h, saturation: s, brightness: b, alpha: a]
}
})))
}
Expand All @@ -301,12 +300,12 @@ impl Color {
Color::Custom(Arc::new(RwLock::new(unsafe {
#[cfg(feature = "appkit")]
{
msg_send_id![class!(NSColor), colorWithCalibratedWhite: level, alpha: alpha].unwrap()
msg_send_id![class!(NSColor), colorWithCalibratedWhite: level, alpha: alpha]
}

#[cfg(all(feature = "uikit", not(feature = "appkit")))]
{
msg_send_id![class!(UIColor), colorWithWhite: level, alpha: alpha].unwrap()
msg_send_id![class!(UIColor), colorWithWhite: level, alpha: alpha]
}
})))
}
Expand Down Expand Up @@ -353,7 +352,7 @@ impl Color {
// am happy to do this for now and let someone who needs true dynamic allocation look into
// it and PR it.
Color::Custom(Arc::new(RwLock::new(unsafe {
let mut color: Id<Object, Owned> = msg_send_id![appkit_dynamic_color::register_class(), new].unwrap();
let mut color: Id<Object, Owned> = msg_send_id![appkit_dynamic_color::register_class(), new];

color.set_ivar(AQUA_LIGHT_COLOR_NORMAL_CONTRAST, {
let color: id = handler(Style {
Expand Down
4 changes: 2 additions & 2 deletions src/defaults/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl UserDefaults {
/// let _ = defaults.get("test");
/// ```
pub fn standard() -> Self {
UserDefaults(unsafe { msg_send_id![class!(NSUserDefaults), standardUserDefaults].unwrap() })
UserDefaults(unsafe { msg_send_id![class!(NSUserDefaults), standardUserDefaults] })
}

/// Returns a user defaults instance for the given suite name. You typically use this to share
Expand All @@ -90,7 +90,7 @@ impl UserDefaults {

UserDefaults(unsafe {
let alloc = msg_send_id![class!(NSUserDefaults), alloc];
msg_send_id![alloc, initWithSuiteName:&*name].unwrap()
msg_send_id![alloc, initWithSuiteName:&*name]
})
}

Expand Down
6 changes: 2 additions & 4 deletions src/filesystem/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ impl Default for FileManager {
/// and simple tasks, with no callbacks, you might want this.
fn default() -> Self {
FileManager(Arc::new(RwLock::new(unsafe {
msg_send_id![class!(NSFileManager), defaultManager].unwrap()
msg_send_id![class!(NSFileManager), defaultManager]
})))
}
}

impl FileManager {
/// Returns a new FileManager that opts in to delegate methods.
pub fn new() -> Self {
FileManager(Arc::new(RwLock::new(unsafe {
msg_send_id![class!(NSFileManager), new].unwrap()
})))
FileManager(Arc::new(RwLock::new(unsafe { msg_send_id![class!(NSFileManager), new] })))
}

/// Given a directory/domain combination, will attempt to get the directory that matches.
Expand Down
4 changes: 2 additions & 2 deletions src/filesystem/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl FileSavePanel {
FileSavePanel {
panel: unsafe {
let cls = class!(NSSavePanel);
msg_send_id![cls, savePanel].unwrap()
msg_send_id![cls, savePanel]
},

delegate: unsafe { msg_send_id![class!(NSObject), new].unwrap() },
delegate: unsafe { msg_send_id![class!(NSObject), new] },

can_create_directories: true
}
Expand Down
4 changes: 2 additions & 2 deletions src/filesystem/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ impl FileSelectPanel {
FileSelectPanel {
panel: unsafe {
let cls = class!(NSOpenPanel);
msg_send_id![cls, openPanel].unwrap()
msg_send_id![cls, openPanel]
},

delegate: unsafe { msg_send_id![class!(NSObject), new].unwrap() },
delegate: unsafe { msg_send_id![class!(NSObject), new] },

can_choose_files: true,
can_choose_directories: false,
Expand Down
3 changes: 0 additions & 3 deletions src/foundation/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ impl NSArray {
arrayWithObjects: objects.as_ptr(),
count: objects.len()
]
.unwrap()
})
}

Expand Down Expand Up @@ -70,7 +69,6 @@ impl From<Vec<&Object>> for NSArray {
arrayWithObjects: objects.as_ptr(),
count: objects.len(),
]
.unwrap()
})
}
}
Expand All @@ -84,7 +82,6 @@ impl From<Vec<id>> for NSArray {
arrayWithObjects: objects.as_ptr(),
count: objects.len()
]
.unwrap()
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/foundation/autoreleasepool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl AutoReleasePool {
/// Creates and returns a new `AutoReleasePool`. You need to take care to keep this alive for
/// as long as you need it.
pub fn new() -> Self {
AutoReleasePool(unsafe { msg_send_id![class!(NSAutoreleasePool), new].unwrap() })
AutoReleasePool(unsafe { msg_send_id![class!(NSAutoreleasePool), new] })
}

/// Drains the underlying AutoreleasePool.
Expand Down
4 changes: 2 additions & 2 deletions src/foundation/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl NSData {
deallocator: dealloc,
];
mem::forget(bytes);
NSData(obj.unwrap())
NSData(obj)
}
}

Expand All @@ -68,7 +68,7 @@ impl NSData {
dataWithBytes: bytes_ptr,
length: bytes.len(),
];
NSData(obj.unwrap())
NSData(obj)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/foundation/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl NSMutableDictionary {
/// object model. You can, of course, bypass it and `msg_send![]` yourself, but it'd require an
/// `unsafe {}` block... so you'll know you're in special territory then.
pub fn new() -> Self {
NSMutableDictionary(unsafe { msg_send_id![class!(NSMutableDictionary), new].unwrap() })
NSMutableDictionary(unsafe { msg_send_id![class!(NSMutableDictionary), new] })
}

/// Inserts an object into the backing NSMutablyDictionary.
Expand Down
5 changes: 2 additions & 3 deletions src/foundation/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ impl NSNumber {
true => YES,
false => NO
}]
.unwrap()
})
}

/// Constructs a `numberWithInteger` instance of `NSNumber` and retains it.
pub fn integer(value: i64) -> Self {
NSNumber(unsafe { msg_send_id![class!(NSNumber), numberWithInteger: value as NSInteger].unwrap() })
NSNumber(unsafe { msg_send_id![class!(NSNumber), numberWithInteger: value as NSInteger] })
}

/// Constructs a `numberWithDouble` instance of `NSNumber` and retains it.
pub fn float(value: f64) -> Self {
NSNumber(unsafe { msg_send_id![class!(NSNumber), numberWithDouble: value].unwrap() })
NSNumber(unsafe { msg_send_id![class!(NSNumber), numberWithDouble: value] })
}

/// Returns the `objCType` of the underlying `NSNumber` as a Rust `&str`. This flag can be used
Expand Down
2 changes: 0 additions & 2 deletions src/foundation/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ impl<'a> NSString<'a> {
length: s.len(),
encoding: UTF8_ENCODING,
]
.unwrap()
},

phantom: PhantomData
Expand All @@ -52,7 +51,6 @@ impl<'a> NSString<'a> {
encoding: UTF8_ENCODING,
freeWhenDone: NO,
]
.unwrap()
},

phantom: PhantomData
Expand Down
2 changes: 1 addition & 1 deletion src/foundation/urls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'a> NSURL<'a> {
let url = NSString::new(url);

Self {
objc: unsafe { msg_send_id![class!(NSURL), URLWithString:&*url].unwrap() },
objc: unsafe { msg_send_id![class!(NSURL), URLWithString:&*url] },

phantom: PhantomData
}
Expand Down
Loading

0 comments on commit 3fdc19f

Please # to comment.