Skip to content

Commit

Permalink
Make objc macros compatible with use macro! (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbool authored and SSheldon committed Apr 7, 2019
1 parent fa7ca43 commit b7dfa8f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ let sel = sel!(setObject:forKey:);
*/
#[macro_export]
macro_rules! sel {
($name:ident) => ({sel_impl!(concat!(stringify!($name), '\0'))});
($($name:ident :)+) => ({sel_impl!(concat!($(stringify!($name), ':'),+, '\0'))});
($name:ident) => ({$crate::sel_impl!(concat!(stringify!($name), '\0'))});
($($name:ident :)+) => ({$crate::sel_impl!(concat!($(stringify!($name), ':'),+, '\0'))});
}

/**
Expand Down Expand Up @@ -108,28 +108,28 @@ let _: () = msg_send![obj, setArg1:1 arg2:2];
#[macro_export]
macro_rules! msg_send {
(super($obj:expr, $superclass:expr), $name:ident) => ({
let sel = sel!($name);
let sel = $crate::sel!($name);
match $crate::__send_super_message(&*$obj, $superclass, sel, ()) {
Err(s) => panic!("{}", s),
Ok(r) => r,
}
});
(super($obj:expr, $superclass:expr), $($name:ident : $arg:expr)+) => ({
let sel = sel!($($name:)+);
let sel = $crate::sel!($($name:)+);
match $crate::__send_super_message(&*$obj, $superclass, sel, ($($arg,)*)) {
Err(s) => panic!("{}", s),
Ok(r) => r,
}
});
($obj:expr, $name:ident) => ({
let sel = sel!($name);
let sel = $crate::sel!($name);
match $crate::__send_message(&*$obj, sel, ()) {
Err(s) => panic!("{}", s),
Ok(r) => r,
}
});
($obj:expr, $($name:ident : $arg:expr)+) => ({
let sel = sel!($($name:)+);
let sel = $crate::sel!($($name:)+);
match $crate::__send_message(&*$obj, sel, ($($arg,)*)) {
Err(s) => panic!("{}", s),
Ok(r) => r,
Expand Down
23 changes: 23 additions & 0 deletions tests/use_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![cfg(any(target_os = "macos", target_os = "ios"))]

extern crate objc;

use objc::{class, msg_send, sel};
use objc::runtime::Object;

#[test]
fn use_class_and_msg_send() {
unsafe {
let cls = class!(NSObject);
let obj: *mut Object = msg_send![cls, new];
let _hash: usize = msg_send![obj, hash];
let _: () = msg_send![obj, release];
}
}

#[test]
fn use_sel() {
let _sel = sel!(description);
let _sel = sel!(setObject:forKey:);
}

0 comments on commit b7dfa8f

Please sign in to comment.