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
Provenance
- Commit
bd5e39b- Tree
2a825c5- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
ae372df
3 files changed+28 added−34 removed
modified:src/error.rs+26 added-32 removed
201 unmodified lines202 // 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 }209 210 /// Wrap the error value with additional context.297 unmodified lines508 }509 }510 511 struct 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 }521 522 // Safety: requires layout of *e to match ErrorImpl<E>.523-unsafe fn object_drop<E>(e: NonNull<ErrorImpl<()>>) {523+unsafe 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 }529 530 // Safety: requires layout of *e to match ErrorImpl<E>.531-unsafe fn object_drop_front<E>(e: NonNull<ErrorImpl<()>>, target: TypeId) {531+unsafe 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 line537 drop(unerased);538 }539 540 // Safety: requires layout of *e to match ErrorImpl<E>.541-unsafe fn object_ref<E>(e: NonNull<ErrorImpl<()>>) -> *const (dyn StdError + Send + Sync + 'static)541+unsafe fn object_ref<E>(e: NonNull<ErrorImpl>) -> *const (dyn StdError + Send + Sync + 'static)542 where543 E: StdError + Send + Sync + 'static,544 {545 // Attach E's native StdError vtable onto a pointer to self._object.2 unmodified lines548 549 // Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived550 // from a `&mut`551 #[cfg(feature = "std")]552-unsafe fn object_mut<E>(e: NonNull<ErrorImpl<()>>) -> *mut (dyn StdError + Send + Sync + 'static)552+unsafe fn object_mut<E>(e: NonNull<ErrorImpl>) -> *mut (dyn StdError + Send + Sync + 'static)553 where554 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 }559 560 // Safety: requires layout of *e to match ErrorImpl<E>.561-unsafe fn object_boxed<E>(e: NonNull<ErrorImpl<()>>) -> Box<dyn StdError + Send + Sync + 'static>561+unsafe fn object_boxed<E>(e: NonNull<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>562 where563 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 }568 569 // Safety: requires layout of *e to match ErrorImpl<E>.570-unsafe fn object_downcast<E>(e: NonNull<ErrorImpl<()>>, target: TypeId) -> Option<NonNull<()>>570+unsafe fn object_downcast<E>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>571 where572 E: 'static,573 {574 if TypeId::of::<E>() == target {7 unmodified lines582 }583 }584 585 // Safety: requires layout of *e to match ErrorImpl<E>.586-unsafe fn object_downcast_mut<E>(e: NonNull<ErrorImpl<()>>, target: TypeId) -> Option<NonNull<()>>586+unsafe fn object_downcast_mut<E>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>587 where588 E: 'static,589 {590 if TypeId::of::<E>() == target {8 unmodified lines599 }600 601 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.602 #[cfg(feature = "std")]603-unsafe fn context_downcast<C, E>(e: NonNull<ErrorImpl<()>>, target: TypeId) -> Option<NonNull<()>>603+unsafe fn context_downcast<C, E>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>604 where605 C: 'static,606 E: 'static,607 {11 unmodified lines619 }620 621 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.622 #[cfg(feature = "std")]623-unsafe fn context_downcast_mut<C, E>(624- e: NonNull<ErrorImpl<()>>,625- target: TypeId,626-) -> Option<NonNull<()>>623+unsafe fn context_downcast_mut<C, E>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>624 where625 C: 'static,626 E: 'static,627 {11 unmodified lines639 }640 641 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.642 #[cfg(feature = "std")]646-unsafe fn context_drop_rest<C, E>(e: NonNull<ErrorImpl<()>>, target: TypeId)643+unsafe fn context_drop_rest<C, E>(e: NonNull<ErrorImpl>, target: TypeId)644 where645 C: 'static,646 E: 'static,647 {10 unmodified lines658 }659 }660 661 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.665-unsafe fn context_chain_downcast<C>(666- e: NonNull<ErrorImpl<()>>,667- target: TypeId,668-) -> Option<NonNull<()>>662+unsafe fn context_chain_downcast<C>(e: NonNull<ErrorImpl>, target: TypeId) -> Option<NonNull<()>>663 where664 C: 'static,665 {666 if TypeId::of::<C>() == target {9 unmodified lines676 }677 678 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.679 unsafe fn context_chain_downcast_mut<C>(686- e: NonNull<ErrorImpl<()>>,680+ e: NonNull<ErrorImpl>,681 target: TypeId,682 ) -> Option<NonNull<()>>683 where684 C: 'static,10 unmodified lines695 }696 }697 698 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.705-unsafe fn context_chain_drop_rest<C>(e: NonNull<ErrorImpl<()>>, target: TypeId)699+unsafe fn context_chain_drop_rest<C>(e: NonNull<ErrorImpl>, target: TypeId)700 where701 C: 'static,702 {703 // Called after downcasting by value to either the C or one of the causes5 unmodified lines709 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 lines722 // 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)]732-pub(crate) struct ErrorImpl<E> {726+pub(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 lines747 pub error: E,748 }749 750 impl<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 }758 765-impl ErrorImpl<()> {759+impl 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 right98 unmodified linesmodified:src/fmt.rs+1 added-1 removed
1 unmodified line2 use crate::error::ErrorImpl;3 use core::fmt::{self, Debug, Write};4 use core::ptr::NonNull;5 6-impl ErrorImpl<()> {6+impl ErrorImpl {7 pub(crate) unsafe fn display(this: NonNull<Self>, f: &mut fmt::Formatter) -> fmt::Result {8 write!(f, "{}", Self::error(this))?;9 10 if f.alternate() {145 unmodified linesmodified:src/lib.rs+1 added-1 removed
368 unmodified lines369 /// }370 /// ```371 #[repr(transparent)]372 pub struct Error {373- inner: NonNull<ErrorImpl<()>>,373+ inner: NonNull<ErrorImpl>,374 }375 376 unsafe impl Send for Error {}377 unsafe impl Sync for Error {}282 unmodified lines