Skip to content

Commit 6e8c000

Browse files
committed
Eliminate pointer->reference->pointer during downcast
1 parent 67c4abd commit 6e8c000

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/error.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -819,12 +819,12 @@ where
819819
{
820820
if TypeId::of::<C>() == target {
821821
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::<()>())
824824
} else if TypeId::of::<E>() == target {
825825
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::<()>())
828828
} else {
829829
None
830830
}
@@ -854,11 +854,12 @@ where
854854
C: 'static,
855855
{
856856
let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();
857-
let unerased = unsafe { unerased_ref.deref() };
858857
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::<()>())
860860
} else {
861861
// Recurse down the context chain per the inner error's vtable.
862+
let unerased = unsafe { unerased_ref.deref() };
862863
let source = &unerased._object.error;
863864
unsafe { (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target) }
864865
}

0 commit comments

Comments
 (0)