Merge pull request #33 from dtolnay/typeid
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Move TypeId out of the Box allocation
authored by David Tolnay · committed by GitHub · parents 30c2eab, 7b9b07e · Browse files · 2 files·+57 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+57 added−24 removed
- tests:+8 added−0 removed
- other:+49 added−24 removed
Provenance
- Commit
8885e93- Tree
1f4b843- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
30c2eab- Parent 2
7b9b07e
2 files changed+57 added−24 removed
modified:src/error.rs+49 added-24 removed
25 unmodified lines26 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self27 where28 E: StdError + Send + Sync + 'static,29 {30- let type_id = TypeId::of::<E>();30+ let vtable = &ErrorVTable {31+ object_drop: object_drop::<E>,32+ object_drop_front: object_drop_front::<E>,33+ object_ref: object_ref::<E>,34+ object_mut: object_mut::<E>,35+ object_boxed: object_boxed::<E>,36+ object_is: object_is::<E>,37+ };38 32- // Safety: passing typeid of the right type E.33- unsafe { Error::construct(error, type_id, backtrace) }39+ // Safety: passing vtable that operates on the right type E.40+ unsafe { Error::construct(error, vtable, backtrace) }41 }42 43 pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self44 where45 M: Display + Debug + Send + Sync + 'static,46 {47 let error = MessageError(message);41- let type_id = TypeId::of::<M>();48+ let vtable = &ErrorVTable {49+ object_drop: object_drop::<MessageError<M>>,50+ object_drop_front: object_drop_front::<MessageError<M>>,51+ object_ref: object_ref::<MessageError<M>>,52+ object_mut: object_mut::<MessageError<M>>,53+ object_boxed: object_boxed::<MessageError<M>>,54+ object_is: object_is::<M>,55+ };56 43- // Safety: MessageError is repr(transparent) so MessageError<M> has the44- // same layout as the typeid specifies.45- unsafe { Error::construct(error, type_id, backtrace) }57+ // Safety: MessageError is repr(transparent) so it is okay for the58+ // vtable to allow casting the MessageError<M> to M.59+ unsafe { Error::construct(error, vtable, backtrace) }60 }61 62 pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self63 where64 M: Display + Send + Sync + 'static,65 {66 let error = DisplayError(message);53- let type_id = TypeId::of::<M>();67+ let vtable = &ErrorVTable {68+ object_drop: object_drop::<DisplayError<M>>,69+ object_drop_front: object_drop_front::<DisplayError<M>>,70+ object_ref: object_ref::<DisplayError<M>>,71+ object_mut: object_mut::<DisplayError<M>>,72+ object_boxed: object_boxed::<DisplayError<M>>,73+ object_is: object_is::<M>,74+ };75 55- // Safety: DisplayError is repr(transparent) so DisplayError<M> has the56- // same layout as the typeid specifies.57- unsafe { Error::construct(error, type_id, backtrace) }76+ // Safety: DisplayError is repr(transparent) so it is okay for the77+ // vtable to allow casting the DisplayError<M> to M.78+ unsafe { Error::construct(error, vtable, backtrace) }79 }80 81 pub(crate) fn from_context<C, E>(context: C, error: E) -> Self82 where5 unmodified lines88 89 // Takes backtrace as argument rather than capturing it here so that the90 // user sees one fewer layer of wrapping noise in the backtrace.91 //71- // Unsafe because the type represented by type_id must have the same layout72- // as E or else we allow invalid downcasts.73- unsafe fn construct<E>(error: E, type_id: TypeId, backtrace: Option<Backtrace>) -> Self92+ // Unsafe because the given vtable must have sensible behavior on the error93+ // value of type E.94+ unsafe fn construct<E>(95+ error: E,96+ vtable: &'static ErrorVTable,97+ backtrace: Option<Backtrace>,98+ ) -> Self99 where100 E: StdError + Send + Sync + 'static,101 {77- let vtable = &ErrorVTable {78- object_drop: object_drop::<E>,79- object_drop_front: object_drop_front::<E>,80- object_ref: object_ref::<E>,81- object_mut: object_mut::<E>,82- object_boxed: object_boxed::<E>,83- };102 let inner = Box::new(ErrorImpl {103 vtable,86- type_id,104 backtrace,105 _error: error,106 });107 let erased = mem::transmute::<Box<ErrorImpl<E>>, Box<ErrorImpl<()>>>(inner);124 unmodified lines232 pub fn is<E>(&self) -> bool233 where234 E: Display + Debug + Send + Sync + 'static,235 {219- TypeId::of::<E>() == self.inner.type_id236+ let target = TypeId::of::<E>();237+ unsafe { (self.inner.vtable.object_is)(target) }238 }239 240 /// Attempt to downcast the error object to a concrete type.241 pub fn downcast<E>(self) -> Result<E, Self>134 unmodified lines376 object_drop_front: unsafe fn(Box<ErrorImpl<()>>),377 object_ref: unsafe fn(&ErrorImpl<()>) -> &(dyn StdError + Send + Sync + 'static),378 object_mut: unsafe fn(&mut ErrorImpl<()>) -> &mut (dyn StdError + Send + Sync + 'static),379 object_boxed: unsafe fn(Box<ErrorImpl<()>>) -> Box<dyn StdError + Send + Sync + 'static>,380+ object_is: unsafe fn(TypeId) -> bool,381 }382 383 unsafe fn object_drop<E>(e: Box<ErrorImpl<()>>) {384 // Cast back to ErrorImpl<E> so that the allocator receives the correct30 unmodified lines415 {416 mem::transmute::<Box<ErrorImpl<()>>, Box<ErrorImpl<E>>>(e)417 }418 419+unsafe fn object_is<T>(target: TypeId) -> bool420+where421+ T: 'static,422+{423+ TypeId::of::<T>() == target424+}425+426 // repr C to ensure that `E` remains in the final position427 #[repr(C)]428 pub(crate) struct ErrorImpl<E> {429 vtable: &'static ErrorVTable,404- type_id: TypeId,430 backtrace: Option<Backtrace>,431 // NOTE: Don't use directly. Use only through vtable. Erased type may have different alignment.432 _error: E,433 }180 unmodified linesmodified:tests/test_downcast.rs+8 added
97 unmodified lines98 "oh no!",99 error.downcast_ref::<LargeAlignedError>().unwrap().0100 );101 }102+103+#[test]104+fn test_unsuccessful_downcast() {105+ let mut error = bail_error().unwrap_err();106+ assert!(error.downcast_ref::<&str>().is_none());107+ assert!(error.downcast_mut::<&str>().is_none());108+ assert!(error.downcast::<&str>().is_err());109+}