Skip to content

Commit 18b26ba

Browse files
committed
Convert internal objc_try! macro to a function
1 parent 1b62354 commit 18b26ba

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

objc2/src/message/apple/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use objc2_sys::objc_super;
22

3-
use super::{Encode, MessageArguments, MessageError};
3+
use super::{conditional_try, Encode, MessageArguments, MessageError};
44
use crate::runtime::{Class, Imp, Object, Sel};
55

66
#[cfg(target_arch = "x86")]
@@ -23,6 +23,7 @@ trait MsgSendFn: Encode {
2323
const MSG_SEND_SUPER: Imp;
2424
}
2525

26+
#[inline(always)]
2627
pub unsafe fn send_unverified<A, R>(
2728
receiver: *mut Object,
2829
sel: Sel,
@@ -33,9 +34,10 @@ where
3334
R: Encode,
3435
{
3536
let msg_send_fn = R::MSG_SEND;
36-
objc_try!({ A::invoke(msg_send_fn, receiver, sel, args) })
37+
conditional_try(|| A::invoke(msg_send_fn, receiver, sel, args))
3738
}
3839

40+
#[inline]
3941
pub unsafe fn send_super_unverified<A, R>(
4042
receiver: *mut Object,
4143
superclass: &Class,
@@ -52,5 +54,5 @@ where
5254
};
5355
let receiver = &sup as *const objc_super as *mut Object;
5456
let msg_send_fn = R::MSG_SEND_SUPER;
55-
objc_try!({ A::invoke(msg_send_fn, receiver, sel, args) })
57+
conditional_try(|| A::invoke(msg_send_fn, receiver, sel, args))
5658
}

objc2/src/message/gnustep.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::mem;
22
use objc2_sys::{objc_msg_lookup, objc_msg_lookup_super, objc_super};
33

4-
use super::{Encode, MessageArguments, MessageError};
4+
use super::{conditional_try, Encode, MessageArguments, MessageError};
55
use crate::runtime::{Class, Object, Sel};
66

77
pub unsafe fn send_unverified<A, R>(
@@ -18,7 +18,7 @@ where
1818
}
1919

2020
let msg_send_fn = objc_msg_lookup(receiver as *mut _, sel.as_ptr() as *const _);
21-
objc_try!({ A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args) })
21+
conditional_try(|| A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args))
2222
}
2323

2424
pub unsafe fn send_super_unverified<A, R>(
@@ -36,5 +36,5 @@ where
3636
super_class: superclass as *const Class as *const _,
3737
};
3838
let msg_send_fn = objc_msg_lookup_super(&sup, sel.as_ptr() as *const _);
39-
objc_try!({ A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args) })
39+
conditional_try(|| A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args))
4040
}

objc2/src/message/mod.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,21 @@ use crate::runtime::{Class, Imp, Object, Sel};
99
use crate::{Encode, EncodeArguments, RefEncode};
1010

1111
#[cfg(feature = "exception")]
12-
macro_rules! objc_try {
13-
($b:block) => {
14-
$crate::exception::catch_exception(|| $b).map_err(|exception| {
15-
use alloc::borrow::ToOwned;
16-
if let Some(exception) = exception {
17-
MessageError(alloc::format!("Uncaught exception {:?}", exception))
18-
} else {
19-
MessageError("Uncaught exception nil".to_owned())
20-
}
21-
})
22-
};
12+
unsafe fn conditional_try<R: Encode>(f: impl FnOnce() -> R) -> Result<R, MessageError> {
13+
use alloc::borrow::ToOwned;
14+
crate::exception::catch_exception(f).map_err(|exception| {
15+
if let Some(exception) = exception {
16+
MessageError(alloc::format!("Uncaught exception {:?}", exception))
17+
} else {
18+
MessageError("Uncaught exception nil".to_owned())
19+
}
20+
})
2321
}
2422

2523
#[cfg(not(feature = "exception"))]
26-
macro_rules! objc_try {
27-
($b:block) => {
28-
Ok($b)
29-
};
24+
#[inline(always)]
25+
unsafe fn conditional_try<R: Encode>(f: impl FnOnce() -> R) -> Result<R, MessageError> {
26+
Ok(f())
3027
}
3128

3229
mod verify;

0 commit comments

Comments
 (0)