Skip to content

Commit

Permalink
Improve monomorphization of conditional_try
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Dec 24, 2022
1 parent 8c3839d commit 8408975
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
5 changes: 2 additions & 3 deletions crates/objc2/src/message/apple/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::conditional_try;
use crate::encode::Encode;
use crate::ffi;
use crate::runtime::{Class, Imp, Object, Sel};
Expand Down Expand Up @@ -33,7 +32,7 @@ where
R: Encode,
{
let msg_send_fn = R::MSG_SEND;
unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
unsafe { conditional_try!(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
}

#[inline]
Expand All @@ -57,5 +56,5 @@ where
let receiver = receiver.cast();

let msg_send_fn = R::MSG_SEND_SUPER;
unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
unsafe { conditional_try!(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
}
5 changes: 2 additions & 3 deletions crates/objc2/src/message/gnustep.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use core::hint;
use core::mem;

use super::conditional_try;
use crate::encode::Encode;
use crate::ffi;
use crate::runtime::{Class, Imp, Object, Sel};
Expand Down Expand Up @@ -39,7 +38,7 @@ where

let msg_send_fn = unsafe { ffi::objc_msg_lookup(receiver.cast(), sel.as_ptr()) };
let msg_send_fn = unwrap_msg_send_fn(msg_send_fn);
unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
unsafe { conditional_try!(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
}

#[track_caller]
Expand All @@ -65,5 +64,5 @@ where
};
let msg_send_fn = unsafe { ffi::objc_msg_lookup_super(&sup, sel.as_ptr()) };
let msg_send_fn = unwrap_msg_send_fn(msg_send_fn);
unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
unsafe { conditional_try!(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
}
43 changes: 25 additions & 18 deletions crates/objc2/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,34 @@ use crate::rc::{Id, Owned, Ownership, Shared};
use crate::runtime::{Class, Imp, Object, Sel};
use crate::ClassType;

/// Wrap the given closure in `exception::catch` if the `catch-all` feature is
/// enabled.
///
/// This is a macro to help with monomorphization when the feature is
/// disabled, as well as improving the final stack trace (`#[track_caller]`
/// doesn't really work on closures).
#[cfg(not(feature = "catch-all"))]
macro_rules! conditional_try {
(|| $expr:expr) => {
$expr
};
}

#[cfg(feature = "catch-all")]
#[track_caller]
unsafe fn conditional_try<R: EncodeConvert>(f: impl FnOnce() -> R) -> R {
let f = core::panic::AssertUnwindSafe(f);
match unsafe { crate::exception::catch(f) } {
Ok(r) => r,
Err(exception) => {
if let Some(exception) = exception {
panic!("uncaught {:?}", exception)
} else {
panic!("uncaught exception nil")
macro_rules! conditional_try {
(|| $expr:expr) => {
let f = core::panic::AssertUnwindSafe(|| $expr);
match crate::exception::catch(f) {
Ok(r) => r,
Err(exception) => {
if let Some(exception) = exception {
panic!("uncaught {exception:?}")
} else {
panic!("uncaught exception nil")
}
}
}
}
}

#[cfg(not(feature = "catch-all"))]
#[inline]
#[track_caller]
unsafe fn conditional_try<R: EncodeConvert>(f: impl FnOnce() -> R) -> R {
f()
};
}

/// Help with monomorphizing in `icrate`
Expand Down

0 comments on commit 8408975

Please sign in to comment.