Skip to content
Myr

Elide ErrorImpl type parameter if erased

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent ae372df · Browse files · 3 files+28 added−34 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+28 added−34 removed

  • other:+28 added−34 removed

3 files+28 added−34 removed

modified:src/error.rs+26 added-32 removed
201 unmodified lines
202 // Box<ErrorImpl<dyn StdError + Send + Sync + 'static>> except that the203 // result is a thin pointer. The necessary behavior for manipulating the204 // underlying ErrorImpl<E> is preserved in the vtable provided by the205 // caller rather than a builtin fat pointer vtable.206 let inner = NonNull::new_unchecked(Box::into_raw(inner) as *mut ErrorImpl<()>);206 let inner = NonNull::new_unchecked(Box::into_raw(inner) as *mut ErrorImpl);207 Error { inner }208 }209210 /// Wrap the error value with additional context.
297 unmodified lines
508 }509}510511struct ErrorVTable {512 object_drop: unsafe fn(NonNull<ErrorImpl<()>>),513 object_ref: unsafe fn(NonNull<ErrorImpl<()>>) -> *const (dyn StdError + Send + Sync + 'static),512 object_drop: unsafe fn(NonNull<ErrorImpl>),513 object_ref: unsafe fn(NonNull<ErrorImpl>) -> *const (dyn StdError + Send + Sync + 'static),514 #[cfg(feature = "std")]515 object_mut: unsafe fn(NonNull<ErrorImpl<()>>) -> *mut (dyn StdError + Send + Sync + 'static),516 object_boxed: unsafe fn(NonNull<ErrorImpl<()>>) -> Box<dyn StdError + Send + Sync + 'static>,517 object_downcast: unsafe fn(NonNull<ErrorImpl<()>>, TypeId) -> Option<NonNull<()>>,518 object_downcast_mut: unsafe fn(NonNull<ErrorImpl<()>>, TypeId) -> Option<NonNull<()>>,519 object_drop_rest: unsafe fn(NonNull<ErrorImpl<()>>, TypeId),515 object_mut: unsafe fn(NonNull<ErrorImpl>) -> *mut (dyn StdError + Send + Sync + 'static),516 object_boxed: unsafe fn(NonNull<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,517 object_downcast: unsafe fn(NonNull<ErrorImpl>, TypeId) -> Option<NonNull<()>>,518 object_downcast_mut: unsafe fn(NonNull<ErrorImpl>, TypeId) -> Option<NonNull<()>>,519 object_drop_rest: unsafe fn(NonNull<ErrorImpl>, TypeId),520}521522// Safety: requires layout of *e to match ErrorImpl<E>.523unsafe fn object_drop<E>(e: NonNull<ErrorImpl<()>>) {523unsafe fn object_drop<E>(e: NonNull<ErrorImpl>) {524 // Cast back to ErrorImpl<E> so that the allocator receives the correct525 // Layout to deallocate the Box's memory.526 let unerased = Box::from_raw(e.as_ptr() as *mut ErrorImpl<E>);527 drop(unerased);528}529530// Safety: requires layout of *e to match ErrorImpl<E>.531unsafe fn object_drop_front<E>(e: NonNull<ErrorImpl<()>>, target: TypeId) {531unsafe fn object_drop_front<E>(e: NonNull<ErrorImpl>, target: TypeId) {532 // Drop the fields of ErrorImpl other than E as well as the Box allocation,533 // without dropping E itself. This is used by downcast after doing a534 // ptr::read to take ownership of the E.535 let _ = target;
1 unmodified line
537 drop(unerased);538}539540// Safety: requires layout of *e to match ErrorImpl<E>.541unsafe fn object_ref<E>(e: NonNull<ErrorImpl<()>>) -> *const (dyn StdError + Send + Sync + 'static)541unsafe fn object_ref<E>(e: NonNull<ErrorImpl>) -> *const (dyn StdError + Send + Sync + 'static)542where543 E: StdError + Send + Sync + 'static,544{545 // Attach E's native StdError vtable onto a pointer to self._object.
2 unmodified lines
548549// Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived550// from a `&mut`551#[cfg(feature = "std")]552unsafe fn object_mut<E>(e: NonNull<ErrorImpl<()>>) -> *mut (dyn StdError + Send + Sync + 'static)552unsafe fn object_mut<E>(e: NonNull<ErrorImpl>) -> *mut (dyn StdError + Send + Sync + 'static)553where554 E: StdError + Send + Sync + 'static,555{556 // Attach E's native StdError vtable onto a pointer to self._object.557 &mut (*(e.as_ptr() as *mut ErrorImpl<E>))._object558}559560// Safety: requires layout of *e to match ErrorImpl<E>.561unsafe fn object_boxed<E>(e: NonNull<ErrorImpl<()>>) -> Box<dyn StdError + Send + Sync + 'static>561unsafe fn object_boxed<E>(e: NonNull<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>562where563 E: StdError + Send + Sync + 'static,564{565 // Attach ErrorImpl<E>'s native StdError vtable. The StdError impl is below.566 Box::<ErrorImpl<E>>::from_raw(e.as_ptr() as *mut ErrorImpl<E>)567}568569// Safety: requires layout of *e to match ErrorImpl<E>.570unsafe fn object_downcast<E>(e: NonNull<ErrorImpl<()>>, target: TypeId) -> Option<NonNull<()>>570unsafe fn object_downcast<E>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>571where572 E: 'static,573{574 if TypeId::of::<E>() == target {
7 unmodified lines
582 }583}584585// Safety: requires layout of *e to match ErrorImpl<E>.586unsafe fn object_downcast_mut<E>(e: NonNull<ErrorImpl<()>>, target: TypeId) -> Option<NonNull<()>>586unsafe fn object_downcast_mut<E>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>587where588 E: 'static,589{590 if TypeId::of::<E>() == target {
8 unmodified lines
599}600601// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.602#[cfg(feature = "std")]603unsafe fn context_downcast<C, E>(e: NonNull<ErrorImpl<()>>, target: TypeId) -> Option<NonNull<()>>603unsafe fn context_downcast<C, E>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>604where605 C: 'static,606 E: 'static,607{
11 unmodified lines
619}620621// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.622#[cfg(feature = "std")]623unsafe fn context_downcast_mut<C, E>(624 e: NonNull<ErrorImpl<()>>,625 target: TypeId,626) -> Option<NonNull<()>>623unsafe fn context_downcast_mut<C, E>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>624where625 C: 'static,626 E: 'static,627{
11 unmodified lines
639}640641// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.642#[cfg(feature = "std")]646unsafe fn context_drop_rest<C, E>(e: NonNull<ErrorImpl<()>>, target: TypeId)643unsafe fn context_drop_rest<C, E>(e: NonNull<ErrorImpl>, target: TypeId)644where645 C: 'static,646 E: 'static,647{
10 unmodified lines
658 }659}660661// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.665unsafe fn context_chain_downcast<C>(666 e: NonNull<ErrorImpl<()>>,667 target: TypeId,668) -> Option<NonNull<()>>662unsafe fn context_chain_downcast<C>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>663where664 C: 'static,665{666 if TypeId::of::<C>() == target {
9 unmodified lines
676}677678// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.679unsafe fn context_chain_downcast_mut<C>(686 e: NonNull<ErrorImpl<()>>,680 e: NonNull<ErrorImpl>,681 target: TypeId,682) -> Option<NonNull<()>>683where684 C: 'static,
10 unmodified lines
695 }696}697698// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.705unsafe fn context_chain_drop_rest<C>(e: NonNull<ErrorImpl<()>>, target: TypeId)699unsafe fn context_chain_drop_rest<C>(e: NonNull<ErrorImpl>, target: TypeId)700where701 C: 'static,702{703 // Called after downcasting by value to either the C or one of the causes
5 unmodified lines
709 drop(unerased);710 } else {711 let unerased =712 Box::from_raw(e.as_ptr() as *mut ErrorImpl<ContextError<C, ManuallyDrop<Error>>>);719 // Read the NonNull<ErrorImpl<()>> from the next error.713 // Read the NonNull<ErrorImpl> from the next error.714 let inner = unerased._object.error.inner;715 drop(unerased);716 let vtable = inner.as_ref().vtable;717 // Recursively drop the next error using the same target typeid.
4 unmodified lines
722// NOTE: If working with `ErrorImpl<()>`, references should be avoided in favor723// of raw pointers and `NonNull`.724// repr C to ensure that E remains in the final position.725#[repr(C)]732pub(crate) struct ErrorImpl<E> {726pub(crate) struct ErrorImpl<E = ()> {727 vtable: &'static ErrorVTable,728 backtrace: Option<Backtrace>,729 // NOTE: Don't use directly. Use only through vtable. Erased type may have730 // different alignment.
16 unmodified lines
747 pub error: E,748}749750impl<E> ErrorImpl<E> {757 fn erase(&self) -> NonNull<ErrorImpl<()>> {751 fn erase(&self) -> NonNull<ErrorImpl> {752 // Erase the concrete type of E but preserve the vtable in self.vtable753 // for manipulating the resulting thin pointer. This is analogous to an754 // unsize coercion.761 NonNull::from(self).cast::<ErrorImpl<()>>()755 NonNull::from(self).cast::<ErrorImpl>()756 }757}758765impl ErrorImpl<()> {759impl ErrorImpl {760 pub(crate) unsafe fn error<'a>(761 this: NonNull<Self>,762 ) -> &'a (dyn StdError + Send + Sync + 'static) {763 // Use vtable to attach E's native StdError vtable for the right
98 unmodified lines
modified:src/fmt.rs+1 added-1 removed
1 unmodified line
2use crate::error::ErrorImpl;3use core::fmt::{self, Debug, Write};4use core::ptr::NonNull;56impl ErrorImpl<()> {6impl ErrorImpl {7 pub(crate) unsafe fn display(this: NonNull<Self>, f: &mut fmt::Formatter) -> fmt::Result {8 write!(f, "{}", Self::error(this))?;910 if f.alternate() {
145 unmodified lines
modified:src/lib.rs+1 added-1 removed
368 unmodified lines
369/// }370/// ```371#[repr(transparent)]372pub struct Error {373 inner: NonNull<ErrorImpl<()>>,373 inner: NonNull<ErrorImpl>,374}375376unsafe impl Send for Error {}377unsafe impl Sync for Error {}
282 unmodified lines