From 4e9cd309e0432b8ef5f379019f02972e14160646 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 3 Oct 2021 22:36:02 +0200 Subject: [PATCH] Convert internal objc_try! macro to a function --- objc2/src/message/apple/mod.rs | 6 +++--- objc2/src/message/gnustep.rs | 6 +++--- objc2/src/message/mod.rs | 27 ++++++++++++--------------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/objc2/src/message/apple/mod.rs b/objc2/src/message/apple/mod.rs index d8bed3946..399815354 100644 --- a/objc2/src/message/apple/mod.rs +++ b/objc2/src/message/apple/mod.rs @@ -1,6 +1,6 @@ use objc2_sys::objc_super; -use super::{Encode, MessageArguments, MessageError}; +use super::{conditional_try, Encode, MessageArguments, MessageError}; use crate::runtime::{Class, Imp, Object, Sel}; #[cfg(target_arch = "x86")] @@ -33,7 +33,7 @@ where R: Encode, { let msg_send_fn = R::MSG_SEND; - objc_try!({ A::invoke(msg_send_fn, receiver, sel, args) }) + conditional_try(|| A::invoke(msg_send_fn, receiver, sel, args)) } pub unsafe fn send_super_unverified( @@ -52,5 +52,5 @@ where }; let receiver = &sup as *const objc_super as *mut Object; let msg_send_fn = R::MSG_SEND_SUPER; - objc_try!({ A::invoke(msg_send_fn, receiver, sel, args) }) + conditional_try(|| A::invoke(msg_send_fn, receiver, sel, args)) } diff --git a/objc2/src/message/gnustep.rs b/objc2/src/message/gnustep.rs index ab50fd96a..0f8168107 100644 --- a/objc2/src/message/gnustep.rs +++ b/objc2/src/message/gnustep.rs @@ -1,7 +1,7 @@ use core::mem; use objc2_sys::{objc_msg_lookup, objc_msg_lookup_super, objc_super}; -use super::{Encode, MessageArguments, MessageError}; +use super::{conditional_try, Encode, MessageArguments, MessageError}; use crate::runtime::{Class, Object, Sel}; pub unsafe fn send_unverified( @@ -18,7 +18,7 @@ where } let msg_send_fn = objc_msg_lookup(receiver as *mut _, sel.as_ptr() as *const _); - objc_try!({ A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args) }) + conditional_try(|| A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args)) } pub unsafe fn send_super_unverified( @@ -36,5 +36,5 @@ where super_class: superclass as *const Class as *const _, }; let msg_send_fn = objc_msg_lookup_super(&sup, sel.as_ptr() as *const _); - objc_try!({ A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args) }) + conditional_try(|| A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args)) } diff --git a/objc2/src/message/mod.rs b/objc2/src/message/mod.rs index c91a56005..94daec505 100644 --- a/objc2/src/message/mod.rs +++ b/objc2/src/message/mod.rs @@ -9,24 +9,21 @@ use crate::runtime::{Class, Imp, Object, Sel}; use crate::{Encode, EncodeArguments, RefEncode}; #[cfg(feature = "exception")] -macro_rules! objc_try { - ($b:block) => { - $crate::exception::catch_exception(|| $b).map_err(|exception| { - use alloc::borrow::ToOwned; - if let Some(exception) = exception { - MessageError(alloc::format!("Uncaught exception {:?}", exception)) - } else { - MessageError("Uncaught exception nil".to_owned()) - } - }) - }; +unsafe fn conditional_try(f: impl FnOnce() -> R) -> Result { + use alloc::borrow::ToOwned; + crate::exception::catch_exception(f).map_err(|exception| { + if let Some(exception) = exception { + MessageError(alloc::format!("Uncaught exception {:?}", exception)) + } else { + MessageError("Uncaught exception nil".to_owned()) + } + }) } #[cfg(not(feature = "exception"))] -macro_rules! objc_try { - ($b:block) => { - Ok($b) - }; +#[inline(always)] +unsafe fn conditional_try(f: impl FnOnce() -> R) -> Result { + Ok(f()) } mod verify;