Skip to content

Commit 18f8d48

Browse files
committed
Improve monomorphization of conditional_try
1 parent 8c3839d commit 18f8d48

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

crates/objc2/src/message/apple/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::conditional_try;
21
use crate::encode::Encode;
32
use crate::ffi;
43
use crate::runtime::{Class, Imp, Object, Sel};
@@ -33,7 +32,7 @@ where
3332
R: Encode,
3433
{
3534
let msg_send_fn = R::MSG_SEND;
36-
unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
35+
unsafe { conditional_try!(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
3736
}
3837

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

5958
let msg_send_fn = R::MSG_SEND_SUPER;
60-
unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
59+
unsafe { conditional_try!(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
6160
}

crates/objc2/src/message/gnustep.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::hint;
22
use core::mem;
33

4-
use super::conditional_try;
54
use crate::encode::Encode;
65
use crate::ffi;
76
use crate::runtime::{Class, Imp, Object, Sel};
@@ -39,7 +38,7 @@ where
3938

4039
let msg_send_fn = unsafe { ffi::objc_msg_lookup(receiver.cast(), sel.as_ptr()) };
4140
let msg_send_fn = unwrap_msg_send_fn(msg_send_fn);
42-
unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
41+
unsafe { conditional_try!(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
4342
}
4443

4544
#[track_caller]
@@ -65,5 +64,5 @@ where
6564
};
6665
let msg_send_fn = unsafe { ffi::objc_msg_lookup_super(&sup, sel.as_ptr()) };
6766
let msg_send_fn = unwrap_msg_send_fn(msg_send_fn);
68-
unsafe { conditional_try(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
67+
unsafe { conditional_try!(|| A::__invoke(msg_send_fn, receiver, sel, args)) }
6968
}

crates/objc2/src/message/mod.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,34 @@ use crate::rc::{Id, Owned, Ownership, Shared};
77
use crate::runtime::{Class, Imp, Object, Sel};
88
use crate::ClassType;
99

10+
/// Wrap the given closure in `exception::catch` if the `catch-all` feature is
11+
/// enabled.
12+
///
13+
/// This is a macro to help with monomorphization when the feature is
14+
/// disabled, as well as improving the final stack trace (`#[track_caller]`
15+
/// doesn't really work on closures).
16+
#[cfg(not(feature = "catch-all"))]
17+
macro_rules! conditional_try {
18+
(|| $expr:expr) => {
19+
$expr
20+
};
21+
}
22+
1023
#[cfg(feature = "catch-all")]
11-
#[track_caller]
12-
unsafe fn conditional_try<R: EncodeConvert>(f: impl FnOnce() -> R) -> R {
13-
let f = core::panic::AssertUnwindSafe(f);
14-
match unsafe { crate::exception::catch(f) } {
15-
Ok(r) => r,
16-
Err(exception) => {
17-
if let Some(exception) = exception {
18-
panic!("uncaught {:?}", exception)
19-
} else {
20-
panic!("uncaught exception nil")
24+
macro_rules! conditional_try {
25+
(|| $expr:expr) => {{
26+
let f = core::panic::AssertUnwindSafe(|| $expr);
27+
match crate::exception::catch(f) {
28+
Ok(r) => r,
29+
Err(exception) => {
30+
if let Some(exception) = exception {
31+
panic!("uncaught {exception:?}")
32+
} else {
33+
panic!("uncaught exception nil")
34+
}
2135
}
2236
}
23-
}
24-
}
25-
26-
#[cfg(not(feature = "catch-all"))]
27-
#[inline]
28-
#[track_caller]
29-
unsafe fn conditional_try<R: EncodeConvert>(f: impl FnOnce() -> R) -> R {
30-
f()
37+
}};
3138
}
3239

3340
/// Help with monomorphizing in `icrate`

0 commit comments

Comments
 (0)