Skip to content

Commit

Permalink
Avoid stacked-borrows violations
Browse files Browse the repository at this point in the history
Turn into a pointer without re-borrowing.
  • Loading branch information
clubby789 authored and vorner committed Dec 25, 2022
1 parent 4793409 commit eaeae27
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ref_cnt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ unsafe impl<T> RefCnt for Arc<T> {
Arc::into_raw(me) as *mut T
}
fn as_ptr(me: &Arc<T>) -> *mut T {
me as &T as *const T as *mut T
// SAFETY: &T cast to *const T will always be aligned, initialised and valid for reads
let ptr = Arc::into_raw(unsafe { std::ptr::read(me) });
ptr as *mut T
}
unsafe fn from_ptr(ptr: *const T) -> Arc<T> {
Arc::from_raw(ptr)
Expand All @@ -105,7 +107,9 @@ unsafe impl<T> RefCnt for Rc<T> {
Rc::into_raw(me) as *mut T
}
fn as_ptr(me: &Rc<T>) -> *mut T {
me as &T as *const T as *mut T
// SAFETY: &T cast to *const T will always be aligned, initialised and valid for reads
let ptr = Rc::into_raw(unsafe { std::ptr::read(me) });
ptr as *mut T
}
unsafe fn from_ptr(ptr: *const T) -> Rc<T> {
Rc::from_raw(ptr)
Expand Down

0 comments on commit eaeae27

Please sign in to comment.