unsafe { self.ptr.as_ref() }
}
- fn from_inner(ptr: NonNull<RcBox<T>>) -> Self {
+ unsafe fn from_inner(ptr: NonNull<RcBox<T>>) -> Self {
Self { ptr, phantom: PhantomData }
}
unsafe fn from_ptr(ptr: *mut RcBox<T>) -> Self {
- Self::from_inner(unsafe { NonNull::new_unchecked(ptr) })
+ unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) }
}
}
// pointers, which ensures that the weak destructor never frees
// the allocation while the strong destructor is running, even
// if the weak pointer is stored inside the strong one.
- Self::from_inner(
- Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
- )
+ unsafe {
+ Self::from_inner(
+ Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
+ )
+ }
}
/// Constructs a new `Rc<T>` using a weak reference to itself. Attempting
// otherwise.
let data = data_fn(&weak);
- unsafe {
+ let strong = unsafe {
let inner = init_ptr.as_ptr();
ptr::write(ptr::addr_of_mut!((*inner).value), data);
let prev_value = (*inner).strong.get();
debug_assert_eq!(prev_value, 0, "No prior strong references should exist");
(*inner).strong.set(1);
- }
- let strong = Rc::from_inner(init_ptr);
+ Rc::from_inner(init_ptr)
+ };
// Strong references should collectively own a shared weak reference,
// so don't run the destructor for our old weak reference.
// pointers, which ensures that the weak destructor never frees
// the allocation while the strong destructor is running, even
// if the weak pointer is stored inside the strong one.
- Ok(Self::from_inner(
- Box::leak(Box::try_new(RcBox { strong: Cell::new(1), weak: Cell::new(1), value })?)
- .into(),
- ))
+ unsafe {
+ Ok(Self::from_inner(
+ Box::leak(Box::try_new(RcBox { strong: Cell::new(1), weak: Cell::new(1), value })?)
+ .into(),
+ ))
+ }
}
/// Constructs a new `Rc` with uninitialized contents, returning an error if the allocation fails
#[unstable(feature = "new_uninit", issue = "63291")]
#[inline]
pub unsafe fn assume_init(self) -> Rc<T> {
- Rc::from_inner(mem::ManuallyDrop::new(self).ptr.cast())
+ unsafe { Rc::from_inner(mem::ManuallyDrop::new(self).ptr.cast()) }
}
}
/// ```
pub fn downcast<T: Any>(self) -> Result<Rc<T>, Rc<dyn Any>> {
if (*self).is::<T>() {
- let ptr = self.ptr.cast::<RcBox<T>>();
- forget(self);
- Ok(Rc::from_inner(ptr))
+ unsafe {
+ let ptr = self.ptr.cast::<RcBox<T>>();
+ forget(self);
+ Ok(Rc::from_inner(ptr))
+ }
} else {
Err(self)
}
/// ```
#[inline]
fn clone(&self) -> Rc<T> {
- self.inner().inc_strong();
- Self::from_inner(self.ptr)
+ unsafe {
+ self.inner().inc_strong();
+ Self::from_inner(self.ptr)
+ }
}
}
#[stable(feature = "rc_weak", since = "1.4.0")]
pub fn upgrade(&self) -> Option<Rc<T>> {
let inner = self.inner()?;
+
if inner.strong() == 0 {
None
} else {
- inner.inc_strong();
- Some(Rc::from_inner(self.ptr))
+ unsafe {
+ inner.inc_strong();
+ Some(Rc::from_inner(self.ptr))
+ }
}
}