@@ -341,12 +341,12 @@ impl<T: ?Sized> Rc<T> {
341
341
unsafe { self.ptr.as_ref() }
342
342
}
343
343
344
- fn from_inner(ptr: NonNull<RcBox<T>>) -> Self {
344
+ unsafe fn from_inner(ptr: NonNull<RcBox<T>>) -> Self {
345
345
Self { ptr, phantom: PhantomData }
346
346
}
347
347
348
348
unsafe fn from_ptr(ptr: *mut RcBox<T>) -> Self {
349
- Self::from_inner(unsafe { NonNull::new_unchecked(ptr) })
349
+ unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) }
350
350
}
351
351
}
352
352
@@ -367,9 +367,11 @@ impl<T> Rc<T> {
367
367
// pointers, which ensures that the weak destructor never frees
368
368
// the allocation while the strong destructor is running, even
369
369
// if the weak pointer is stored inside the strong one.
370
- Self::from_inner(
371
- Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
372
- )
370
+ unsafe {
371
+ Self::from_inner(
372
+ Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
373
+ )
374
+ }
373
375
}
374
376
375
377
/// Constructs a new `Rc<T>` using a weak reference to itself. Attempting
@@ -420,16 +422,16 @@ impl<T> Rc<T> {
420
422
// otherwise.
421
423
let data = data_fn(&weak);
422
424
423
- unsafe {
425
+ let strong = unsafe {
424
426
let inner = init_ptr.as_ptr();
425
427
ptr::write(ptr::addr_of_mut!((*inner).value), data);
426
428
427
429
let prev_value = (*inner).strong.get();
428
430
debug_assert_eq!(prev_value, 0, "No prior strong references should exist");
429
431
(*inner).strong.set(1);
430
- }
431
432
432
- let strong = Rc::from_inner(init_ptr);
433
+ Rc::from_inner(init_ptr)
434
+ };
433
435
434
436
// Strong references should collectively own a shared weak reference,
435
437
// so don't run the destructor for our old weak reference.
@@ -521,10 +523,12 @@ impl<T> Rc<T> {
521
523
// pointers, which ensures that the weak destructor never frees
522
524
// the allocation while the strong destructor is running, even
523
525
// if the weak pointer is stored inside the strong one.
524
- Ok(Self::from_inner(
525
- Box::leak(Box::try_new(RcBox { strong: Cell::new(1), weak: Cell::new(1), value })?)
526
- .into(),
527
- ))
526
+ unsafe {
527
+ Ok(Self::from_inner(
528
+ Box::leak(Box::try_new(RcBox { strong: Cell::new(1), weak: Cell::new(1), value })?)
529
+ .into(),
530
+ ))
531
+ }
528
532
}
529
533
530
534
/// Constructs a new `Rc` with uninitialized contents, returning an error if the allocation fails
@@ -746,7 +750,7 @@ impl<T> Rc<mem::MaybeUninit<T>> {
746
750
#[unstable(feature = "new_uninit", issue = "63291")]
747
751
#[inline]
748
752
pub unsafe fn assume_init(self) -> Rc<T> {
749
- Rc::from_inner(mem::ManuallyDrop::new(self).ptr.cast())
753
+ unsafe { Rc::from_inner(mem::ManuallyDrop::new(self).ptr.cast()) }
750
754
}
751
755
}
752
756
@@ -1214,9 +1218,11 @@ impl Rc<dyn Any> {
1214
1218
/// ```
1215
1219
pub fn downcast<T: Any>(self) -> Result<Rc<T>, Rc<dyn Any>> {
1216
1220
if (*self).is::<T>() {
1217
- let ptr = self.ptr.cast::<RcBox<T>>();
1218
- forget(self);
1219
- Ok(Rc::from_inner(ptr))
1221
+ unsafe {
1222
+ let ptr = self.ptr.cast::<RcBox<T>>();
1223
+ forget(self);
1224
+ Ok(Rc::from_inner(ptr))
1225
+ }
1220
1226
} else {
1221
1227
Err(self)
1222
1228
}
@@ -1489,8 +1495,10 @@ impl<T: ?Sized> Clone for Rc<T> {
1489
1495
/// ```
1490
1496
#[inline]
1491
1497
fn clone(&self) -> Rc<T> {
1492
- self.inner().inc_strong();
1493
- Self::from_inner(self.ptr)
1498
+ unsafe {
1499
+ self.inner().inc_strong();
1500
+ Self::from_inner(self.ptr)
1501
+ }
1494
1502
}
1495
1503
}
1496
1504
@@ -2245,11 +2253,14 @@ impl<T: ?Sized> Weak<T> {
2245
2253
#[stable(feature = "rc_weak", since = "1.4.0")]
2246
2254
pub fn upgrade(&self) -> Option<Rc<T>> {
2247
2255
let inner = self.inner()?;
2256
+
2248
2257
if inner.strong() == 0 {
2249
2258
None
2250
2259
} else {
2251
- inner.inc_strong();
2252
- Some(Rc::from_inner(self.ptr))
2260
+ unsafe {
2261
+ inner.inc_strong();
2262
+ Some(Rc::from_inner(self.ptr))
2263
+ }
2253
2264
}
2254
2265
}
2255
2266
0 commit comments