Skip to content

Commit 3abcd10

Browse files
committed
Make InFlightException invariant over E
1 parent 1741ff0 commit 3abcd10

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/api.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::backend::{ActiveBackend, RethrowHandle, ThrowByValue};
2+
use core::marker::PhantomData;
23

34
/// Throw an exception.
45
///
@@ -73,7 +74,14 @@ pub fn catch<R, E>(func: impl FnOnce() -> R) -> Result<R, E> {
7374
/// At this point, you can either drop the handle, which halts the Lithium machinery and brings you
7475
/// back to the sane land of [`Result`], or call [`InFlightException::rethrow`] to piggy-back on the
7576
/// contexts of the caught exception.
76-
pub struct InFlightException<E>(<ActiveBackend as ThrowByValue>::RethrowHandle<E>);
77+
pub struct InFlightException<E> {
78+
handle: <ActiveBackend as ThrowByValue>::RethrowHandle<E>,
79+
// Variance doesn't *really* matter here, since methods on `InFlightException<E>` never really
80+
// touch objects of type `E`, but making this type predictably invariant over `E` is valuable
81+
// for consistency, as it's difficult to keep track of which variance of the associated type is
82+
// exposed by which backend.
83+
_phantom: PhantomData<*mut E>,
84+
}
7785

7886
impl<E> InFlightException<E> {
7987
/// Throw a new exception by reusing the existing context.
@@ -89,7 +97,7 @@ impl<E> InFlightException<E> {
8997
pub unsafe fn rethrow<F>(self, new_cause: F) -> ! {
9098
// SAFETY: Requirements forwarded.
9199
unsafe {
92-
self.0.rethrow(new_cause);
100+
self.handle.rethrow(new_cause);
93101
}
94102
}
95103
}
@@ -223,7 +231,15 @@ impl<E> InFlightException<E> {
223231
)]
224232
#[inline(always)]
225233
pub fn intercept<R, E>(func: impl FnOnce() -> R) -> Result<R, (E, InFlightException<E>)> {
226-
ActiveBackend::intercept(func).map_err(|(cause, handle)| (cause, InFlightException(handle)))
234+
ActiveBackend::intercept(func).map_err(|(cause, handle)| {
235+
(
236+
cause,
237+
InFlightException {
238+
handle,
239+
_phantom: PhantomData,
240+
},
241+
)
242+
})
227243
}
228244

229245
#[cfg(test)]

0 commit comments

Comments
 (0)