Skip to content
Myr

Merge pull request #452 from dtolnay/downcast

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

Eliminate pointer->reference->pointer during downcast

authored by David Tolnay · committed by GitHub · parents 917a169, 6e8c000 · Browse files · 2 files+30 added−6 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+30 added−6 removed

  • tests:+23 added−0 removed
  • other:+7 added−6 removed

2 files+30 added−6 removed

modified:src/error.rs+7 added-6 removed
817 unmodified lines
818 E: 'static,819{820 if TypeId::of::<C>() == target {821 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();822 let unerased = unsafe { unerased_ref.deref() };823 Some(Ref::new(&unerased._object.context).cast::<()>())822 let inner = unsafe { ptr::addr_of!((*unerased_ref.as_ptr())._object.context) };823 Some(Ref::from_raw(unsafe { NonNull::new_unchecked(inner.cast_mut()) }).cast::<()>())824 } else if TypeId::of::<E>() == target {825 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();826 let unerased = unsafe { unerased_ref.deref() };827 Some(Ref::new(&unerased._object.error).cast::<()>())826 let inner = unsafe { ptr::addr_of!((*unerased_ref.as_ptr())._object.error) };827 Some(Ref::from_raw(unsafe { NonNull::new_unchecked(inner.cast_mut()) }).cast::<()>())828 } else {829 None830 }831}
21 unmodified lines
853where854 C: 'static,855{856 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();857 let unerased = unsafe { unerased_ref.deref() };857 if TypeId::of::<C>() == target {859 Some(Ref::new(&unerased._object.context).cast::<()>())858 let inner = unsafe { ptr::addr_of!((*unerased_ref.as_ptr())._object.context) };859 Some(Ref::from_raw(unsafe { NonNull::new_unchecked(inner.cast_mut()) }).cast::<()>())860 } else {861 // Recurse down the context chain per the inner error's vtable.862 let unerased = unsafe { unerased_ref.deref() };863 let source = &unerased._object.error;864 unsafe { (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target) }865 }866}
194 unmodified lines
modified:tests/test_context.rs+23 added
163 unmodified lines
164 assert!(dropped.all());165}166167#[test]168fn test_downcast_mut() {169 let (mut err, dropped) = make_chain();170171 err.downcast_mut::<HighLevel>().unwrap().message = "high context";172 err.downcast_mut::<MidLevel>().unwrap().message = "mid context";173 err.downcast_mut::<LowLevel>().unwrap().message = "low error";174175 assert_eq!(176 err.downcast_ref::<HighLevel>().unwrap().message,177 "high context",178 );179 assert_eq!(180 err.downcast_ref::<MidLevel>().unwrap().message,181 "mid context",182 );183 assert_eq!(err.downcast_ref::<LowLevel>().unwrap().message, "low error");184185 assert!(dropped.none());186 drop(err);187 assert!(dropped.all());188}189190#[test]191fn test_root_cause() {192 let (err, _) = make_chain();193194 assert_eq!(err.root_cause().to_string(), "no such file or directory");
1 unmodified line