Skip to content

Commit

Permalink
Convert internal objc_try! macro to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Oct 3, 2021
1 parent 1b62354 commit 4e9cd30
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
6 changes: 3 additions & 3 deletions objc2/src/message/apple/mod.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -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<A, R>(
Expand All @@ -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))
}
6 changes: 3 additions & 3 deletions objc2/src/message/gnustep.rs
Original file line number Diff line number Diff line change
@@ -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<A, R>(
Expand All @@ -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<A, R>(
Expand All @@ -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))
}
27 changes: 12 additions & 15 deletions objc2/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: Encode>(f: impl FnOnce() -> R) -> Result<R, MessageError> {
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<R: Encode>(f: impl FnOnce() -> R) -> Result<R, MessageError> {
Ok(f())
}

mod verify;
Expand Down

0 comments on commit 4e9cd30

Please sign in to comment.