alloc/rcs/rc.rs
1//! Single-threaded reference-counting pointers. 'Rc' stands for 'Reference
2//! Counted'.
3//!
4//! The type [`Rc<T>`][`Rc`] provides shared ownership of a value of type `T`,
5//! allocated in the heap. Invoking [`clone`][clone] on [`Rc`] produces a new
6//! pointer to the same allocation in the heap. When the last [`Rc`] pointer to a
7//! given allocation is destroyed, the value stored in that allocation (often
8//! referred to as "inner value") is also dropped.
9//!
10//! Shared references in Rust disallow mutation by default, and [`Rc`]
11//! is no exception: you cannot generally obtain a mutable reference to
12//! something inside an [`Rc`]. If you need mutability, put a [`Cell`]
13//! or [`RefCell`] inside the [`Rc`]; see [an example of mutability
14//! inside an `Rc`][mutability].
15//!
16//! [`Rc`] uses non-atomic reference counting. This means that overhead is very
17//! low, but an [`Rc`] cannot be sent between threads, and consequently [`Rc`]
18//! does not implement [`Send`]. As a result, the Rust compiler
19//! will check *at compile time* that you are not sending [`Rc`]s between
20//! threads. If you need multi-threaded, atomic reference counting, use
21//! [`sync::Arc`][arc].
22//!
23//! The [`downgrade`][downgrade] method can be used to create a non-owning
24//! [`Weak`] pointer. A [`Weak`] pointer can be [`upgrade`][upgrade]d
25//! to an [`Rc`], but this will return [`None`] if the value stored in the allocation has
26//! already been dropped. In other words, `Weak` pointers do not keep the value
27//! inside the allocation alive; however, they *do* keep the allocation
28//! (the backing store for the inner value) alive.
29//!
30//! A cycle between [`Rc`] pointers will never be deallocated. For this reason,
31//! [`Weak`] is used to break cycles. For example, a tree could have strong
32//! [`Rc`] pointers from parent nodes to children, and [`Weak`] pointers from
33//! children back to their parents.
34//!
35//! `Rc<T>` automatically dereferences to `T` (via the [`Deref`] trait),
36//! so you can call `T`'s methods on a value of type [`Rc<T>`][`Rc`]. To avoid name
37//! clashes with `T`'s methods, the methods of [`Rc<T>`][`Rc`] itself are associated
38//! functions, called using [fully qualified syntax]:
39//!
40//! ```
41//! use std::rc::Rc;
42//!
43//! let my_rc = Rc::new(());
44//! let my_weak = Rc::downgrade(&my_rc);
45//! ```
46//!
47//! `Rc<T>`'s implementations of traits like `Clone` may also be called using
48//! fully qualified syntax. Some people prefer to use fully qualified syntax,
49//! while others prefer using method-call syntax.
50//!
51//! ```
52//! use std::rc::Rc;
53//!
54//! let rc = Rc::new(());
55//! // Method-call syntax
56//! let rc2 = rc.clone();
57//! // Fully qualified syntax
58//! let rc3 = Rc::clone(&rc);
59//! ```
60//!
61//! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the inner value may have
62//! already been dropped.
63//!
64//! # Cloning references
65//!
66//! Creating a new reference to the same allocation as an existing reference counted pointer
67//! is done using the `Clone` trait implemented for [`Rc<T>`][`Rc`] and [`Weak<T>`][`Weak`].
68//!
69//! ```
70//! use std::rc::Rc;
71//!
72//! let foo = Rc::new(vec![1.0, 2.0, 3.0]);
73//! // The two syntaxes below are equivalent.
74//! let a = foo.clone();
75//! let b = Rc::clone(&foo);
76//! // a and b both point to the same memory location as foo.
77//! ```
78//!
79//! The `Rc::clone(&from)` syntax is the most idiomatic because it conveys more explicitly
80//! the meaning of the code. In the example above, this syntax makes it easier to see that
81//! this code is creating a new reference rather than copying the whole content of foo.
82//!
83//! # Examples
84//!
85//! Consider a scenario where a set of `Gadget`s are owned by a given `Owner`.
86//! We want to have our `Gadget`s point to their `Owner`. We can't do this with
87//! unique ownership, because more than one gadget may belong to the same
88//! `Owner`. [`Rc`] allows us to share an `Owner` between multiple `Gadget`s,
89//! and have the `Owner` remain allocated as long as any `Gadget` points at it.
90//!
91//! ```
92//! use std::rc::Rc;
93//!
94//! struct Owner {
95//! name: String,
96//! // ...other fields
97//! }
98//!
99//! struct Gadget {
100//! id: i32,
101//! owner: Rc<Owner>,
102//! // ...other fields
103//! }
104//!
105//! fn main() {
106//! // Create a reference-counted `Owner`.
107//! let gadget_owner: Rc<Owner> = Rc::new(
108//! Owner {
109//! name: "Gadget Man".to_string(),
110//! }
111//! );
112//!
113//! // Create `Gadget`s belonging to `gadget_owner`. Cloning the `Rc<Owner>`
114//! // gives us a new pointer to the same `Owner` allocation, incrementing
115//! // the reference count in the process.
116//! let gadget1 = Gadget {
117//! id: 1,
118//! owner: Rc::clone(&gadget_owner),
119//! };
120//! let gadget2 = Gadget {
121//! id: 2,
122//! owner: Rc::clone(&gadget_owner),
123//! };
124//!
125//! // Dispose of our local variable `gadget_owner`.
126//! drop(gadget_owner);
127//!
128//! // Despite dropping `gadget_owner`, we're still able to print out the name
129//! // of the `Owner` of the `Gadget`s. This is because we've only dropped a
130//! // single `Rc<Owner>`, not the `Owner` it points to. As long as there are
131//! // other `Rc<Owner>` pointing at the same `Owner` allocation, it will remain
132//! // live. The field projection `gadget1.owner.name` works because
133//! // `Rc<Owner>` automatically dereferences to `Owner`.
134//! println!("Gadget {} owned by {}", gadget1.id, gadget1.owner.name);
135//! println!("Gadget {} owned by {}", gadget2.id, gadget2.owner.name);
136//!
137//! // At the end of the function, `gadget1` and `gadget2` are destroyed, and
138//! // with them the last counted references to our `Owner`. Gadget Man now
139//! // gets destroyed as well.
140//! }
141//! ```
142//!
143//! If our requirements change, and we also need to be able to traverse from
144//! `Owner` to `Gadget`, we will run into problems. An [`Rc`] pointer from `Owner`
145//! to `Gadget` introduces a cycle. This means that their
146//! reference counts can never reach 0, and the allocation will never be destroyed:
147//! a memory leak. In order to get around this, we can use [`Weak`]
148//! pointers.
149//!
150//! Rust actually makes it somewhat difficult to produce this loop in the first
151//! place. In order to end up with two values that point at each other, one of
152//! them needs to be mutable. This is difficult because [`Rc`] enforces
153//! memory safety by only giving out shared references to the value it wraps,
154//! and these don't allow direct mutation. We need to wrap the part of the
155//! value we wish to mutate in a [`RefCell`], which provides *interior
156//! mutability*: a method to achieve mutability through a shared reference.
157//! [`RefCell`] enforces Rust's borrowing rules at runtime.
158//!
159//! ```
160//! use std::rc::Rc;
161//! use std::rc::Weak;
162//! use std::cell::RefCell;
163//!
164//! struct Owner {
165//! name: String,
166//! gadgets: RefCell<Vec<Weak<Gadget>>>,
167//! // ...other fields
168//! }
169//!
170//! struct Gadget {
171//! id: i32,
172//! owner: Rc<Owner>,
173//! // ...other fields
174//! }
175//!
176//! fn main() {
177//! // Create a reference-counted `Owner`. Note that we've put the `Owner`'s
178//! // vector of `Gadget`s inside a `RefCell` so that we can mutate it through
179//! // a shared reference.
180//! let gadget_owner: Rc<Owner> = Rc::new(
181//! Owner {
182//! name: "Gadget Man".to_string(),
183//! gadgets: RefCell::new(vec![]),
184//! }
185//! );
186//!
187//! // Create `Gadget`s belonging to `gadget_owner`, as before.
188//! let gadget1 = Rc::new(
189//! Gadget {
190//! id: 1,
191//! owner: Rc::clone(&gadget_owner),
192//! }
193//! );
194//! let gadget2 = Rc::new(
195//! Gadget {
196//! id: 2,
197//! owner: Rc::clone(&gadget_owner),
198//! }
199//! );
200//!
201//! // Add the `Gadget`s to their `Owner`.
202//! {
203//! let mut gadgets = gadget_owner.gadgets.borrow_mut();
204//! gadgets.push(Rc::downgrade(&gadget1));
205//! gadgets.push(Rc::downgrade(&gadget2));
206//!
207//! // `RefCell` dynamic borrow ends here.
208//! }
209//!
210//! // Iterate over our `Gadget`s, printing their details out.
211//! for gadget_weak in gadget_owner.gadgets.borrow().iter() {
212//!
213//! // `gadget_weak` is a `Weak<Gadget>`. Since `Weak` pointers can't
214//! // guarantee the allocation still exists, we need to call
215//! // `upgrade`, which returns an `Option<Rc<Gadget>>`.
216//! //
217//! // In this case we know the allocation still exists, so we simply
218//! // `unwrap` the `Option`. In a more complicated program, you might
219//! // need graceful error handling for a `None` result.
220//!
221//! let gadget = gadget_weak.upgrade().unwrap();
222//! println!("Gadget {} owned by {}", gadget.id, gadget.owner.name);
223//! }
224//!
225//! // At the end of the function, `gadget_owner`, `gadget1`, and `gadget2`
226//! // are destroyed. There are now no strong (`Rc`) pointers to the
227//! // gadgets, so they are destroyed. This zeroes the reference count on
228//! // Gadget Man, so he gets destroyed as well.
229//! }
230//! ```
231//!
232//! [clone]: Clone::clone
233//! [`Cell`]: core::cell::Cell
234//! [`RefCell`]: core::cell::RefCell
235//! [arc]: crate::sync::Arc
236//! [`Deref`]: core::ops::Deref
237//! [downgrade]: Rc::downgrade
238//! [upgrade]: Weak::upgrade
239//! [mutability]: core::cell#introducing-mutability-inside-of-something-immutable
240//! [fully qualified syntax]: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name
241
242#![stable(feature = "rust1", since = "1.0.0")]
243
244use core::any::Any;
245use core::cell::{Cell, CloneFromCell};
246#[cfg(not(no_global_oom_handling))]
247use core::clone::TrivialClone;
248use core::clone::{CloneToUninit, Share, UseCloned};
249use core::cmp::Ordering;
250use core::hash::{Hash, Hasher};
251#[cfg(not(no_global_oom_handling))]
252use core::iter;
253use core::marker::{PhantomData, Unsize};
254use core::mem::{self, Alignment, ManuallyDrop};
255use core::num::NonZeroUsize;
256use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, LegacyReceiver};
257#[cfg(not(no_global_oom_handling))]
258use core::ops::{Residual, Try};
259use core::panic::{RefUnwindSafe, UnwindSafe};
260#[cfg(not(no_global_oom_handling))]
261use core::pin::Pin;
262use core::pin::PinSafePointer;
263use core::ptr::{self, NonNull, drop_in_place};
264#[cfg(not(no_global_oom_handling))]
265use core::slice::from_raw_parts_mut;
266use core::{borrow, fmt, hint, intrinsics};
267
268#[cfg(not(no_global_oom_handling))]
269use crate::alloc::handle_alloc_error;
270use crate::alloc::{
271 AllocError, Allocator, AllocatorClone, AllocatorNightly, Global, Layout, StaticAllocator,
272};
273use crate::borrow::{Cow, ToOwned};
274use crate::boxed::Box;
275#[cfg(not(no_global_oom_handling))]
276use crate::string::String;
277#[cfg(not(no_global_oom_handling))]
278use crate::vec::Vec;
279
280// This is repr(C) to future-proof against possible field-reordering, which
281// would interfere with otherwise safe [into|from]_raw() of transmutable
282// inner types.
283// repr(align(2)) (forcing alignment to at least 2) is required because usize
284// has 1-byte alignment on AVR.
285#[repr(C, align(2))]
286struct RcInner<#[rustc_assert_variance(covariant)] T: ?Sized> {
287 strong: Cell<usize>,
288 weak: Cell<usize>,
289 value: T,
290}
291
292/// Calculate layout for `RcInner<T>` using the inner value's layout
293fn rc_inner_layout_for_value_layout(layout: Layout) -> Layout {
294 // Calculate layout using the given value layout.
295 // Previously, layout was calculated on the expression
296 // `&*(ptr as *const RcInner<T>)`, but this created a misaligned
297 // reference (see #54908).
298 Layout::new::<RcInner<()>>()
299 .extend(layout)
300 .unwrap_or_else(|_| panic!("capacity overflow"))
301 .0
302 .pad_to_align()
303}
304
305/// A single-threaded reference-counting pointer. 'Rc' stands for 'Reference
306/// Counted'.
307///
308/// See the [module-level documentation](./index.html) for more details.
309///
310/// The inherent methods of `Rc` are all associated functions, which means
311/// that you have to call them as e.g., [`Rc::get_mut(&mut value)`][get_mut] instead of
312/// `value.get_mut()`. This avoids conflicts with methods of the inner type `T`.
313///
314/// [get_mut]: Rc::get_mut
315#[doc(search_unbox)]
316#[rustc_diagnostic_item = "Rc"]
317#[stable(feature = "rust1", since = "1.0.0")]
318#[rustc_insignificant_dtor]
319#[diagnostic::on_move(
320 message = "the type `{Self}` does not implement `Copy`",
321 label = "this move could be avoided by cloning the original `{Self}`, which is inexpensive",
322 note = "consider using `Rc::clone`"
323)]
324
325pub struct Rc<
326 #[rustc_assert_variance(covariant)] T: ?Sized,
327 // FIXME: When stabilizing this parameter, the `AllocatorNightly`
328 // bound on the `From<Box>` impl *must* be weaked to `Allocator`,
329 // as `Box` is fundamental!
330 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")] A: Allocator = Global,
331> {
332 ptr: NonNull<RcInner<T>>,
333 phantom: PhantomData<RcInner<T>>,
334 alloc: A,
335}
336
337#[stable(feature = "rust1", since = "1.0.0")]
338impl<T: ?Sized, A: Allocator> !Send for Rc<T, A> {}
339
340// Note that this negative impl isn't strictly necessary for correctness,
341// as `Rc` transitively contains a `Cell`, which is itself `!Sync`.
342// However, given how important `Rc`'s `!Sync`-ness is,
343// having an explicit negative impl is nice for documentation purposes
344// and results in nicer error messages.
345#[stable(feature = "rust1", since = "1.0.0")]
346impl<T: ?Sized, A: Allocator> !Sync for Rc<T, A> {}
347
348#[stable(feature = "catch_unwind", since = "1.9.0")]
349impl<T: RefUnwindSafe + ?Sized, A: Allocator + UnwindSafe + RefUnwindSafe> UnwindSafe for Rc<T, A> {}
350#[stable(feature = "rc_ref_unwind_safe", since = "1.58.0")]
351impl<T: RefUnwindSafe + ?Sized, A: Allocator + RefUnwindSafe> RefUnwindSafe for Rc<T, A> {}
352
353#[unstable(feature = "coerce_unsized", issue = "18598")]
354impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Rc<U, A>> for Rc<T, A> {}
355
356#[unstable(feature = "dispatch_from_dyn", issue = "none")]
357impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Rc<U>> for Rc<T> {}
358
359// SAFETY: `Rc::clone` doesn't access any `Cell`s which could contain the `Rc` being cloned.
360#[unstable(feature = "cell_get_cloned", issue = "145329")]
361unsafe impl<T: ?Sized> CloneFromCell for Rc<T> {}
362
363impl<T: ?Sized> Rc<T> {
364 #[inline]
365 unsafe fn from_inner(ptr: NonNull<RcInner<T>>) -> Self {
366 // SAFETY: Upheld by caller.
367 unsafe { Self::from_inner_in(ptr, Global) }
368 }
369
370 #[inline]
371 unsafe fn from_ptr(ptr: *mut RcInner<T>) -> Self {
372 // SAFETY: Upheld by caller.
373 unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) }
374 }
375}
376
377impl<T: ?Sized, A: Allocator> Rc<T, A> {
378 #[inline(always)]
379 fn inner(&self) -> &RcInner<T> {
380 // SAFETY: While this Rc is alive we're guaranteed
381 // that the inner pointer is valid.
382 unsafe { self.ptr.as_ref() }
383 }
384
385 #[inline]
386 fn into_inner_with_allocator(this: Self) -> (NonNull<RcInner<T>>, A) {
387 let this = mem::ManuallyDrop::new(this);
388 // SAFETY: Pulling out the allocator we already own.
389 (this.ptr, unsafe { ptr::read(&this.alloc) })
390 }
391
392 #[inline]
393 unsafe fn from_inner_in(ptr: NonNull<RcInner<T>>, alloc: A) -> Self {
394 Self { ptr, phantom: PhantomData, alloc }
395 }
396
397 #[inline]
398 unsafe fn from_ptr_in(ptr: *mut RcInner<T>, alloc: A) -> Self {
399 // SAFETY: Upheld by caller.
400 unsafe { Self::from_inner_in(NonNull::new_unchecked(ptr), alloc) }
401 }
402
403 // Non-inlined part of `drop`.
404 #[inline(never)]
405 unsafe fn drop_slow(&mut self) {
406 // Reconstruct the "strong weak" pointer and drop it when this
407 // variable goes out of scope. This ensures that the memory is
408 // deallocated even if the destructor of `T` panics.
409 let _weak = Weak { ptr: self.ptr, alloc: &self.alloc };
410
411 // Destroy the contained object.
412 // We cannot use `get_mut_unchecked` here, because `self.alloc` is borrowed.
413 // SAFETY: `self.ptr` is *not* borrowed.
414 unsafe {
415 ptr::drop_in_place(&mut (*self.ptr.as_ptr()).value);
416 }
417 }
418}
419
420impl<T> Rc<T> {
421 /// Constructs a new `Rc<T>`.
422 ///
423 /// # Examples
424 ///
425 /// ```
426 /// use std::rc::Rc;
427 ///
428 /// let five = Rc::new(5);
429 /// ```
430 #[cfg(not(no_global_oom_handling))]
431 #[stable(feature = "rust1", since = "1.0.0")]
432 pub fn new(value: T) -> Rc<T> {
433 // SAFETY: There is an implicit weak pointer owned by all the strong
434 // pointers, which ensures that the weak destructor never frees
435 // the allocation while the strong destructor is running, even
436 // if the weak pointer is stored inside the strong one.
437 unsafe {
438 Self::from_inner(Box::into_non_null(Box::new(RcInner {
439 strong: Cell::new(1),
440 weak: Cell::new(1),
441 value,
442 })))
443 }
444 }
445
446 /// Constructs a new `Rc<T>` while giving you a `Weak<T>` to the allocation,
447 /// to allow you to construct a `T` which holds a weak pointer to itself.
448 ///
449 /// Generally, a structure circularly referencing itself, either directly or
450 /// indirectly, should not hold a strong reference to itself to prevent a memory leak.
451 /// Using this function, you get access to the weak pointer during the
452 /// initialization of `T`, before the `Rc<T>` is created, such that you can
453 /// clone and store it inside the `T`.
454 ///
455 /// `new_cyclic` first allocates the managed allocation for the `Rc<T>`,
456 /// then calls your closure, giving it a `Weak<T>` to this allocation,
457 /// and only afterwards completes the construction of the `Rc<T>` by placing
458 /// the `T` returned from your closure into the allocation.
459 ///
460 /// Since the new `Rc<T>` is not fully-constructed until `Rc<T>::new_cyclic`
461 /// returns, calling [`upgrade`] on the weak reference inside your closure will
462 /// fail and result in a `None` value.
463 ///
464 /// # Panics
465 ///
466 /// If `data_fn` panics, the panic is propagated to the caller, and the
467 /// temporary [`Weak<T>`] is dropped normally.
468 ///
469 /// # Examples
470 ///
471 /// ```
472 /// # #![allow(dead_code)]
473 /// use std::rc::{Rc, Weak};
474 ///
475 /// struct Gadget {
476 /// me: Weak<Gadget>,
477 /// }
478 ///
479 /// impl Gadget {
480 /// /// Constructs a reference counted Gadget.
481 /// fn new() -> Rc<Self> {
482 /// // `me` is a `Weak<Gadget>` pointing at the new allocation of the
483 /// // `Rc` we're constructing.
484 /// Rc::new_cyclic(|me| {
485 /// // Create the actual struct here.
486 /// Gadget { me: me.clone() }
487 /// })
488 /// }
489 ///
490 /// /// Returns a reference counted pointer to Self.
491 /// fn me(&self) -> Rc<Self> {
492 /// self.me.upgrade().unwrap()
493 /// }
494 /// }
495 /// ```
496 /// [`upgrade`]: Weak::upgrade
497 #[cfg(not(no_global_oom_handling))]
498 #[stable(feature = "arc_new_cyclic", since = "1.60.0")]
499 pub fn new_cyclic<F>(data_fn: F) -> Rc<T>
500 where
501 F: FnOnce(&Weak<T>) -> T,
502 {
503 Self::new_cyclic_in(data_fn, Global)
504 }
505
506 /// Constructs a new `Rc` with uninitialized contents.
507 ///
508 /// # Examples
509 ///
510 /// ```
511 /// use std::rc::Rc;
512 ///
513 /// let mut five = Rc::<u32>::new_uninit();
514 ///
515 /// // Deferred initialization:
516 /// Rc::get_mut(&mut five).unwrap().write(5);
517 ///
518 /// let five = unsafe { five.assume_init() };
519 ///
520 /// assert_eq!(*five, 5)
521 /// ```
522 #[cfg(not(no_global_oom_handling))]
523 #[stable(feature = "new_uninit", since = "1.82.0")]
524 #[must_use]
525 pub fn new_uninit() -> Rc<mem::MaybeUninit<T>> {
526 // ignore-tidy-undocumented-unsafe
527 unsafe {
528 Rc::from_ptr(Rc::allocate_for_layout(
529 Layout::new::<T>(),
530 |layout| Global.allocate(layout),
531 <*mut u8>::cast,
532 ))
533 }
534 }
535
536 /// Constructs a new `Rc` with uninitialized contents, with the memory
537 /// being filled with `0` bytes.
538 ///
539 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
540 /// incorrect usage of this method.
541 ///
542 /// # Examples
543 ///
544 /// ```
545 /// use std::rc::Rc;
546 ///
547 /// let zero = Rc::<u32>::new_zeroed();
548 /// let zero = unsafe { zero.assume_init() };
549 ///
550 /// assert_eq!(*zero, 0)
551 /// ```
552 ///
553 /// [zeroed]: mem::MaybeUninit::zeroed
554 #[cfg(not(no_global_oom_handling))]
555 #[stable(feature = "new_zeroed_alloc", since = "1.92.0")]
556 #[must_use]
557 pub fn new_zeroed() -> Rc<mem::MaybeUninit<T>> {
558 // ignore-tidy-undocumented-unsafe
559 unsafe {
560 Rc::from_ptr(Rc::allocate_for_layout(
561 Layout::new::<T>(),
562 |layout| Global.allocate_zeroed(layout),
563 <*mut u8>::cast,
564 ))
565 }
566 }
567
568 /// Constructs a new `Rc<T>`, returning an error if the allocation fails
569 ///
570 /// # Examples
571 ///
572 /// ```
573 /// #![feature(allocator_ext)]
574 /// use std::rc::Rc;
575 ///
576 /// let five = Rc::try_new(5);
577 /// # Ok::<(), std::alloc::AllocError>(())
578 /// ```
579 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
580 pub fn try_new(value: T) -> Result<Rc<T>, AllocError> {
581 // SAFETY: There is an implicit weak pointer owned by all the strong
582 // pointers, which ensures that the weak destructor never frees
583 // the allocation while the strong destructor is running, even
584 // if the weak pointer is stored inside the strong one.
585 unsafe {
586 Ok(Self::from_inner(Box::into_non_null(Box::try_new(RcInner {
587 strong: Cell::new(1),
588 weak: Cell::new(1),
589 value,
590 })?)))
591 }
592 }
593
594 /// Constructs a new `Rc` with uninitialized contents, returning an error if the allocation fails
595 ///
596 /// # Examples
597 ///
598 /// ```
599 /// #![feature(allocator_ext)]
600 ///
601 /// use std::rc::Rc;
602 ///
603 /// let mut five = Rc::<u32>::try_new_uninit()?;
604 ///
605 /// // Deferred initialization:
606 /// Rc::get_mut(&mut five).unwrap().write(5);
607 ///
608 /// let five = unsafe { five.assume_init() };
609 ///
610 /// assert_eq!(*five, 5);
611 /// # Ok::<(), std::alloc::AllocError>(())
612 /// ```
613 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
614 pub fn try_new_uninit() -> Result<Rc<mem::MaybeUninit<T>>, AllocError> {
615 // ignore-tidy-undocumented-unsafe
616 unsafe {
617 Ok(Rc::from_ptr(Rc::try_allocate_for_layout(
618 Layout::new::<T>(),
619 |layout| Global.allocate(layout),
620 <*mut u8>::cast,
621 )?))
622 }
623 }
624
625 /// Constructs a new `Rc` with uninitialized contents, with the memory
626 /// being filled with `0` bytes, returning an error if the allocation fails
627 ///
628 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
629 /// incorrect usage of this method.
630 ///
631 /// # Examples
632 ///
633 /// ```
634 /// #![feature(allocator_ext)]
635 ///
636 /// use std::rc::Rc;
637 ///
638 /// let zero = Rc::<u32>::try_new_zeroed()?;
639 /// let zero = unsafe { zero.assume_init() };
640 ///
641 /// assert_eq!(*zero, 0);
642 /// # Ok::<(), std::alloc::AllocError>(())
643 /// ```
644 ///
645 /// [zeroed]: mem::MaybeUninit::zeroed
646 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
647 pub fn try_new_zeroed() -> Result<Rc<mem::MaybeUninit<T>>, AllocError> {
648 // ignore-tidy-undocumented-unsafe
649 unsafe {
650 Ok(Rc::from_ptr(Rc::try_allocate_for_layout(
651 Layout::new::<T>(),
652 |layout| Global.allocate_zeroed(layout),
653 <*mut u8>::cast,
654 )?))
655 }
656 }
657 /// Constructs a new `Pin<Rc<T>>`. If `T` does not implement `Unpin`, then
658 /// `value` will be pinned in memory and unable to be moved.
659 #[cfg(not(no_global_oom_handling))]
660 #[stable(feature = "pin", since = "1.33.0")]
661 #[must_use]
662 pub fn pin(value: T) -> Pin<Rc<T>> {
663 // SAFETY: We own and create the pinned pointer.
664 unsafe { Pin::new_unchecked(Rc::new(value)) }
665 }
666}
667
668impl<T, A: Allocator> Rc<T, A> {
669 /// Constructs a new `Rc` in the provided allocator.
670 ///
671 /// # Examples
672 ///
673 /// ```
674 /// #![feature(allocator_ext)]
675 ///
676 /// use std::rc::Rc;
677 /// use std::alloc::System;
678 ///
679 /// let five = Rc::new_in(5, System);
680 /// ```
681 #[cfg(not(no_global_oom_handling))]
682 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
683 #[inline]
684 pub fn new_in(value: T, alloc: A) -> Rc<T, A> {
685 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
686 // That would make code size bigger.
687 match Self::try_new_in(value, alloc) {
688 Ok(m) => m,
689 Err(_) => handle_alloc_error(Layout::new::<RcInner<T>>()),
690 }
691 }
692
693 /// Constructs a new `Rc` with uninitialized contents in the provided allocator.
694 ///
695 /// # Examples
696 ///
697 /// ```
698 /// #![feature(get_mut_unchecked)]
699 /// #![feature(allocator_ext)]
700 ///
701 /// use std::rc::Rc;
702 /// use std::alloc::System;
703 ///
704 /// let mut five = Rc::<u32, _>::new_uninit_in(System);
705 ///
706 /// let five = unsafe {
707 /// // Deferred initialization:
708 /// Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
709 ///
710 /// five.assume_init()
711 /// };
712 ///
713 /// assert_eq!(*five, 5)
714 /// ```
715 #[cfg(not(no_global_oom_handling))]
716 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
717 #[inline]
718 pub fn new_uninit_in(alloc: A) -> Rc<mem::MaybeUninit<T>, A> {
719 // ignore-tidy-undocumented-unsafe
720 unsafe {
721 Rc::from_ptr_in(
722 Rc::allocate_for_layout(
723 Layout::new::<T>(),
724 |layout| alloc.allocate(layout),
725 <*mut u8>::cast,
726 ),
727 alloc,
728 )
729 }
730 }
731
732 /// Constructs a new `Rc` with uninitialized contents, with the memory
733 /// being filled with `0` bytes, in the provided allocator.
734 ///
735 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
736 /// incorrect usage of this method.
737 ///
738 /// # Examples
739 ///
740 /// ```
741 /// #![feature(allocator_ext)]
742 ///
743 /// use std::rc::Rc;
744 /// use std::alloc::System;
745 ///
746 /// let zero = Rc::<u32, _>::new_zeroed_in(System);
747 /// let zero = unsafe { zero.assume_init() };
748 ///
749 /// assert_eq!(*zero, 0)
750 /// ```
751 ///
752 /// [zeroed]: mem::MaybeUninit::zeroed
753 #[cfg(not(no_global_oom_handling))]
754 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
755 #[inline]
756 pub fn new_zeroed_in(alloc: A) -> Rc<mem::MaybeUninit<T>, A> {
757 // ignore-tidy-undocumented-unsafe
758 unsafe {
759 Rc::from_ptr_in(
760 Rc::allocate_for_layout(
761 Layout::new::<T>(),
762 |layout| alloc.allocate_zeroed(layout),
763 <*mut u8>::cast,
764 ),
765 alloc,
766 )
767 }
768 }
769
770 /// Constructs a new `Rc<T, A>` in the given allocator while giving you a `Weak<T, A>` to the allocation,
771 /// to allow you to construct a `T` which holds a weak pointer to itself.
772 ///
773 /// Generally, a structure circularly referencing itself, either directly or
774 /// indirectly, should not hold a strong reference to itself to prevent a memory leak.
775 /// Using this function, you get access to the weak pointer during the
776 /// initialization of `T`, before the `Rc<T, A>` is created, such that you can
777 /// clone and store it inside the `T`.
778 ///
779 /// `new_cyclic_in` first allocates the managed allocation for the `Rc<T, A>`,
780 /// then calls your closure, giving it a `Weak<T, A>` to this allocation,
781 /// and only afterwards completes the construction of the `Rc<T, A>` by placing
782 /// the `T` returned from your closure into the allocation.
783 ///
784 /// Since the new `Rc<T, A>` is not fully-constructed until `Rc<T, A>::new_cyclic_in`
785 /// returns, calling [`upgrade`] on the weak reference inside your closure will
786 /// fail and result in a `None` value.
787 ///
788 /// # Panics
789 ///
790 /// If `data_fn` panics, the panic is propagated to the caller, and the
791 /// temporary [`Weak<T, A>`] is dropped normally.
792 ///
793 /// # Examples
794 ///
795 /// See [`new_cyclic`].
796 ///
797 /// [`new_cyclic`]: Rc::new_cyclic
798 /// [`upgrade`]: Weak::upgrade
799 #[cfg(not(no_global_oom_handling))]
800 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
801 pub fn new_cyclic_in<F>(data_fn: F, alloc: A) -> Rc<T, A>
802 where
803 F: FnOnce(&Weak<T, A>) -> T,
804 {
805 // Construct the inner in the "uninitialized" state with a single
806 // weak reference.
807 let (uninit_ptr, alloc) = Box::into_non_null_with_allocator(Box::new_in(
808 RcInner {
809 strong: Cell::new(0),
810 weak: Cell::new(1),
811 value: mem::MaybeUninit::<T>::uninit(),
812 },
813 alloc,
814 ));
815 let init_ptr: NonNull<RcInner<T>> = uninit_ptr.cast();
816
817 let weak = Weak { ptr: init_ptr, alloc };
818
819 // It's important we don't give up ownership of the weak pointer, or
820 // else the memory might be freed by the time `data_fn` returns. If
821 // we really wanted to pass ownership, we could create an additional
822 // weak pointer for ourselves, but this would result in additional
823 // updates to the weak reference count which might not be necessary
824 // otherwise.
825 let data = data_fn(&weak);
826
827 // ignore-tidy-undocumented-unsafe
828 unsafe {
829 let inner = init_ptr.as_ptr();
830 ptr::write(&raw mut (*inner).value, data);
831
832 let prev_value = (*inner).strong.get();
833 debug_assert_eq!(prev_value, 0, "No prior strong references should exist");
834 (*inner).strong.set(1);
835
836 // Strong references should collectively own a shared weak reference,
837 // so don't run the destructor for our old weak reference.
838 // Calling into_raw_with_allocator has the double effect of giving us back the allocator,
839 // and forgetting the weak reference.
840 let alloc = weak.into_raw_with_allocator().1;
841
842 Rc::from_inner_in(init_ptr, alloc)
843 }
844 }
845
846 /// Constructs a new `Rc<T>` in the provided allocator, returning an error if the allocation
847 /// fails
848 ///
849 /// # Examples
850 ///
851 /// ```
852 /// #![feature(allocator_ext)]
853 /// use std::rc::Rc;
854 /// use std::alloc::System;
855 ///
856 /// let five = Rc::try_new_in(5, System);
857 /// # Ok::<(), std::alloc::AllocError>(())
858 /// ```
859 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
860 #[inline]
861 pub fn try_new_in(value: T, alloc: A) -> Result<Self, AllocError> {
862 // There is an implicit weak pointer owned by all the strong
863 // pointers, which ensures that the weak destructor never frees
864 // the allocation while the strong destructor is running, even
865 // if the weak pointer is stored inside the strong one.
866 let (ptr, alloc) = Box::into_non_null_with_allocator(Box::try_new_in(
867 RcInner { strong: Cell::new(1), weak: Cell::new(1), value },
868 alloc,
869 )?);
870 // SAFETY: Pointer is valid.
871 Ok(unsafe { Self::from_inner_in(ptr, alloc) })
872 }
873
874 /// Constructs a new `Rc` with uninitialized contents, in the provided allocator, returning an
875 /// error if the allocation fails
876 ///
877 /// # Examples
878 ///
879 /// ```
880 /// #![feature(allocator_ext)]
881 /// #![feature(get_mut_unchecked)]
882 ///
883 /// use std::rc::Rc;
884 /// use std::alloc::System;
885 ///
886 /// let mut five = Rc::<u32, _>::try_new_uninit_in(System)?;
887 ///
888 /// let five = unsafe {
889 /// // Deferred initialization:
890 /// Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
891 ///
892 /// five.assume_init()
893 /// };
894 ///
895 /// assert_eq!(*five, 5);
896 /// # Ok::<(), std::alloc::AllocError>(())
897 /// ```
898 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
899 #[inline]
900 pub fn try_new_uninit_in(alloc: A) -> Result<Rc<mem::MaybeUninit<T>, A>, AllocError> {
901 // ignore-tidy-undocumented-unsafe
902 unsafe {
903 Ok(Rc::from_ptr_in(
904 Rc::try_allocate_for_layout(
905 Layout::new::<T>(),
906 |layout| alloc.allocate(layout),
907 <*mut u8>::cast,
908 )?,
909 alloc,
910 ))
911 }
912 }
913
914 /// Constructs a new `Rc` with uninitialized contents, with the memory
915 /// being filled with `0` bytes, in the provided allocator, returning an error if the allocation
916 /// fails
917 ///
918 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
919 /// incorrect usage of this method.
920 ///
921 /// # Examples
922 ///
923 /// ```
924 /// #![feature(allocator_ext)]
925 ///
926 /// use std::rc::Rc;
927 /// use std::alloc::System;
928 ///
929 /// let zero = Rc::<u32, _>::try_new_zeroed_in(System)?;
930 /// let zero = unsafe { zero.assume_init() };
931 ///
932 /// assert_eq!(*zero, 0);
933 /// # Ok::<(), std::alloc::AllocError>(())
934 /// ```
935 ///
936 /// [zeroed]: mem::MaybeUninit::zeroed
937 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
938 #[inline]
939 pub fn try_new_zeroed_in(alloc: A) -> Result<Rc<mem::MaybeUninit<T>, A>, AllocError> {
940 // ignore-tidy-undocumented-unsafe
941 unsafe {
942 Ok(Rc::from_ptr_in(
943 Rc::try_allocate_for_layout(
944 Layout::new::<T>(),
945 |layout| alloc.allocate_zeroed(layout),
946 <*mut u8>::cast,
947 )?,
948 alloc,
949 ))
950 }
951 }
952
953 /// Constructs a new `Pin<Rc<T>>` in the provided allocator. If `T` does not implement `Unpin`, then
954 /// `value` will be pinned in memory and unable to be moved.
955 #[cfg(not(no_global_oom_handling))]
956 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
957 #[inline]
958 pub fn pin_in(value: T, alloc: A) -> Pin<Self>
959 where
960 A: StaticAllocator,
961 {
962 // SAFETY: We own and create the pinned pointer.
963 unsafe { Pin::new_unchecked(Rc::new_in(value, alloc)) }
964 }
965
966 /// Returns the inner value, if the `Rc` has exactly one strong reference.
967 ///
968 /// Otherwise, an [`Err`] is returned with the same `Rc` that was
969 /// passed in.
970 ///
971 /// This will succeed even if there are outstanding weak references.
972 ///
973 /// # Examples
974 ///
975 /// ```
976 /// use std::rc::Rc;
977 ///
978 /// let x = Rc::new(3);
979 /// assert_eq!(Rc::try_unwrap(x), Ok(3));
980 ///
981 /// let x = Rc::new(4);
982 /// let _y = Rc::clone(&x);
983 /// assert_eq!(*Rc::try_unwrap(x).unwrap_err(), 4);
984 /// ```
985 #[inline]
986 #[stable(feature = "rc_unique", since = "1.4.0")]
987 pub fn try_unwrap(this: Self) -> Result<T, Self> {
988 if Rc::strong_count(&this) == 1 {
989 let this = ManuallyDrop::new(this);
990
991 // ignore-tidy-undocumented-unsafe
992 let val: T = unsafe { ptr::read(&**this) }; // copy the contained object
993 // ignore-tidy-undocumented-unsafe
994 let alloc: A = unsafe { ptr::read(&this.alloc) }; // copy the allocator
995
996 // Indicate to Weaks that they can't be promoted by decrementing
997 // the strong count, and then remove the implicit "strong weak"
998 // pointer while also handling drop logic by just crafting a
999 // fake Weak.
1000 this.inner().dec_strong();
1001 let _weak = Weak { ptr: this.ptr, alloc };
1002 Ok(val)
1003 } else {
1004 Err(this)
1005 }
1006 }
1007
1008 /// Returns the inner value, if the `Rc` has exactly one strong reference.
1009 ///
1010 /// Otherwise, [`None`] is returned and the `Rc` is dropped.
1011 ///
1012 /// This will succeed even if there are outstanding weak references.
1013 ///
1014 /// If `Rc::into_inner` is called on every clone of this `Rc`,
1015 /// it is guaranteed that exactly one of the calls returns the inner value.
1016 /// This means in particular that the inner value is not dropped.
1017 ///
1018 /// [`Rc::try_unwrap`] is conceptually similar to `Rc::into_inner`.
1019 /// And while they are meant for different use-cases, `Rc::into_inner(this)`
1020 /// is in fact equivalent to <code>[Rc::try_unwrap]\(this).[ok][Result::ok]()</code>.
1021 /// (Note that the same kind of equivalence does **not** hold true for
1022 /// [`Arc`](crate::sync::Arc), due to race conditions that do not apply to `Rc`!)
1023 ///
1024 /// # Examples
1025 ///
1026 /// ```
1027 /// use std::rc::Rc;
1028 ///
1029 /// let x = Rc::new(3);
1030 /// assert_eq!(Rc::into_inner(x), Some(3));
1031 ///
1032 /// let x = Rc::new(4);
1033 /// let y = Rc::clone(&x);
1034 ///
1035 /// assert_eq!(Rc::into_inner(y), None);
1036 /// assert_eq!(Rc::into_inner(x), Some(4));
1037 /// ```
1038 #[inline]
1039 #[stable(feature = "rc_into_inner", since = "1.70.0")]
1040 pub fn into_inner(this: Self) -> Option<T> {
1041 Rc::try_unwrap(this).ok()
1042 }
1043
1044 /// Maps the value in an `Rc`, reusing the allocation if possible.
1045 ///
1046 /// `f` is called on a reference to the value in the `Rc`, and the result is returned, also in
1047 /// an `Rc`.
1048 ///
1049 /// Note: this is an associated function, which means that you have
1050 /// to call it as `Rc::map(r, f)` instead of `r.map(f)`. This
1051 /// is so that there is no conflict with a method on the inner type.
1052 ///
1053 /// # Examples
1054 ///
1055 /// ```
1056 /// use std::rc::Rc;
1057 ///
1058 /// let r = Rc::new(7);
1059 /// let new = Rc::map(r, |i| i + 7);
1060 /// assert_eq!(*new, 14);
1061 /// ```
1062 #[cfg(not(no_global_oom_handling))]
1063 #[stable(feature = "smart_pointer_map", since = "1.100.0")]
1064 pub fn map<U>(this: Self, f: impl FnOnce(&T) -> U) -> Rc<U, A> {
1065 if size_of::<T>() == size_of::<U>()
1066 && align_of::<T>() == align_of::<U>()
1067 && Rc::is_unique(&this)
1068 {
1069 // ignore-tidy-undocumented-unsafe
1070 unsafe {
1071 let (ptr, alloc) = Rc::into_raw_with_allocator(this);
1072 let value = ptr.read();
1073 let mut allocation = Rc::from_raw_in(ptr.cast::<mem::MaybeUninit<U>>(), alloc);
1074
1075 Rc::get_mut_unchecked(&mut allocation).write(f(&value));
1076 allocation.assume_init()
1077 }
1078 } else {
1079 let output = f(&*this);
1080 let (ptr, alloc) = Rc::into_raw_with_allocator(this);
1081 // ignore-tidy-undocumented-unsafe
1082 unsafe { Rc::decrement_strong_count_in(ptr, &alloc) }
1083
1084 Rc::new_in(output, alloc)
1085 }
1086 }
1087
1088 /// Attempts to map the value in an `Rc`, reusing the allocation if possible.
1089 ///
1090 /// `f` is called on a reference to the value in the `Rc`, and if the operation succeeds, the
1091 /// result is returned, also in an `Rc`.
1092 ///
1093 /// Note: this is an associated function, which means that you have
1094 /// to call it as `Rc::try_map(r, f)` instead of `r.try_map(f)`. This
1095 /// is so that there is no conflict with a method on the inner type.
1096 ///
1097 /// # Examples
1098 ///
1099 /// ```
1100 /// #![feature(smart_pointer_try_map)]
1101 ///
1102 /// use std::rc::Rc;
1103 ///
1104 /// let b = Rc::new(7);
1105 /// let new = Rc::try_map(b, |&i| u32::try_from(i)).unwrap();
1106 /// assert_eq!(*new, 7);
1107 /// ```
1108 #[cfg(not(no_global_oom_handling))]
1109 #[unstable(feature = "smart_pointer_try_map", issue = "144419")]
1110 pub fn try_map<R>(
1111 this: Self,
1112 f: impl FnOnce(&T) -> R,
1113 ) -> <R::Residual as Residual<Rc<R::Output, A>>>::TryType
1114 where
1115 R: Try,
1116 R::Residual: Residual<Rc<R::Output, A>>,
1117 {
1118 if size_of::<T>() == size_of::<R::Output>()
1119 && align_of::<T>() == align_of::<R::Output>()
1120 && Rc::is_unique(&this)
1121 {
1122 // ignore-tidy-undocumented-unsafe
1123 unsafe {
1124 let (ptr, alloc) = Rc::into_raw_with_allocator(this);
1125 let value = ptr.read();
1126 let mut allocation =
1127 Rc::from_raw_in(ptr.cast::<mem::MaybeUninit<R::Output>>(), alloc);
1128
1129 Rc::get_mut_unchecked(&mut allocation).write(f(&value)?);
1130 try { allocation.assume_init() }
1131 }
1132 } else {
1133 let output = f(&*this)?;
1134 let (ptr, alloc) = Rc::into_raw_with_allocator(this);
1135 // ignore-tidy-undocumented-unsafe
1136 unsafe { Rc::decrement_strong_count_in(ptr, &alloc) }
1137
1138 try { Rc::new_in(output, alloc) }
1139 }
1140 }
1141}
1142
1143impl<T> Rc<[T]> {
1144 /// Constructs a new reference-counted slice with uninitialized contents.
1145 ///
1146 /// # Examples
1147 ///
1148 /// ```
1149 /// use std::rc::Rc;
1150 ///
1151 /// let mut values = Rc::<[u32]>::new_uninit_slice(3);
1152 ///
1153 /// // Deferred initialization:
1154 /// let data = Rc::get_mut(&mut values).unwrap();
1155 /// data[0].write(1);
1156 /// data[1].write(2);
1157 /// data[2].write(3);
1158 ///
1159 /// let values = unsafe { values.assume_init() };
1160 ///
1161 /// assert_eq!(*values, [1, 2, 3])
1162 /// ```
1163 #[cfg(not(no_global_oom_handling))]
1164 #[stable(feature = "new_uninit", since = "1.82.0")]
1165 #[must_use]
1166 pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
1167 // ignore-tidy-undocumented-unsafe
1168 unsafe { Rc::from_ptr(Rc::allocate_for_slice(len)) }
1169 }
1170
1171 /// Constructs a new reference-counted slice with uninitialized contents, with the memory being
1172 /// filled with `0` bytes.
1173 ///
1174 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
1175 /// incorrect usage of this method.
1176 ///
1177 /// # Examples
1178 ///
1179 /// ```
1180 /// use std::rc::Rc;
1181 ///
1182 /// let values = Rc::<[u32]>::new_zeroed_slice(3);
1183 /// let values = unsafe { values.assume_init() };
1184 ///
1185 /// assert_eq!(*values, [0, 0, 0])
1186 /// ```
1187 ///
1188 /// [zeroed]: mem::MaybeUninit::zeroed
1189 #[cfg(not(no_global_oom_handling))]
1190 #[stable(feature = "new_zeroed_alloc", since = "1.92.0")]
1191 #[must_use]
1192 pub fn new_zeroed_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
1193 // ignore-tidy-undocumented-unsafe
1194 unsafe {
1195 Rc::from_ptr(Rc::allocate_for_layout(
1196 Layout::array::<T>(len).unwrap(),
1197 |layout| Global.allocate_zeroed(layout),
1198 |mem| mem.cast::<T>().cast_slice(len) as *mut RcInner<[mem::MaybeUninit<T>]>,
1199 ))
1200 }
1201 }
1202}
1203
1204impl<T, A: Allocator> Rc<[T], A> {
1205 /// Constructs a new reference-counted slice with uninitialized contents.
1206 ///
1207 /// # Examples
1208 ///
1209 /// ```
1210 /// #![feature(get_mut_unchecked)]
1211 /// #![feature(allocator_ext)]
1212 ///
1213 /// use std::rc::Rc;
1214 /// use std::alloc::System;
1215 ///
1216 /// let mut values = Rc::<[u32], _>::new_uninit_slice_in(3, System);
1217 ///
1218 /// let values = unsafe {
1219 /// // Deferred initialization:
1220 /// Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
1221 /// Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
1222 /// Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
1223 ///
1224 /// values.assume_init()
1225 /// };
1226 ///
1227 /// assert_eq!(*values, [1, 2, 3])
1228 /// ```
1229 #[cfg(not(no_global_oom_handling))]
1230 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1231 #[inline]
1232 pub fn new_uninit_slice_in(len: usize, alloc: A) -> Rc<[mem::MaybeUninit<T>], A> {
1233 // ignore-tidy-undocumented-unsafe
1234 unsafe { Rc::from_ptr_in(Rc::allocate_for_slice_in(len, &alloc), alloc) }
1235 }
1236
1237 /// Constructs a new reference-counted slice with uninitialized contents, with the memory being
1238 /// filled with `0` bytes.
1239 ///
1240 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
1241 /// incorrect usage of this method.
1242 ///
1243 /// # Examples
1244 ///
1245 /// ```
1246 /// #![feature(allocator_ext)]
1247 ///
1248 /// use std::rc::Rc;
1249 /// use std::alloc::System;
1250 ///
1251 /// let values = Rc::<[u32], _>::new_zeroed_slice_in(3, System);
1252 /// let values = unsafe { values.assume_init() };
1253 ///
1254 /// assert_eq!(*values, [0, 0, 0])
1255 /// ```
1256 ///
1257 /// [zeroed]: mem::MaybeUninit::zeroed
1258 #[cfg(not(no_global_oom_handling))]
1259 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1260 #[inline]
1261 pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Rc<[mem::MaybeUninit<T>], A> {
1262 // ignore-tidy-undocumented-unsafe
1263 unsafe {
1264 Rc::from_ptr_in(
1265 Rc::allocate_for_layout(
1266 Layout::array::<T>(len).unwrap(),
1267 |layout| alloc.allocate_zeroed(layout),
1268 |mem| mem.cast::<T>().cast_slice(len) as *mut RcInner<[mem::MaybeUninit<T>]>,
1269 ),
1270 alloc,
1271 )
1272 }
1273 }
1274
1275 /// Converts the reference-counted slice into a reference-counted array.
1276 ///
1277 /// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
1278 ///
1279 /// # Errors
1280 ///
1281 /// Returns the original `Rc<[T]>` in the `Err` variant if `self.len()` does not equal `N`.
1282 ///
1283 /// # Examples
1284 ///
1285 /// ```
1286 /// #![feature(alloc_slice_into_array)]
1287 /// use std::rc::Rc;
1288 ///
1289 /// let rc_slice: Rc<[i32]> = Rc::new([1, 2, 3]);
1290 ///
1291 /// let rc_array: Rc<[i32; 3]> = rc_slice.into_array().unwrap();
1292 /// ```
1293 #[unstable(feature = "alloc_slice_into_array", issue = "148082")]
1294 #[inline]
1295 pub fn into_array<const N: usize>(self) -> Result<Rc<[T; N], A>, Self> {
1296 if self.len() == N {
1297 let (ptr, alloc) = Self::into_raw_with_allocator(self);
1298 let ptr = ptr as *const [T; N];
1299
1300 // SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
1301 let me = unsafe { Rc::from_raw_in(ptr, alloc) };
1302 Ok(me)
1303 } else {
1304 Err(self)
1305 }
1306 }
1307}
1308
1309impl<T, A: Allocator> Rc<mem::MaybeUninit<T>, A> {
1310 /// Converts to `Rc<T>`.
1311 ///
1312 /// # Safety
1313 ///
1314 /// As with [`MaybeUninit::assume_init`],
1315 /// it is up to the caller to guarantee that the inner value
1316 /// really is in an initialized state.
1317 /// Calling this when the content is not yet fully initialized
1318 /// causes immediate undefined behavior.
1319 ///
1320 /// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
1321 ///
1322 /// # Examples
1323 ///
1324 /// ```
1325 /// use std::rc::Rc;
1326 ///
1327 /// let mut five = Rc::<u32>::new_uninit();
1328 ///
1329 /// // Deferred initialization:
1330 /// Rc::get_mut(&mut five).unwrap().write(5);
1331 ///
1332 /// let five = unsafe { five.assume_init() };
1333 ///
1334 /// assert_eq!(*five, 5)
1335 /// ```
1336 #[stable(feature = "new_uninit", since = "1.82.0")]
1337 #[inline]
1338 pub unsafe fn assume_init(self) -> Rc<T, A> {
1339 let (ptr, alloc) = Rc::into_inner_with_allocator(self);
1340 // ignore-tidy-undocumented-unsafe
1341 unsafe { Rc::from_inner_in(ptr.cast(), alloc) }
1342 }
1343}
1344
1345impl<T: ?Sized + CloneToUninit> Rc<T> {
1346 /// Constructs a new `Rc<T>` with a clone of `value`.
1347 ///
1348 /// # Examples
1349 ///
1350 /// ```
1351 /// #![feature(clone_from_ref)]
1352 /// use std::rc::Rc;
1353 ///
1354 /// let hello: Rc<str> = Rc::clone_from_ref("hello");
1355 /// ```
1356 #[cfg(not(no_global_oom_handling))]
1357 #[unstable(feature = "clone_from_ref", issue = "149075")]
1358 pub fn clone_from_ref(value: &T) -> Rc<T> {
1359 Rc::clone_from_ref_in(value, Global)
1360 }
1361
1362 /// Constructs a new `Rc<T>` with a clone of `value`, returning an error if allocation fails
1363 ///
1364 /// # Examples
1365 ///
1366 /// ```
1367 /// #![feature(clone_from_ref)]
1368 /// use std::rc::Rc;
1369 ///
1370 /// let hello: Rc<str> = Rc::try_clone_from_ref("hello")?;
1371 /// # Ok::<(), std::alloc::AllocError>(())
1372 /// ```
1373 #[unstable(feature = "clone_from_ref", issue = "149075")]
1374 //#[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1375 pub fn try_clone_from_ref(value: &T) -> Result<Rc<T>, AllocError> {
1376 Rc::try_clone_from_ref_in(value, Global)
1377 }
1378}
1379
1380impl<T: ?Sized + CloneToUninit, A: Allocator> Rc<T, A> {
1381 /// Constructs a new `Rc<T>` with a clone of `value` in the provided allocator.
1382 ///
1383 /// # Examples
1384 ///
1385 /// ```
1386 /// #![feature(clone_from_ref)]
1387 /// #![feature(allocator_ext)]
1388 /// use std::rc::Rc;
1389 /// use std::alloc::System;
1390 ///
1391 /// let hello: Rc<str, System> = Rc::clone_from_ref_in("hello", System);
1392 /// ```
1393 #[cfg(not(no_global_oom_handling))]
1394 #[unstable(feature = "clone_from_ref", issue = "149075")]
1395 //#[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1396 pub fn clone_from_ref_in(value: &T, alloc: A) -> Rc<T, A> {
1397 // `in_progress` drops the allocation if we panic before finishing initializing it.
1398 let mut in_progress: UniqueRcUninit<T, A> = UniqueRcUninit::new(value, alloc);
1399
1400 // Initialize with clone of value.
1401 // ignore-tidy-undocumented-unsafe
1402 unsafe {
1403 // Clone. If the clone panics, `in_progress` will be dropped and clean up.
1404 value.clone_to_uninit(in_progress.data_ptr().cast());
1405 // Cast type of pointer, now that it is initialized.
1406 in_progress.into_rc()
1407 }
1408 }
1409
1410 /// Constructs a new `Rc<T>` with a clone of `value` in the provided allocator, returning an error if allocation fails
1411 ///
1412 /// # Examples
1413 ///
1414 /// ```
1415 /// #![feature(clone_from_ref)]
1416 /// #![feature(allocator_ext)]
1417 /// use std::rc::Rc;
1418 /// use std::alloc::System;
1419 ///
1420 /// let hello: Rc<str, System> = Rc::try_clone_from_ref_in("hello", System)?;
1421 /// # Ok::<(), std::alloc::AllocError>(())
1422 /// ```
1423 #[unstable(feature = "clone_from_ref", issue = "149075")]
1424 //#[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1425 pub fn try_clone_from_ref_in(value: &T, alloc: A) -> Result<Rc<T, A>, AllocError> {
1426 // `in_progress` drops the allocation if we panic before finishing initializing it.
1427 let mut in_progress: UniqueRcUninit<T, A> = UniqueRcUninit::try_new(value, alloc)?;
1428
1429 // Initialize with clone of value.
1430 // ignore-tidy-undocumented-unsafe
1431 let initialized_clone = unsafe {
1432 // Clone. If the clone panics, `in_progress` will be dropped and clean up.
1433 value.clone_to_uninit(in_progress.data_ptr().cast());
1434 // Cast type of pointer, now that it is initialized.
1435 in_progress.into_rc()
1436 };
1437
1438 Ok(initialized_clone)
1439 }
1440}
1441
1442impl<T, A: Allocator> Rc<[mem::MaybeUninit<T>], A> {
1443 /// Converts to `Rc<[T]>`.
1444 ///
1445 /// # Safety
1446 ///
1447 /// As with [`MaybeUninit::assume_init`],
1448 /// it is up to the caller to guarantee that the inner value
1449 /// really is in an initialized state.
1450 /// Calling this when the content is not yet fully initialized
1451 /// causes immediate undefined behavior.
1452 ///
1453 /// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
1454 ///
1455 /// # Examples
1456 ///
1457 /// ```
1458 /// use std::rc::Rc;
1459 ///
1460 /// let mut values = Rc::<[u32]>::new_uninit_slice(3);
1461 ///
1462 /// // Deferred initialization:
1463 /// let data = Rc::get_mut(&mut values).unwrap();
1464 /// data[0].write(1);
1465 /// data[1].write(2);
1466 /// data[2].write(3);
1467 ///
1468 /// let values = unsafe { values.assume_init() };
1469 ///
1470 /// assert_eq!(*values, [1, 2, 3])
1471 /// ```
1472 #[stable(feature = "new_uninit", since = "1.82.0")]
1473 #[inline]
1474 pub unsafe fn assume_init(self) -> Rc<[T], A> {
1475 let (ptr, alloc) = Rc::into_inner_with_allocator(self);
1476 // ignore-tidy-undocumented-unsafe
1477 unsafe { Rc::from_ptr_in(ptr.as_ptr() as _, alloc) }
1478 }
1479}
1480
1481impl<T: ?Sized> Rc<T> {
1482 /// Constructs an `Rc<T>` from a raw pointer.
1483 ///
1484 /// The raw pointer must have been previously returned by a call to
1485 /// [`Rc<U>::into_raw`][into_raw] or [`Rc<U>::into_raw_with_allocator`][into_raw_with_allocator].
1486 ///
1487 /// # Safety
1488 ///
1489 /// * Creating a `Rc<T>` from a pointer other than one returned from
1490 /// [`Rc<U>::into_raw`][into_raw] or [`Rc<U>::into_raw_with_allocator`][into_raw_with_allocator]
1491 /// is undefined behavior.
1492 /// * If `U` is sized, it must have the same size and alignment as `T`. This
1493 /// is trivially true if `U` is `T`.
1494 /// * If `U` is unsized, its data pointer must have the same size and
1495 /// alignment as `T`. This is trivially true if `Rc<U>` was constructed
1496 /// through `Rc<T>` and then converted to `Rc<U>` through an [unsized
1497 /// coercion].
1498 /// * Note that if `U` or `U`'s data pointer is not `T` but has the same size
1499 /// and alignment, this is basically like transmuting references of
1500 /// different types. See [`mem::transmute`][transmute] for more information
1501 /// on what restrictions apply in this case.
1502 /// * The raw pointer must point to a block of memory allocated by the global allocator
1503 /// * The user of `from_raw` has to make sure a specific value of `T` is only
1504 /// dropped once.
1505 ///
1506 /// This function is unsafe because improper use may lead to memory unsafety,
1507 /// even if the returned `Rc<T>` is never accessed.
1508 ///
1509 /// [into_raw]: Rc::into_raw
1510 /// [into_raw_with_allocator]: Rc::into_raw_with_allocator
1511 /// [transmute]: core::mem::transmute
1512 /// [unsized coercion]: https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions
1513 ///
1514 /// # Examples
1515 ///
1516 /// ```
1517 /// use std::rc::Rc;
1518 ///
1519 /// let x = Rc::new("hello".to_owned());
1520 /// let x_ptr = Rc::into_raw(x);
1521 ///
1522 /// unsafe {
1523 /// // Convert back to an `Rc` to prevent leak.
1524 /// let x = Rc::from_raw(x_ptr);
1525 /// assert_eq!(&*x, "hello");
1526 ///
1527 /// // Further calls to `Rc::from_raw(x_ptr)` would be memory-unsafe.
1528 /// }
1529 ///
1530 /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
1531 /// ```
1532 ///
1533 /// Convert a slice back into its original array:
1534 ///
1535 /// ```
1536 /// use std::rc::Rc;
1537 ///
1538 /// let x: Rc<[u32]> = Rc::new([1, 2, 3]);
1539 /// let x_ptr: *const [u32] = Rc::into_raw(x);
1540 ///
1541 /// unsafe {
1542 /// let x: Rc<[u32; 3]> = Rc::from_raw(x_ptr.cast::<[u32; 3]>());
1543 /// assert_eq!(&*x, &[1, 2, 3]);
1544 /// }
1545 /// ```
1546 #[inline]
1547 #[stable(feature = "rc_raw", since = "1.17.0")]
1548 pub unsafe fn from_raw(ptr: *const T) -> Self {
1549 // ignore-tidy-undocumented-unsafe
1550 unsafe { Self::from_raw_in(ptr, Global) }
1551 }
1552
1553 /// Consumes the `Rc`, returning the wrapped pointer.
1554 ///
1555 /// To avoid a memory leak the pointer must be converted back to an `Rc` using
1556 /// [`Rc::from_raw`].
1557 ///
1558 /// # Examples
1559 ///
1560 /// ```
1561 /// use std::rc::Rc;
1562 ///
1563 /// let x = Rc::new("hello".to_owned());
1564 /// let x_ptr = Rc::into_raw(x);
1565 /// assert_eq!(unsafe { &*x_ptr }, "hello");
1566 /// # // Prevent leaks for Miri.
1567 /// # drop(unsafe { Rc::from_raw(x_ptr) });
1568 /// ```
1569 #[must_use = "losing the pointer will leak memory"]
1570 #[stable(feature = "rc_raw", since = "1.17.0")]
1571 #[rustc_never_returns_null_ptr]
1572 pub fn into_raw(this: Self) -> *const T {
1573 let this = ManuallyDrop::new(this);
1574 Self::as_ptr(&*this)
1575 }
1576
1577 /// Increments the strong reference count on the `Rc<T>` associated with the
1578 /// provided pointer by one.
1579 ///
1580 /// # Safety
1581 ///
1582 /// The pointer must have been obtained through [`Rc::into_raw`] and must satisfy the
1583 /// same layout requirements specified in [`Rc::from_raw_in`].
1584 /// The associated `Rc` instance must be valid (i.e. the strong count must be at
1585 /// least 1) for the duration of this method, and `ptr` must point to a block of memory
1586 /// allocated by the global allocator.
1587 ///
1588 /// # Examples
1589 ///
1590 /// ```
1591 /// use std::rc::Rc;
1592 ///
1593 /// let five = Rc::new(5);
1594 ///
1595 /// unsafe {
1596 /// let ptr = Rc::into_raw(five);
1597 /// Rc::increment_strong_count(ptr);
1598 ///
1599 /// let five = Rc::from_raw(ptr);
1600 /// assert_eq!(2, Rc::strong_count(&five));
1601 /// # // Prevent leaks for Miri.
1602 /// # Rc::decrement_strong_count(ptr);
1603 /// }
1604 /// ```
1605 #[inline]
1606 #[stable(feature = "rc_mutate_strong_count", since = "1.53.0")]
1607 pub unsafe fn increment_strong_count(ptr: *const T) {
1608 // ignore-tidy-undocumented-unsafe
1609 unsafe { Self::increment_strong_count_in(ptr, Global) }
1610 }
1611
1612 /// Decrements the strong reference count on the `Rc<T>` associated with the
1613 /// provided pointer by one.
1614 ///
1615 /// # Safety
1616 ///
1617 /// The pointer must have been obtained through `Rc::into_raw` and must satisfy the
1618 /// same layout requirements specified in [`Rc::from_raw_in`][from_raw_in].
1619 /// The associated `Rc` instance must be valid (i.e. the strong count must be at
1620 /// least 1) when invoking this method, and `ptr` must point to a block of memory
1621 /// allocated by the global allocator. This method can be used to release the final `Rc` and
1622 /// backing storage, but **should not** be called after the final `Rc` has been released.
1623 ///
1624 /// [from_raw_in]: Rc::from_raw_in
1625 ///
1626 /// # Examples
1627 ///
1628 /// ```
1629 /// use std::rc::Rc;
1630 ///
1631 /// let five = Rc::new(5);
1632 ///
1633 /// unsafe {
1634 /// let ptr = Rc::into_raw(five);
1635 /// Rc::increment_strong_count(ptr);
1636 ///
1637 /// let five = Rc::from_raw(ptr);
1638 /// assert_eq!(2, Rc::strong_count(&five));
1639 /// Rc::decrement_strong_count(ptr);
1640 /// assert_eq!(1, Rc::strong_count(&five));
1641 /// }
1642 /// ```
1643 #[inline]
1644 #[stable(feature = "rc_mutate_strong_count", since = "1.53.0")]
1645 pub unsafe fn decrement_strong_count(ptr: *const T) {
1646 // ignore-tidy-undocumented-unsafe
1647 unsafe { Self::decrement_strong_count_in(ptr, Global) }
1648 }
1649
1650 /// Gets the number of strong (`Rc`) pointers to the allocation behind the given raw pointer.
1651 ///
1652 /// This method does not consume or drop the `Rc` behind this pointer.
1653 ///
1654 /// # Safety
1655 ///
1656 /// The pointer must point to (and have valid metadata for) the value inside a live `Rc`
1657 /// allocation, such as a pointer returned by [`Rc::into_raw`],
1658 /// [`Rc::into_raw_with_allocator`], or [`Rc::as_ptr`].
1659 /// `T` must have the same alignment as that value.
1660 /// The associated `Rc` instance must be valid (i.e. the strong count must be at
1661 /// least 1) for the duration of this method.
1662 ///
1663 /// # Examples
1664 ///
1665 /// ```
1666 /// #![feature(arc_raw_get_strong)]
1667 /// use std::rc::Rc;
1668 ///
1669 /// let five = Rc::new(5);
1670 /// let _also_five = Rc::clone(&five);
1671 /// let ptr = Rc::into_raw(five);
1672 ///
1673 /// unsafe {
1674 /// assert_eq!(2, Rc::strong_count_from_raw(ptr));
1675 ///
1676 /// // Convert back to an `Rc` to avoid leaking memory.
1677 /// let five = Rc::from_raw(ptr);
1678 /// assert_eq!(2, Rc::strong_count(&five));
1679 /// }
1680 /// ```
1681 #[inline]
1682 #[must_use]
1683 #[unstable(feature = "arc_raw_get_strong", issue = "157021")]
1684 pub unsafe fn strong_count_from_raw(ptr: *const T) -> usize {
1685 // SAFETY: Upheld by caller.
1686 let offset = unsafe { data_offset(ptr) };
1687 // Reverse the offset to find the original RcInner.
1688 // SAFETY: Caller ensures this pointer was to an `Rc` allocation,
1689 // so offsetting must be inbounds.
1690 let rc_ptr = unsafe { ptr.byte_sub(offset) as *mut RcInner<T> };
1691 // SAFETY: Per the above, an `RcInner` is stored here.
1692 unsafe { (*rc_ptr).strong.get() }
1693 }
1694}
1695
1696impl<T: ?Sized, A: Allocator> Rc<T, A> {
1697 /// Returns a reference to the underlying allocator.
1698 ///
1699 /// Note: this is an associated function, which means that you have
1700 /// to call it as `Rc::allocator(&r)` instead of `r.allocator()`. This
1701 /// is so that there is no conflict with a method on the inner type.
1702 #[inline]
1703 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1704 pub fn allocator(this: &Self) -> &A {
1705 &this.alloc
1706 }
1707
1708 /// Consumes the `Rc`, returning the wrapped pointer and allocator.
1709 ///
1710 /// To avoid a memory leak the pointer must be converted back to an `Rc` using
1711 /// [`Rc::from_raw_in`].
1712 ///
1713 /// # Examples
1714 ///
1715 /// ```
1716 /// #![feature(allocator_ext)]
1717 /// use std::rc::Rc;
1718 /// use std::alloc::System;
1719 ///
1720 /// let x = Rc::new_in("hello".to_owned(), System);
1721 /// let (ptr, alloc) = Rc::into_raw_with_allocator(x);
1722 /// assert_eq!(unsafe { &*ptr }, "hello");
1723 /// let x = unsafe { Rc::from_raw_in(ptr, alloc) };
1724 /// assert_eq!(&*x, "hello");
1725 /// ```
1726 #[must_use = "losing the pointer will leak memory"]
1727 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1728 pub fn into_raw_with_allocator(this: Self) -> (*const T, A) {
1729 let this = mem::ManuallyDrop::new(this);
1730 let ptr = Self::as_ptr(&this);
1731 // SAFETY: `this` is ManuallyDrop so the allocator will not be double-dropped
1732 let alloc = unsafe { ptr::read(&this.alloc) };
1733 (ptr, alloc)
1734 }
1735
1736 /// Provides a raw pointer to the data.
1737 ///
1738 /// The counts are not affected in any way and the `Rc` is not consumed. The pointer is valid
1739 /// for as long as there are strong counts in the `Rc`.
1740 ///
1741 /// # Examples
1742 ///
1743 /// ```
1744 /// use std::rc::Rc;
1745 ///
1746 /// let x = Rc::new(0);
1747 /// let y = Rc::clone(&x);
1748 /// let x_ptr = Rc::as_ptr(&x);
1749 /// assert_eq!(x_ptr, Rc::as_ptr(&y));
1750 /// assert_eq!(unsafe { *x_ptr }, 0);
1751 /// ```
1752 #[stable(feature = "weak_into_raw", since = "1.45.0")]
1753 #[rustc_never_returns_null_ptr]
1754 pub fn as_ptr(this: &Self) -> *const T {
1755 let ptr: *mut RcInner<T> = NonNull::as_ptr(this.ptr);
1756
1757 // SAFETY: This cannot go through Deref::deref or Rc::inner because
1758 // this is required to retain raw/mut provenance such that e.g. `get_mut` can
1759 // write through the pointer after the Rc is recovered through `from_raw`.
1760 unsafe { &raw mut (*ptr).value }
1761 }
1762
1763 /// Constructs an `Rc<T, A>` from a raw pointer in the provided allocator.
1764 ///
1765 /// The raw pointer must have been previously returned by a call to [`Rc<U,
1766 /// A>::into_raw`][into_raw] or [`Rc<U, A>::into_raw_with_allocator`][into_raw_with_allocator].
1767 ///
1768 /// # Safety
1769 ///
1770 /// * Creating a `Rc<T, A>` from a pointer other than one returned from
1771 /// [`Rc<U, A>::into_raw`][into_raw] or [`Rc<U, A>::into_raw_with_allocator`][into_raw_with_allocator]
1772 /// is undefined behavior.
1773 /// * If `U` is sized, it must have the same size and alignment as `T`. This
1774 /// is trivially true if `U` is `T`.
1775 /// * If `U` is unsized, its data pointer must have the same size and
1776 /// alignment as `T`. This is trivially true if `Rc<U, A>` was constructed
1777 /// through `Rc<T, A>` and then converted to `Rc<U, A>` through an [unsized
1778 /// coercion].
1779 /// * Note that if `U` or `U`'s data pointer is not `T` but has the same size
1780 /// and alignment, this is basically like transmuting references of
1781 /// different types. See [`mem::transmute`][transmute] for more information
1782 /// on what restrictions apply in this case.
1783 /// * The raw pointer must point to a block of memory allocated by `alloc`
1784 /// * The user of `from_raw` has to make sure a specific value of `T` is only
1785 /// dropped once.
1786 ///
1787 /// This function is unsafe because improper use may lead to memory unsafety,
1788 /// even if the returned `Rc<T, A>` is never accessed.
1789 ///
1790 /// [into_raw]: Rc::into_raw
1791 /// [into_raw_with_allocator]: Rc::into_raw_with_allocator
1792 /// [transmute]: core::mem::transmute
1793 /// [unsized coercion]: https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions
1794 ///
1795 /// # Examples
1796 ///
1797 /// ```
1798 /// #![feature(allocator_ext)]
1799 ///
1800 /// use std::rc::Rc;
1801 /// use std::alloc::System;
1802 ///
1803 /// let x = Rc::new_in("hello".to_owned(), System);
1804 /// let (x_ptr, _alloc) = Rc::into_raw_with_allocator(x);
1805 ///
1806 /// unsafe {
1807 /// // Convert back to an `Rc` to prevent leak.
1808 /// let x = Rc::from_raw_in(x_ptr, System);
1809 /// assert_eq!(&*x, "hello");
1810 ///
1811 /// // Further calls to `Rc::from_raw(x_ptr)` would be memory-unsafe.
1812 /// }
1813 ///
1814 /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
1815 /// ```
1816 ///
1817 /// Convert a slice back into its original array:
1818 ///
1819 /// ```
1820 /// #![feature(allocator_ext)]
1821 ///
1822 /// use std::rc::Rc;
1823 /// use std::alloc::System;
1824 ///
1825 /// let x: Rc<[u32], _> = Rc::new_in([1, 2, 3], System);
1826 /// let x_ptr: *const [u32] = Rc::into_raw_with_allocator(x).0;
1827 ///
1828 /// unsafe {
1829 /// let x: Rc<[u32; 3], _> = Rc::from_raw_in(x_ptr.cast::<[u32; 3]>(), System);
1830 /// assert_eq!(&*x, &[1, 2, 3]);
1831 /// }
1832 /// ```
1833 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1834 pub unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self {
1835 // ignore-tidy-undocumented-unsafe
1836 let offset = unsafe { data_offset(ptr) };
1837
1838 // Reverse the offset to find the original RcInner.
1839 // ignore-tidy-undocumented-unsafe
1840 let rc_ptr = unsafe { ptr.byte_sub(offset) as *mut RcInner<T> };
1841
1842 // ignore-tidy-undocumented-unsafe
1843 unsafe { Self::from_ptr_in(rc_ptr, alloc) }
1844 }
1845
1846 /// Creates a new [`Weak`] pointer to this allocation.
1847 ///
1848 /// # Examples
1849 ///
1850 /// ```
1851 /// use std::rc::Rc;
1852 ///
1853 /// let five = Rc::new(5);
1854 ///
1855 /// let weak_five = Rc::downgrade(&five);
1856 /// ```
1857 #[must_use = "this returns a new `Weak` pointer, \
1858 without modifying the original `Rc`"]
1859 #[stable(feature = "rc_weak", since = "1.4.0")]
1860 pub fn downgrade(this: &Self) -> Weak<T, A>
1861 where
1862 A: AllocatorClone,
1863 {
1864 this.inner().inc_weak();
1865 // Make sure we do not create a dangling Weak
1866 debug_assert!(!is_dangling(this.ptr.as_ptr()));
1867 Weak { ptr: this.ptr, alloc: this.alloc.clone() }
1868 }
1869
1870 /// Gets the number of [`Weak`] pointers to this allocation.
1871 ///
1872 /// # Examples
1873 ///
1874 /// ```
1875 /// use std::rc::Rc;
1876 ///
1877 /// let five = Rc::new(5);
1878 /// let _weak_five = Rc::downgrade(&five);
1879 ///
1880 /// assert_eq!(1, Rc::weak_count(&five));
1881 /// ```
1882 #[inline]
1883 #[must_use]
1884 #[stable(feature = "rc_counts", since = "1.15.0")]
1885 pub fn weak_count(this: &Self) -> usize {
1886 this.inner().weak() - 1
1887 }
1888
1889 /// Gets the number of strong (`Rc`) pointers to this allocation.
1890 ///
1891 /// # Examples
1892 ///
1893 /// ```
1894 /// use std::rc::Rc;
1895 ///
1896 /// let five = Rc::new(5);
1897 /// let _also_five = Rc::clone(&five);
1898 ///
1899 /// assert_eq!(2, Rc::strong_count(&five));
1900 /// ```
1901 #[inline]
1902 #[must_use]
1903 #[stable(feature = "rc_counts", since = "1.15.0")]
1904 pub fn strong_count(this: &Self) -> usize {
1905 this.inner().strong()
1906 }
1907
1908 /// Increments the strong reference count on the `Rc<T>` associated with the
1909 /// provided pointer by one.
1910 ///
1911 /// # Safety
1912 ///
1913 /// The pointer must have been obtained through `Rc::into_raw` and must satisfy the
1914 /// same layout requirements specified in [`Rc::from_raw_in`][from_raw_in].
1915 /// The associated `Rc` instance must be valid (i.e. the strong count must be at
1916 /// least 1) for the duration of this method, and `ptr` must point to a block of memory
1917 /// allocated by `alloc`.
1918 ///
1919 /// [from_raw_in]: Rc::from_raw_in
1920 ///
1921 /// # Examples
1922 ///
1923 /// ```
1924 /// #![feature(allocator_ext)]
1925 ///
1926 /// use std::rc::Rc;
1927 /// use std::alloc::System;
1928 ///
1929 /// let five = Rc::new_in(5, System);
1930 ///
1931 /// unsafe {
1932 /// let (ptr, _alloc) = Rc::into_raw_with_allocator(five);
1933 /// Rc::increment_strong_count_in(ptr, System);
1934 ///
1935 /// let five = Rc::from_raw_in(ptr, System);
1936 /// assert_eq!(2, Rc::strong_count(&five));
1937 /// # // Prevent leaks for Miri.
1938 /// # Rc::decrement_strong_count_in(ptr, System);
1939 /// }
1940 /// ```
1941 #[inline]
1942 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1943 pub unsafe fn increment_strong_count_in(ptr: *const T, alloc: A)
1944 where
1945 A: AllocatorClone,
1946 {
1947 // Retain Rc, but don't touch refcount by wrapping in ManuallyDrop
1948 // ignore-tidy-undocumented-unsafe
1949 let rc = unsafe { mem::ManuallyDrop::new(Rc::<T, A>::from_raw_in(ptr, alloc)) };
1950 // Now increase refcount, but don't drop new refcount either
1951 let _rc_clone: mem::ManuallyDrop<_> = rc.clone();
1952 }
1953
1954 /// Decrements the strong reference count on the `Rc<T>` associated with the
1955 /// provided pointer by one.
1956 ///
1957 /// # Safety
1958 ///
1959 /// The pointer must have been obtained through `Rc::into_raw`and must satisfy the
1960 /// same layout requirements specified in [`Rc::from_raw_in`][from_raw_in].
1961 /// The associated `Rc` instance must be valid (i.e. the strong count must be at
1962 /// least 1) when invoking this method, and `ptr` must point to a block of memory
1963 /// allocated by `alloc`. This method can be used to release the final `Rc` and
1964 /// backing storage, but **should not** be called after the final `Rc` has been released.
1965 ///
1966 /// [from_raw_in]: Rc::from_raw_in
1967 ///
1968 /// # Examples
1969 ///
1970 /// ```
1971 /// #![feature(allocator_ext)]
1972 ///
1973 /// use std::rc::Rc;
1974 /// use std::alloc::System;
1975 ///
1976 /// let five = Rc::new_in(5, System);
1977 ///
1978 /// unsafe {
1979 /// let (ptr, _alloc) = Rc::into_raw_with_allocator(five);
1980 /// Rc::increment_strong_count_in(ptr, System);
1981 ///
1982 /// let five = Rc::from_raw_in(ptr, System);
1983 /// assert_eq!(2, Rc::strong_count(&five));
1984 /// Rc::decrement_strong_count_in(ptr, System);
1985 /// assert_eq!(1, Rc::strong_count(&five));
1986 /// }
1987 /// ```
1988 #[inline]
1989 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
1990 pub unsafe fn decrement_strong_count_in(ptr: *const T, alloc: A) {
1991 // SAFETY: Upheld by caller.
1992 unsafe { drop(Rc::from_raw_in(ptr, alloc)) };
1993 }
1994
1995 /// Determine whether this is the unique reference to the underlying data.
1996 ///
1997 /// Returns `true` if there are no other `Rc` or [`Weak`] pointers to the same allocation;
1998 /// returns `false` otherwise.
1999 ///
2000 /// If this function returns `true`, then it is guaranteed to be safe to call
2001 /// [`get_mut_unchecked`] on this `Rc`, as long as no clones occur in between.
2002 ///
2003 /// The result can become stale if the `Rc` is cloned or downgraded between
2004 /// the check and its use. Make sure no new `Rc` or `Weak` pointers are created
2005 /// in between.
2006 ///
2007 /// # Examples
2008 ///
2009 /// ```
2010 /// use std::rc::Rc;
2011 ///
2012 /// let x = Rc::new(3);
2013 /// assert!(Rc::is_unique(&x));
2014 ///
2015 /// let y = Rc::clone(&x);
2016 /// assert!(!Rc::is_unique(&x));
2017 /// drop(y);
2018 ///
2019 /// // Weak references also count, because they could be upgraded at any time.
2020 /// let z = Rc::downgrade(&x);
2021 /// assert!(!Rc::is_unique(&x));
2022 /// ```
2023 ///
2024 /// # Pointer invalidation
2025 ///
2026 /// This function will always return the same value as `Rc::get_mut(rc).is_some()`. However,
2027 /// unlike that operation it does not produce any mutable references to the underlying data,
2028 /// meaning no pointers to the data inside the `Rc` are invalidated by the call. Thus, the
2029 /// following code is valid:
2030 ///
2031 /// ```
2032 /// use std::rc::Rc;
2033 ///
2034 /// let rc = Rc::new(5);
2035 /// let pointer: *const i32 = &*rc;
2036 /// assert!(Rc::is_unique(&rc));
2037 /// assert_eq!(unsafe { *pointer }, 5);
2038 /// ```
2039 ///
2040 /// The following changes would make the example unsound:
2041 ///
2042 /// - Using `Rc::get_mut` to create a mutable reference while also accessing the value
2043 /// through `pointer`.
2044 /// - Dropping `rc` before dereferencing `pointer`, leaving it dangling.
2045 ///
2046 /// [`get_mut_unchecked`]: Self::get_mut_unchecked
2047 #[inline]
2048 #[stable(feature = "arc_is_unique", since = "CURRENT_RUSTC_VERSION")]
2049 pub fn is_unique(this: &Self) -> bool {
2050 Rc::weak_count(this) == 0 && Rc::strong_count(this) == 1
2051 }
2052
2053 /// Returns a mutable reference into the given `Rc`, if there are
2054 /// no other `Rc` or [`Weak`] pointers to the same allocation.
2055 ///
2056 /// Returns [`None`] otherwise, because it is not safe to
2057 /// mutate a shared value.
2058 ///
2059 /// See also [`make_mut`][make_mut], which will [`clone`][clone]
2060 /// the inner value when there are other `Rc` pointers.
2061 ///
2062 /// [make_mut]: Rc::make_mut
2063 /// [clone]: Clone::clone
2064 ///
2065 /// # Examples
2066 ///
2067 /// ```
2068 /// use std::rc::Rc;
2069 ///
2070 /// let mut x = Rc::new(3);
2071 /// *Rc::get_mut(&mut x).unwrap() = 4;
2072 /// assert_eq!(*x, 4);
2073 ///
2074 /// let _y = Rc::clone(&x);
2075 /// assert!(Rc::get_mut(&mut x).is_none());
2076 /// ```
2077 #[inline]
2078 #[stable(feature = "rc_unique", since = "1.4.0")]
2079 pub fn get_mut(this: &mut Self) -> Option<&mut T> {
2080 // SAFETY: Ensured by uniqueness check.
2081 if Rc::is_unique(this) { unsafe { Some(Rc::get_mut_unchecked(this)) } } else { None }
2082 }
2083
2084 /// Returns a mutable reference into the given `Rc`,
2085 /// without any check.
2086 ///
2087 /// See also [`get_mut`], which is safe and does appropriate checks.
2088 ///
2089 /// [`get_mut`]: Rc::get_mut
2090 ///
2091 /// # Safety
2092 ///
2093 /// If any other `Rc` or [`Weak`] pointers to the same allocation exist, then
2094 /// they must not be dereferenced or have active borrows for the duration
2095 /// of the returned borrow, and their inner type must be exactly the same as the
2096 /// inner type of this Rc (including lifetimes). This is trivially the case if no
2097 /// such pointers exist, for example immediately after `Rc::new`.
2098 ///
2099 /// # Examples
2100 ///
2101 /// ```
2102 /// #![feature(get_mut_unchecked)]
2103 ///
2104 /// use std::rc::Rc;
2105 ///
2106 /// let mut x = Rc::new(String::new());
2107 /// unsafe {
2108 /// Rc::get_mut_unchecked(&mut x).push_str("foo")
2109 /// }
2110 /// assert_eq!(*x, "foo");
2111 /// ```
2112 /// Other `Rc` pointers to the same allocation must be to the same type.
2113 /// ```no_run
2114 /// #![feature(get_mut_unchecked)]
2115 ///
2116 /// use std::rc::Rc;
2117 ///
2118 /// let x: Rc<str> = Rc::from("Hello, world!");
2119 /// let mut y: Rc<[u8]> = x.clone().into();
2120 /// unsafe {
2121 /// // this is Undefined Behavior, because x's inner type is str, not [u8]
2122 /// Rc::get_mut_unchecked(&mut y).fill(0xff); // 0xff is invalid in UTF-8
2123 /// }
2124 /// println!("{}", &*x); // Invalid UTF-8 in a str
2125 /// ```
2126 /// Other `Rc` pointers to the same allocation must be to the exact same type, including lifetimes.
2127 /// ```no_run
2128 /// #![feature(get_mut_unchecked)]
2129 ///
2130 /// use std::rc::Rc;
2131 ///
2132 /// let x: Rc<&str> = Rc::new("Hello, world!");
2133 /// {
2134 /// let s = String::from("Oh, no!");
2135 /// let mut y: Rc<&str> = x.clone();
2136 /// unsafe {
2137 /// // this is Undefined Behavior, because x's inner type
2138 /// // is &'long str, not &'short str
2139 /// *Rc::get_mut_unchecked(&mut y) = &s;
2140 /// }
2141 /// }
2142 /// println!("{}", &*x); // Use-after-free
2143 /// ```
2144 #[inline]
2145 #[unstable(feature = "get_mut_unchecked", issue = "63292")]
2146 pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {
2147 // We are careful to *not* create a reference covering the "count" fields, as
2148 // this would conflict with accesses to the reference counts (e.g. by `Weak`).
2149 // ignore-tidy-undocumented-unsafe
2150 unsafe { &mut (*this.ptr.as_ptr()).value }
2151 }
2152
2153 #[inline]
2154 #[stable(feature = "ptr_eq", since = "1.17.0")]
2155 /// Returns `true` if the two `Rc`s point to the same allocation in a vein similar to
2156 /// [`ptr::eq`]. This function ignores the metadata of `dyn Trait` pointers.
2157 ///
2158 /// # Examples
2159 ///
2160 /// ```
2161 /// use std::rc::Rc;
2162 ///
2163 /// let five = Rc::new(5);
2164 /// let same_five = Rc::clone(&five);
2165 /// let other_five = Rc::new(5);
2166 ///
2167 /// assert!(Rc::ptr_eq(&five, &same_five));
2168 /// assert!(!Rc::ptr_eq(&five, &other_five));
2169 /// ```
2170 pub fn ptr_eq(this: &Self, other: &Self) -> bool {
2171 ptr::addr_eq(this.ptr.as_ptr(), other.ptr.as_ptr())
2172 }
2173}
2174
2175#[cfg(not(no_global_oom_handling))]
2176impl<T: ?Sized + CloneToUninit, A: AllocatorClone> Rc<T, A> {
2177 /// Makes a mutable reference into the given `Rc`.
2178 ///
2179 /// If there are other `Rc` pointers to the same allocation, then `make_mut` will
2180 /// [`clone`] the inner value to a new allocation to ensure unique ownership. This is also
2181 /// referred to as clone-on-write.
2182 ///
2183 /// However, if there are no other `Rc` pointers to this allocation, but some [`Weak`]
2184 /// pointers, then the [`Weak`] pointers will be disassociated and the inner value will not
2185 /// be cloned.
2186 ///
2187 /// See also [`get_mut`], which will fail rather than cloning the inner value
2188 /// or disassociating [`Weak`] pointers.
2189 ///
2190 /// [`clone`]: Clone::clone
2191 /// [`get_mut`]: Rc::get_mut
2192 ///
2193 /// # Examples
2194 ///
2195 /// ```
2196 /// use std::rc::Rc;
2197 ///
2198 /// let mut data = Rc::new(5);
2199 ///
2200 /// *Rc::make_mut(&mut data) += 1; // Won't clone anything
2201 /// let mut other_data = Rc::clone(&data); // Won't clone inner data
2202 /// *Rc::make_mut(&mut data) += 1; // Clones inner data
2203 /// *Rc::make_mut(&mut data) += 1; // Won't clone anything
2204 /// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
2205 ///
2206 /// // Now `data` and `other_data` point to different allocations.
2207 /// assert_eq!(*data, 8);
2208 /// assert_eq!(*other_data, 12);
2209 /// ```
2210 ///
2211 /// [`Weak`] pointers will be disassociated:
2212 ///
2213 /// ```
2214 /// use std::rc::Rc;
2215 ///
2216 /// let mut data = Rc::new(75);
2217 /// let weak = Rc::downgrade(&data);
2218 ///
2219 /// assert!(75 == *data);
2220 /// assert!(75 == *weak.upgrade().unwrap());
2221 ///
2222 /// *Rc::make_mut(&mut data) += 1;
2223 ///
2224 /// assert!(76 == *data);
2225 /// assert!(weak.upgrade().is_none());
2226 /// ```
2227 #[inline]
2228 #[stable(feature = "rc_unique", since = "1.4.0")]
2229 pub fn make_mut(this: &mut Self) -> &mut T {
2230 let size_of_val = size_of_val::<T>(&**this);
2231
2232 if Rc::strong_count(this) != 1 {
2233 // Gotta clone the data, there are other Rcs.
2234 *this = Rc::clone_from_ref_in(&**this, this.alloc.clone());
2235 } else if Rc::weak_count(this) != 0 {
2236 // Can just steal the data, all that's left is Weaks
2237
2238 let mut in_progress: UniqueRcUninit<T, A> =
2239 UniqueRcUninit::new(&**this, this.alloc.clone());
2240 // ignore-tidy-undocumented-unsafe
2241 unsafe {
2242 // Initialize `in_progress` with move of **this.
2243 // We have to express this in terms of bytes because `T: ?Sized`; there is no
2244 // operation that just copies a value based on its `size_of_val()`.
2245 ptr::copy_nonoverlapping(
2246 ptr::from_ref(&**this).cast::<u8>(),
2247 in_progress.data_ptr().cast::<u8>(),
2248 size_of_val,
2249 );
2250
2251 // This leaves us with 0 strong refs, so the data has
2252 // effectively been moved to the new rc.
2253 this.inner().dec_strong();
2254
2255 // Remove implicit strong-weak ref (no need to craft a fake
2256 // Weak here -- we know other Weaks can clean up for us)
2257 this.inner().dec_weak();
2258
2259 // Last chance to not accidentally forget the allocator.
2260 // Only drop at the end of the scope to avoid panics.
2261 let _alloc = ptr::read(&this.alloc);
2262
2263 // Replace `this` with newly constructed Rc that has the moved data.
2264 ptr::write(this, in_progress.into_rc());
2265 }
2266 }
2267 // SAFETY: We're guaranteed that the pointer
2268 // returned is the *only* pointer that will ever be returned to T. Our
2269 // reference count is guaranteed to be 1 at this point, and we required
2270 // the `Rc<T>` itself to be `mut`, so we're returning the only possible
2271 // reference to the allocation.
2272 unsafe { &mut this.ptr.as_mut().value }
2273 }
2274}
2275
2276impl<T: Clone, A: Allocator> Rc<T, A> {
2277 /// If we have the only reference to `T` then unwrap it. Otherwise, clone `T` and return the
2278 /// clone.
2279 ///
2280 /// Assuming `rc_t` is of type `Rc<T>`, this function is functionally equivalent to
2281 /// `(*rc_t).clone()`, but will avoid cloning the inner value where possible.
2282 ///
2283 /// # Examples
2284 ///
2285 /// ```
2286 /// # use std::{ptr, rc::Rc};
2287 /// let inner = String::from("test");
2288 /// let ptr = inner.as_ptr();
2289 ///
2290 /// let rc = Rc::new(inner);
2291 /// let inner = Rc::unwrap_or_clone(rc);
2292 /// // The inner value was not cloned
2293 /// assert!(ptr::eq(ptr, inner.as_ptr()));
2294 ///
2295 /// let rc = Rc::new(inner);
2296 /// let rc2 = rc.clone();
2297 /// let inner = Rc::unwrap_or_clone(rc);
2298 /// // Because there were 2 references, we had to clone the inner value.
2299 /// assert!(!ptr::eq(ptr, inner.as_ptr()));
2300 /// // `rc2` is the last reference, so when we unwrap it we get back
2301 /// // the original `String`.
2302 /// let inner = Rc::unwrap_or_clone(rc2);
2303 /// assert!(ptr::eq(ptr, inner.as_ptr()));
2304 /// ```
2305 #[inline]
2306 #[stable(feature = "arc_unwrap_or_clone", since = "1.76.0")]
2307 pub fn unwrap_or_clone(this: Self) -> T {
2308 Rc::try_unwrap(this).unwrap_or_else(|rc| (*rc).clone())
2309 }
2310}
2311
2312impl<A: Allocator> Rc<dyn Any, A> {
2313 /// Attempts to downcast the `Rc<dyn Any>` to a concrete type.
2314 ///
2315 /// # Examples
2316 ///
2317 /// ```
2318 /// use std::any::Any;
2319 /// use std::rc::Rc;
2320 ///
2321 /// fn print_if_string(value: Rc<dyn Any>) {
2322 /// if let Ok(string) = value.downcast::<String>() {
2323 /// println!("String ({}): {}", string.len(), string);
2324 /// }
2325 /// }
2326 ///
2327 /// let my_string = "Hello World".to_string();
2328 /// print_if_string(Rc::new(my_string));
2329 /// print_if_string(Rc::new(0i8));
2330 /// ```
2331 #[inline]
2332 #[stable(feature = "rc_downcast", since = "1.29.0")]
2333 pub fn downcast<T: Any>(self) -> Result<Rc<T, A>, Self> {
2334 if (*self).is::<T>() {
2335 // SAFETY: Check ensures typecast is corrext.
2336 unsafe {
2337 let (ptr, alloc) = Rc::into_inner_with_allocator(self);
2338 Ok(Rc::from_inner_in(ptr.cast(), alloc))
2339 }
2340 } else {
2341 Err(self)
2342 }
2343 }
2344
2345 /// Downcasts the `Rc<dyn Any>` to a concrete type.
2346 ///
2347 /// For a safe alternative see [`downcast`].
2348 ///
2349 /// # Examples
2350 ///
2351 /// ```
2352 /// #![feature(downcast_unchecked)]
2353 ///
2354 /// use std::any::Any;
2355 /// use std::rc::Rc;
2356 ///
2357 /// let x: Rc<dyn Any> = Rc::new(1_usize);
2358 ///
2359 /// unsafe {
2360 /// assert_eq!(*x.downcast_unchecked::<usize>(), 1);
2361 /// }
2362 /// ```
2363 ///
2364 /// # Safety
2365 ///
2366 /// The contained value must be of type `T`. Calling this method
2367 /// with the incorrect type is *undefined behavior*.
2368 ///
2369 /// [`downcast`]: Self::downcast
2370 #[inline]
2371 #[unstable(feature = "downcast_unchecked", issue = "90850")]
2372 pub unsafe fn downcast_unchecked<T: Any>(self) -> Rc<T, A> {
2373 // SAFETY: Caller ensures typecast is correct.
2374 unsafe {
2375 let (ptr, alloc) = Rc::into_inner_with_allocator(self);
2376 Rc::from_inner_in(ptr.cast(), alloc)
2377 }
2378 }
2379}
2380
2381impl<T: ?Sized> Rc<T> {
2382 /// Allocates an `RcInner<T>` with sufficient space for
2383 /// a possibly-unsized inner value where the value has the layout provided.
2384 ///
2385 /// The function `mem_to_rc_inner` is called with the data pointer
2386 /// and must return back a (potentially fat)-pointer for the `RcInner<T>`.
2387 #[cfg(not(no_global_oom_handling))]
2388 unsafe fn allocate_for_layout(
2389 value_layout: Layout,
2390 allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,
2391 mem_to_rc_inner: impl FnOnce(*mut u8) -> *mut RcInner<T>,
2392 ) -> *mut RcInner<T> {
2393 let layout = rc_inner_layout_for_value_layout(value_layout);
2394 // ignore-tidy-undocumented-unsafe
2395 unsafe {
2396 Rc::try_allocate_for_layout(value_layout, allocate, mem_to_rc_inner)
2397 .unwrap_or_else(|_| handle_alloc_error(layout))
2398 }
2399 }
2400
2401 /// Allocates an `RcInner<T>` with sufficient space for
2402 /// a possibly-unsized inner value where the value has the layout provided,
2403 /// returning an error if allocation fails.
2404 ///
2405 /// The function `mem_to_rc_inner` is called with the data pointer
2406 /// and must return back a (potentially fat)-pointer for the `RcInner<T>`.
2407 #[inline]
2408 unsafe fn try_allocate_for_layout(
2409 value_layout: Layout,
2410 allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,
2411 mem_to_rc_inner: impl FnOnce(*mut u8) -> *mut RcInner<T>,
2412 ) -> Result<*mut RcInner<T>, AllocError> {
2413 let layout = rc_inner_layout_for_value_layout(value_layout);
2414
2415 // Allocate for the layout.
2416 let ptr = allocate(layout)?;
2417
2418 // Initialize the RcInner
2419 let inner = mem_to_rc_inner(ptr.as_non_null_ptr().as_ptr());
2420 // ignore-tidy-undocumented-unsafe
2421 unsafe {
2422 debug_assert_eq!(Layout::for_value_raw(inner), layout);
2423
2424 (&raw mut (*inner).strong).write(Cell::new(1));
2425 (&raw mut (*inner).weak).write(Cell::new(1));
2426 }
2427
2428 Ok(inner)
2429 }
2430}
2431
2432impl<T: ?Sized, A: Allocator> Rc<T, A> {
2433 /// Allocates an `RcInner<T>` with sufficient space for an unsized inner value
2434 #[cfg(not(no_global_oom_handling))]
2435 unsafe fn allocate_for_ptr_in(ptr: *const T, alloc: &A) -> *mut RcInner<T> {
2436 // Allocate for the `RcInner<T>` using the given value.
2437 // ignore-tidy-undocumented-unsafe
2438 unsafe {
2439 Rc::<T>::allocate_for_layout(
2440 Layout::for_value_raw(ptr),
2441 |layout| alloc.allocate(layout),
2442 |mem| mem.with_metadata_of(ptr as *const RcInner<T>),
2443 )
2444 }
2445 }
2446
2447 #[cfg(not(no_global_oom_handling))]
2448 fn from_box_in(src: Box<T, A>) -> Rc<T, A> {
2449 let value_size = size_of_val(&*src);
2450 // ignore-tidy-undocumented-unsafe
2451 unsafe {
2452 let ptr = Self::allocate_for_ptr_in(&*src, Box::allocator(&src));
2453
2454 // Copy value as bytes
2455 ptr::copy_nonoverlapping(
2456 (&raw const *src) as *const u8,
2457 (&raw mut (*ptr).value) as *mut u8,
2458 value_size,
2459 );
2460
2461 // Free the allocation without dropping its contents
2462 let (bptr, alloc) = Box::into_raw_with_allocator(src);
2463 let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, &alloc);
2464 drop(src);
2465
2466 Self::from_ptr_in(ptr, alloc)
2467 }
2468 }
2469}
2470
2471impl<T> Rc<[T]> {
2472 /// Allocates an `RcInner<[T]>` with the given length.
2473 #[cfg(not(no_global_oom_handling))]
2474 unsafe fn allocate_for_slice(len: usize) -> *mut RcInner<[T]> {
2475 // ignore-tidy-undocumented-unsafe
2476 unsafe {
2477 Self::allocate_for_layout(
2478 Layout::array::<T>(len).unwrap(),
2479 |layout| Global.allocate(layout),
2480 |mem| mem.cast::<T>().cast_slice(len) as *mut RcInner<[T]>,
2481 )
2482 }
2483 }
2484
2485 /// Copy elements from slice into newly allocated `Rc<[T]>`
2486 ///
2487 /// Unsafe because the caller must either take ownership, bind `T: Copy` or
2488 /// bind `T: TrivialClone`.
2489 #[cfg(not(no_global_oom_handling))]
2490 unsafe fn copy_from_slice(v: &[T]) -> Rc<[T]> {
2491 // ignore-tidy-undocumented-unsafe
2492 unsafe {
2493 let ptr = Self::allocate_for_slice(v.len());
2494 ptr::copy_nonoverlapping(v.as_ptr(), (&raw mut (*ptr).value) as *mut T, v.len());
2495 Self::from_ptr(ptr)
2496 }
2497 }
2498
2499 /// Constructs an `Rc<[T]>` from an iterator known to be of a certain size.
2500 ///
2501 /// Behavior is undefined should the size be wrong.
2502 #[cfg(not(no_global_oom_handling))]
2503 unsafe fn from_iter_exact(iter: impl Iterator<Item = T>, len: usize) -> Rc<[T]> {
2504 // Panic guard while cloning T elements.
2505 // In the event of a panic, elements that have been written
2506 // into the new RcInner will be dropped, then the memory freed.
2507 struct Guard<T> {
2508 mem: NonNull<u8>,
2509 elems: *mut T,
2510 layout: Layout,
2511 n_elems: usize,
2512 }
2513
2514 impl<T> Drop for Guard<T> {
2515 fn drop(&mut self) {
2516 // ignore-tidy-undocumented-unsafe
2517 unsafe {
2518 let slice = from_raw_parts_mut(self.elems, self.n_elems);
2519 ptr::drop_in_place(slice);
2520
2521 Global.deallocate(self.mem, self.layout);
2522 }
2523 }
2524 }
2525
2526 // ignore-tidy-undocumented-unsafe
2527 unsafe {
2528 let ptr = Self::allocate_for_slice(len);
2529
2530 let mem = ptr as *mut _ as *mut u8;
2531 let layout = Layout::for_value_raw(ptr);
2532
2533 // Pointer to first element
2534 let elems = (&raw mut (*ptr).value) as *mut T;
2535
2536 let mut guard = Guard { mem: NonNull::new_unchecked(mem), elems, layout, n_elems: 0 };
2537
2538 for (i, item) in iter.enumerate() {
2539 ptr::write(elems.add(i), item);
2540 guard.n_elems += 1;
2541 }
2542
2543 // All clear. Forget the guard so it doesn't free the new RcInner.
2544 mem::forget(guard);
2545
2546 Self::from_ptr(ptr)
2547 }
2548 }
2549}
2550
2551impl<T, A: Allocator> Rc<[T], A> {
2552 /// Allocates an `RcInner<[T]>` with the given length.
2553 #[inline]
2554 #[cfg(not(no_global_oom_handling))]
2555 unsafe fn allocate_for_slice_in(len: usize, alloc: &A) -> *mut RcInner<[T]> {
2556 // ignore-tidy-undocumented-unsafe
2557 unsafe {
2558 Rc::<[T]>::allocate_for_layout(
2559 Layout::array::<T>(len).unwrap(),
2560 |layout| alloc.allocate(layout),
2561 |mem| mem.cast::<T>().cast_slice(len) as *mut RcInner<[T]>,
2562 )
2563 }
2564 }
2565}
2566
2567#[cfg(not(no_global_oom_handling))]
2568/// Specialization trait used for `From<&[T]>`.
2569trait RcFromSlice<T> {
2570 fn from_slice(slice: &[T]) -> Self;
2571}
2572
2573#[cfg(not(no_global_oom_handling))]
2574impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
2575 #[inline]
2576 default fn from_slice(v: &[T]) -> Self {
2577 // ignore-tidy-undocumented-unsafe
2578 unsafe { Self::from_iter_exact(v.iter().cloned(), v.len()) }
2579 }
2580}
2581
2582#[cfg(not(no_global_oom_handling))]
2583impl<T: TrivialClone> RcFromSlice<T> for Rc<[T]> {
2584 #[inline]
2585 fn from_slice(v: &[T]) -> Self {
2586 // SAFETY: `T` implements `TrivialClone`, so this is sound and equivalent
2587 // to the above.
2588 unsafe { Rc::copy_from_slice(v) }
2589 }
2590}
2591
2592#[stable(feature = "rust1", since = "1.0.0")]
2593impl<T: ?Sized, A: Allocator> Deref for Rc<T, A> {
2594 type Target = T;
2595
2596 #[inline(always)]
2597 fn deref(&self) -> &T {
2598 &self.inner().value
2599 }
2600}
2601
2602// The API of this pointer type enforces that if the `T` is pinned, then *all*
2603// clones of this `Rc<T>` are wrapped as `Pin<Rc<T>>`. Since an `&Rc<T>` could
2604// be used to obtain an `Rc<T>` that is not wrapped in `Pin` (and later used
2605// with `Rc::get_mut`), this means that this type treats `&Rc<T>` as evidence
2606// that the `T` is not pinned. The implementations of various traits are written
2607// accordingly. Since this type is not fundamental, downstream crates cannot
2608// provide malicious implementations of any of the traits relevant for `Pin`.
2609#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
2610unsafe impl<T: ?Sized, A: StaticAllocator> PinSafePointer for Rc<T, A> {}
2611
2612//#[unstable(feature = "unique_rc_arc", issue = "112566")]
2613#[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")]
2614unsafe impl<T: ?Sized, A: StaticAllocator> PinSafePointer for UniqueRc<T, A> {}
2615
2616#[unstable(feature = "deref_pure_trait", issue = "87121")]
2617unsafe impl<T: ?Sized, A: Allocator> DerefPure for Rc<T, A> {}
2618
2619//#[unstable(feature = "unique_rc_arc", issue = "112566")]
2620#[unstable(feature = "deref_pure_trait", issue = "87121")]
2621unsafe impl<T: ?Sized, A: Allocator> DerefPure for UniqueRc<T, A> {}
2622
2623#[unstable(feature = "legacy_receiver_trait", issue = "none")]
2624impl<T: ?Sized> LegacyReceiver for Rc<T> {}
2625
2626#[stable(feature = "rust1", since = "1.0.0")]
2627unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Rc<T, A> {
2628 /// Drops the `Rc`.
2629 ///
2630 /// This will decrement the strong reference count. If the strong reference
2631 /// count reaches zero then the only other references (if any) are
2632 /// [`Weak`], so we `drop` the inner value.
2633 ///
2634 /// # Examples
2635 ///
2636 /// ```
2637 /// use std::rc::Rc;
2638 ///
2639 /// struct Foo;
2640 ///
2641 /// impl Drop for Foo {
2642 /// fn drop(&mut self) {
2643 /// println!("dropped!");
2644 /// }
2645 /// }
2646 ///
2647 /// let foo = Rc::new(Foo);
2648 /// let foo2 = Rc::clone(&foo);
2649 ///
2650 /// drop(foo); // Doesn't print anything
2651 /// drop(foo2); // Prints "dropped!"
2652 /// ```
2653 #[inline]
2654 fn drop(&mut self) {
2655 // ignore-tidy-undocumented-unsafe
2656 unsafe {
2657 self.inner().dec_strong();
2658 if self.inner().strong() == 0 {
2659 self.drop_slow();
2660 }
2661 }
2662 }
2663}
2664
2665#[stable(feature = "rust1", since = "1.0.0")]
2666impl<T: ?Sized, A: AllocatorClone> Clone for Rc<T, A> {
2667 /// Makes a clone of the `Rc` pointer.
2668 ///
2669 /// This creates another pointer to the same allocation, increasing the
2670 /// strong reference count.
2671 ///
2672 /// # Examples
2673 ///
2674 /// ```
2675 /// use std::rc::Rc;
2676 ///
2677 /// let five = Rc::new(5);
2678 ///
2679 /// let _ = Rc::clone(&five);
2680 /// ```
2681 #[inline]
2682 fn clone(&self) -> Self {
2683 // ignore-tidy-undocumented-unsafe
2684 unsafe {
2685 self.inner().inc_strong();
2686 Self::from_inner_in(self.ptr, self.alloc.clone())
2687 }
2688 }
2689}
2690
2691#[unstable(feature = "ergonomic_clones", issue = "132290")]
2692impl<T: ?Sized, A: AllocatorClone> UseCloned for Rc<T, A> {}
2693
2694#[unstable(feature = "share_trait", issue = "156756")]
2695impl<T: ?Sized, A: AllocatorClone> Share for Rc<T, A> {}
2696
2697#[cfg(not(no_global_oom_handling))]
2698#[stable(feature = "rust1", since = "1.0.0")]
2699impl<T: Default> Default for Rc<T> {
2700 /// Creates a new `Rc<T>`, with the `Default` value for `T`.
2701 ///
2702 /// # Examples
2703 ///
2704 /// ```
2705 /// use std::rc::Rc;
2706 ///
2707 /// let x: Rc<i32> = Default::default();
2708 /// assert_eq!(*x, 0);
2709 /// ```
2710 #[inline]
2711 fn default() -> Self {
2712 // ignore-tidy-undocumented-unsafe
2713 unsafe {
2714 Self::from_inner(Box::into_non_null(Box::write(
2715 Box::new_uninit(),
2716 RcInner { strong: Cell::new(1), weak: Cell::new(1), value: T::default() },
2717 )))
2718 }
2719 }
2720}
2721
2722#[cfg(not(no_global_oom_handling))]
2723#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
2724impl Default for Rc<str> {
2725 /// Creates an empty `str` inside an `Rc`.
2726 ///
2727 /// This may or may not share an allocation with other Rcs on the same thread.
2728 #[inline]
2729 fn default() -> Self {
2730 let rc = Rc::<[u8]>::default();
2731 // SAFETY: `[u8]` has the same layout as `str`.
2732 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const str) }
2733 }
2734}
2735
2736#[cfg(not(no_global_oom_handling))]
2737#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
2738impl<T> Default for Rc<[T]> {
2739 /// Creates an empty `[T]` inside an `Rc`.
2740 ///
2741 /// This may or may not share an allocation with other Rcs on the same thread.
2742 #[inline]
2743 fn default() -> Self {
2744 let arr: [T; 0] = [];
2745 Rc::from(arr)
2746 }
2747}
2748
2749#[cfg(not(no_global_oom_handling))]
2750#[stable(feature = "pin_default_impls", since = "1.91.0")]
2751impl<T> Default for Pin<Rc<T>>
2752where
2753 T: ?Sized,
2754 Rc<T>: Default,
2755{
2756 #[inline]
2757 fn default() -> Self {
2758 // SAFETY: We own and create the pinned pointer.
2759 unsafe { Pin::new_unchecked(Rc::<T>::default()) }
2760 }
2761}
2762
2763#[stable(feature = "rust1", since = "1.0.0")]
2764trait RcEqIdent<T: ?Sized + PartialEq, A: Allocator> {
2765 fn eq(&self, other: &Rc<T, A>) -> bool;
2766 fn ne(&self, other: &Rc<T, A>) -> bool;
2767}
2768
2769#[stable(feature = "rust1", since = "1.0.0")]
2770impl<T: ?Sized + PartialEq, A: Allocator> RcEqIdent<T, A> for Rc<T, A> {
2771 #[inline]
2772 default fn eq(&self, other: &Rc<T, A>) -> bool {
2773 **self == **other
2774 }
2775
2776 #[inline]
2777 default fn ne(&self, other: &Rc<T, A>) -> bool {
2778 **self != **other
2779 }
2780}
2781
2782// Hack to allow specializing on `Eq` even though `Eq` has a method.
2783#[unsafe(rustc_allow_lifetime_dependent_specialization)]
2784pub(crate) trait MarkerEq: PartialEq<Self> {}
2785
2786impl<T: ?Sized + Eq> MarkerEq for T {}
2787
2788/// We're doing this specialization here, and not as a more general optimization on `&T`, because it
2789/// would otherwise add a cost to all equality checks on refs. We assume that `Rc`s are used to
2790/// store large values, that are slow to clone, but also heavy to check for equality, causing this
2791/// cost to pay off more easily. It's also more likely to have two `Rc` clones, that point to
2792/// the same value, than two `&T`s.
2793///
2794/// We can only do this when `T: Eq` as a `PartialEq` might be deliberately irreflexive.
2795#[stable(feature = "rust1", since = "1.0.0")]
2796impl<T: ?Sized + MarkerEq, A: Allocator> RcEqIdent<T, A> for Rc<T, A> {
2797 #[inline]
2798 fn eq(&self, other: &Rc<T, A>) -> bool {
2799 ptr::eq(self.ptr.as_ptr(), other.ptr.as_ptr()) || **self == **other
2800 }
2801
2802 #[inline]
2803 fn ne(&self, other: &Rc<T, A>) -> bool {
2804 !ptr::eq(self.ptr.as_ptr(), other.ptr.as_ptr()) && **self != **other
2805 }
2806}
2807
2808#[stable(feature = "rust1", since = "1.0.0")]
2809impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for Rc<T, A> {
2810 /// Equality for two `Rc`s.
2811 ///
2812 /// Two `Rc`s are equal if their inner values are equal, even if they are
2813 /// stored in different allocation.
2814 ///
2815 /// If `T` also implements `Eq` (implying reflexivity of equality),
2816 /// two `Rc`s that point to the same allocation are
2817 /// always equal.
2818 ///
2819 /// # Examples
2820 ///
2821 /// ```
2822 /// use std::rc::Rc;
2823 ///
2824 /// let five = Rc::new(5);
2825 ///
2826 /// assert!(five == Rc::new(5));
2827 /// ```
2828 #[inline]
2829 fn eq(&self, other: &Rc<T, A>) -> bool {
2830 RcEqIdent::eq(self, other)
2831 }
2832
2833 /// Inequality for two `Rc`s.
2834 ///
2835 /// Two `Rc`s are not equal if their inner values are not equal.
2836 ///
2837 /// If `T` also implements `Eq` (implying reflexivity of equality),
2838 /// two `Rc`s that point to the same allocation are
2839 /// always equal.
2840 ///
2841 /// # Examples
2842 ///
2843 /// ```
2844 /// use std::rc::Rc;
2845 ///
2846 /// let five = Rc::new(5);
2847 ///
2848 /// assert!(five != Rc::new(6));
2849 /// ```
2850 #[inline]
2851 fn ne(&self, other: &Rc<T, A>) -> bool {
2852 RcEqIdent::ne(self, other)
2853 }
2854}
2855
2856#[stable(feature = "rust1", since = "1.0.0")]
2857impl<T: ?Sized + Eq, A: Allocator> Eq for Rc<T, A> {}
2858
2859#[stable(feature = "rust1", since = "1.0.0")]
2860impl<T: ?Sized + PartialOrd, A: Allocator> PartialOrd for Rc<T, A> {
2861 /// Partial comparison for two `Rc`s.
2862 ///
2863 /// The two are compared by calling `partial_cmp()` on their inner values.
2864 ///
2865 /// # Examples
2866 ///
2867 /// ```
2868 /// use std::rc::Rc;
2869 /// use std::cmp::Ordering;
2870 ///
2871 /// let five = Rc::new(5);
2872 ///
2873 /// assert_eq!(Some(Ordering::Less), five.partial_cmp(&Rc::new(6)));
2874 /// ```
2875 #[inline(always)]
2876 fn partial_cmp(&self, other: &Rc<T, A>) -> Option<Ordering> {
2877 (**self).partial_cmp(&**other)
2878 }
2879
2880 /// Less-than comparison for two `Rc`s.
2881 ///
2882 /// The two are compared by calling `<` on their inner values.
2883 ///
2884 /// # Examples
2885 ///
2886 /// ```
2887 /// use std::rc::Rc;
2888 ///
2889 /// let five = Rc::new(5);
2890 ///
2891 /// assert!(five < Rc::new(6));
2892 /// ```
2893 #[inline(always)]
2894 fn lt(&self, other: &Rc<T, A>) -> bool {
2895 **self < **other
2896 }
2897
2898 /// 'Less than or equal to' comparison for two `Rc`s.
2899 ///
2900 /// The two are compared by calling `<=` on their inner values.
2901 ///
2902 /// # Examples
2903 ///
2904 /// ```
2905 /// use std::rc::Rc;
2906 ///
2907 /// let five = Rc::new(5);
2908 ///
2909 /// assert!(five <= Rc::new(5));
2910 /// ```
2911 #[inline(always)]
2912 fn le(&self, other: &Rc<T, A>) -> bool {
2913 **self <= **other
2914 }
2915
2916 /// Greater-than comparison for two `Rc`s.
2917 ///
2918 /// The two are compared by calling `>` on their inner values.
2919 ///
2920 /// # Examples
2921 ///
2922 /// ```
2923 /// use std::rc::Rc;
2924 ///
2925 /// let five = Rc::new(5);
2926 ///
2927 /// assert!(five > Rc::new(4));
2928 /// ```
2929 #[inline(always)]
2930 fn gt(&self, other: &Rc<T, A>) -> bool {
2931 **self > **other
2932 }
2933
2934 /// 'Greater than or equal to' comparison for two `Rc`s.
2935 ///
2936 /// The two are compared by calling `>=` on their inner values.
2937 ///
2938 /// # Examples
2939 ///
2940 /// ```
2941 /// use std::rc::Rc;
2942 ///
2943 /// let five = Rc::new(5);
2944 ///
2945 /// assert!(five >= Rc::new(5));
2946 /// ```
2947 #[inline(always)]
2948 fn ge(&self, other: &Rc<T, A>) -> bool {
2949 **self >= **other
2950 }
2951}
2952
2953#[stable(feature = "rust1", since = "1.0.0")]
2954impl<T: ?Sized + Ord, A: Allocator> Ord for Rc<T, A> {
2955 /// Comparison for two `Rc`s.
2956 ///
2957 /// The two are compared by calling `cmp()` on their inner values.
2958 ///
2959 /// # Examples
2960 ///
2961 /// ```
2962 /// use std::rc::Rc;
2963 /// use std::cmp::Ordering;
2964 ///
2965 /// let five = Rc::new(5);
2966 ///
2967 /// assert_eq!(Ordering::Less, five.cmp(&Rc::new(6)));
2968 /// ```
2969 #[inline]
2970 fn cmp(&self, other: &Rc<T, A>) -> Ordering {
2971 (**self).cmp(&**other)
2972 }
2973}
2974
2975#[stable(feature = "rust1", since = "1.0.0")]
2976impl<T: ?Sized + Hash, A: Allocator> Hash for Rc<T, A> {
2977 fn hash<H: Hasher>(&self, state: &mut H) {
2978 (**self).hash(state);
2979 }
2980}
2981
2982#[stable(feature = "rust1", since = "1.0.0")]
2983impl<T: ?Sized + fmt::Display, A: Allocator> fmt::Display for Rc<T, A> {
2984 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2985 fmt::Display::fmt(&**self, f)
2986 }
2987}
2988
2989#[stable(feature = "rust1", since = "1.0.0")]
2990impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for Rc<T, A> {
2991 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2992 fmt::Debug::fmt(&**self, f)
2993 }
2994}
2995
2996#[stable(feature = "rust1", since = "1.0.0")]
2997impl<T: ?Sized, A: Allocator> fmt::Pointer for Rc<T, A> {
2998 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2999 fmt::Pointer::fmt(&(&raw const **self), f)
3000 }
3001}
3002
3003#[cfg(not(no_global_oom_handling))]
3004#[stable(feature = "from_for_ptrs", since = "1.6.0")]
3005impl<T> From<T> for Rc<T> {
3006 /// Converts a generic type `T` into an `Rc<T>`
3007 ///
3008 /// The conversion allocates on the heap and moves `t`
3009 /// from the stack into it.
3010 ///
3011 /// # Example
3012 /// ```rust
3013 /// # use std::rc::Rc;
3014 /// let x = 5;
3015 /// let rc = Rc::new(5);
3016 ///
3017 /// assert_eq!(Rc::from(x), rc);
3018 /// ```
3019 fn from(t: T) -> Self {
3020 Rc::new(t)
3021 }
3022}
3023
3024#[cfg(not(no_global_oom_handling))]
3025#[stable(feature = "shared_from_array", since = "1.74.0")]
3026impl<T, const N: usize> From<[T; N]> for Rc<[T]> {
3027 /// Converts a [`[T; N]`](prim@array) into an `Rc<[T]>`.
3028 ///
3029 /// The conversion moves the array into a newly allocated `Rc`.
3030 ///
3031 /// # Example
3032 ///
3033 /// ```
3034 /// # use std::rc::Rc;
3035 /// let original: [i32; 3] = [1, 2, 3];
3036 /// let shared: Rc<[i32]> = Rc::from(original);
3037 /// assert_eq!(&[1, 2, 3], &shared[..]);
3038 /// ```
3039 #[inline]
3040 fn from(v: [T; N]) -> Rc<[T]> {
3041 Rc::<[T; N]>::from(v)
3042 }
3043}
3044
3045#[cfg(not(no_global_oom_handling))]
3046#[stable(feature = "shared_from_slice", since = "1.21.0")]
3047impl<T: Clone> From<&[T]> for Rc<[T]> {
3048 /// Allocates a reference-counted slice and fills it by cloning `v`'s items.
3049 ///
3050 /// # Example
3051 ///
3052 /// ```
3053 /// # use std::rc::Rc;
3054 /// let original: &[i32] = &[1, 2, 3];
3055 /// let shared: Rc<[i32]> = Rc::from(original);
3056 /// assert_eq!(&[1, 2, 3], &shared[..]);
3057 /// ```
3058 #[inline]
3059 fn from(v: &[T]) -> Rc<[T]> {
3060 <Self as RcFromSlice<T>>::from_slice(v)
3061 }
3062}
3063
3064#[cfg(not(no_global_oom_handling))]
3065#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
3066impl<T: Clone> From<&mut [T]> for Rc<[T]> {
3067 /// Allocates a reference-counted slice and fills it by cloning `v`'s items.
3068 ///
3069 /// # Example
3070 ///
3071 /// ```
3072 /// # use std::rc::Rc;
3073 /// let mut original = [1, 2, 3];
3074 /// let original: &mut [i32] = &mut original;
3075 /// let shared: Rc<[i32]> = Rc::from(original);
3076 /// assert_eq!(&[1, 2, 3], &shared[..]);
3077 /// ```
3078 #[inline]
3079 fn from(v: &mut [T]) -> Rc<[T]> {
3080 Rc::from(&*v)
3081 }
3082}
3083
3084#[cfg(not(no_global_oom_handling))]
3085#[stable(feature = "shared_from_slice", since = "1.21.0")]
3086impl From<&str> for Rc<str> {
3087 /// Allocates a reference-counted string slice and copies `v` into it.
3088 ///
3089 /// # Example
3090 ///
3091 /// ```
3092 /// # use std::rc::Rc;
3093 /// let shared: Rc<str> = Rc::from("statue");
3094 /// assert_eq!("statue", &shared[..]);
3095 /// ```
3096 #[inline]
3097 fn from(v: &str) -> Rc<str> {
3098 let rc = Rc::<[u8]>::from(v.as_bytes());
3099 // ignore-tidy-undocumented-unsafe
3100 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const str) }
3101 }
3102}
3103
3104#[cfg(not(no_global_oom_handling))]
3105#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
3106impl From<&mut str> for Rc<str> {
3107 /// Allocates a reference-counted string slice and copies `v` into it.
3108 ///
3109 /// # Example
3110 ///
3111 /// ```
3112 /// # use std::rc::Rc;
3113 /// let mut original = String::from("statue");
3114 /// let original: &mut str = &mut original;
3115 /// let shared: Rc<str> = Rc::from(original);
3116 /// assert_eq!("statue", &shared[..]);
3117 /// ```
3118 #[inline]
3119 fn from(v: &mut str) -> Rc<str> {
3120 Rc::from(&*v)
3121 }
3122}
3123
3124#[cfg(not(no_global_oom_handling))]
3125#[stable(feature = "shared_from_slice", since = "1.21.0")]
3126impl From<String> for Rc<str> {
3127 /// Allocates a reference-counted string slice and copies `v` into it.
3128 ///
3129 /// # Example
3130 ///
3131 /// ```
3132 /// # use std::rc::Rc;
3133 /// let original: String = "statue".to_owned();
3134 /// let shared: Rc<str> = Rc::from(original);
3135 /// assert_eq!("statue", &shared[..]);
3136 /// ```
3137 #[inline]
3138 fn from(v: String) -> Rc<str> {
3139 Rc::from(&v[..])
3140 }
3141}
3142
3143#[cfg(not(no_global_oom_handling))]
3144#[stable(feature = "shared_from_slice", since = "1.21.0")]
3145impl<T: ?Sized, A: AllocatorNightly> From<Box<T, A>> for Rc<T, A> {
3146 /// Move a boxed object to a new, reference counted, allocation.
3147 ///
3148 /// # Example
3149 ///
3150 /// ```
3151 /// # use std::rc::Rc;
3152 /// let original: Box<i32> = Box::new(1);
3153 /// let shared: Rc<i32> = Rc::from(original);
3154 /// assert_eq!(1, *shared);
3155 /// ```
3156 #[inline]
3157 fn from(v: Box<T, A>) -> Rc<T, A> {
3158 Rc::from_box_in(v)
3159 }
3160}
3161
3162#[cfg(not(no_global_oom_handling))]
3163#[stable(feature = "shared_from_slice", since = "1.21.0")]
3164impl<T, A: AllocatorNightly> From<Vec<T, A>> for Rc<[T], A> {
3165 /// Allocates a reference-counted slice and moves `v`'s items into it.
3166 ///
3167 /// # Example
3168 ///
3169 /// ```
3170 /// # use std::rc::Rc;
3171 /// let unique: Vec<i32> = vec![1, 2, 3];
3172 /// let shared: Rc<[i32]> = Rc::from(unique);
3173 /// assert_eq!(&[1, 2, 3], &shared[..]);
3174 /// ```
3175 #[inline]
3176 fn from(v: Vec<T, A>) -> Rc<[T], A> {
3177 // ignore-tidy-undocumented-unsafe
3178 unsafe {
3179 let (vec_ptr, len, cap, alloc) = v.into_raw_parts_with_allocator();
3180
3181 let rc_ptr = Self::allocate_for_slice_in(len, &alloc);
3182 ptr::copy_nonoverlapping(vec_ptr, (&raw mut (*rc_ptr).value) as *mut T, len);
3183
3184 // Create a `Vec<T, &A>` with length 0, to deallocate the buffer
3185 // without dropping its contents or the allocator
3186 let _ = Vec::from_raw_parts_in(vec_ptr, 0, cap, &alloc);
3187
3188 Self::from_ptr_in(rc_ptr, alloc)
3189 }
3190 }
3191}
3192
3193#[stable(feature = "shared_from_cow", since = "1.45.0")]
3194impl<'a, B> From<Cow<'a, B>> for Rc<B>
3195where
3196 B: ToOwned + ?Sized,
3197 Rc<B>: From<&'a B> + From<B::Owned>,
3198{
3199 /// Creates a reference-counted pointer from a clone-on-write pointer by
3200 /// copying its content.
3201 ///
3202 /// # Example
3203 ///
3204 /// ```rust
3205 /// # use std::rc::Rc;
3206 /// # use std::borrow::Cow;
3207 /// let cow: Cow<'_, str> = Cow::Borrowed("eggplant");
3208 /// let shared: Rc<str> = Rc::from(cow);
3209 /// assert_eq!("eggplant", &shared[..]);
3210 /// ```
3211 #[inline]
3212 fn from(cow: Cow<'a, B>) -> Rc<B> {
3213 match cow {
3214 Cow::Borrowed(s) => Rc::from(s),
3215 Cow::Owned(s) => Rc::from(s),
3216 }
3217 }
3218}
3219
3220#[stable(feature = "shared_from_str", since = "1.62.0")]
3221impl From<Rc<str>> for Rc<[u8]> {
3222 /// Converts a reference-counted string slice into a byte slice.
3223 ///
3224 /// # Example
3225 ///
3226 /// ```
3227 /// # use std::rc::Rc;
3228 /// let string: Rc<str> = Rc::from("eggplant");
3229 /// let bytes: Rc<[u8]> = Rc::from(string);
3230 /// assert_eq!("eggplant".as_bytes(), bytes.as_ref());
3231 /// ```
3232 #[inline]
3233 fn from(rc: Rc<str>) -> Self {
3234 // SAFETY: `str` has the same layout as `[u8]`.
3235 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const [u8]) }
3236 }
3237}
3238
3239#[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
3240impl<T, A: Allocator, const N: usize> TryFrom<Rc<[T], A>> for Rc<[T; N], A> {
3241 type Error = Rc<[T], A>;
3242
3243 fn try_from(boxed_slice: Rc<[T], A>) -> Result<Self, Self::Error> {
3244 if boxed_slice.len() == N {
3245 let (ptr, alloc) = Rc::into_inner_with_allocator(boxed_slice);
3246 // ignore-tidy-undocumented-unsafe
3247 Ok(unsafe { Rc::from_inner_in(ptr.cast(), alloc) })
3248 } else {
3249 Err(boxed_slice)
3250 }
3251 }
3252}
3253
3254#[cfg(not(no_global_oom_handling))]
3255#[stable(feature = "shared_from_iter", since = "1.37.0")]
3256impl<T> FromIterator<T> for Rc<[T]> {
3257 /// Takes each element in the `Iterator` and collects it into an `Rc<[T]>`.
3258 ///
3259 /// # Performance characteristics
3260 ///
3261 /// ## The general case
3262 ///
3263 /// In the general case, collecting into `Rc<[T]>` is done by first
3264 /// collecting into a `Vec<T>`. That is, when writing the following:
3265 ///
3266 /// ```rust
3267 /// # use std::rc::Rc;
3268 /// let evens: Rc<[u8]> = (0..10).filter(|&x| x % 2 == 0).collect();
3269 /// # assert_eq!(&*evens, &[0, 2, 4, 6, 8]);
3270 /// ```
3271 ///
3272 /// this behaves as if we wrote:
3273 ///
3274 /// ```rust
3275 /// # use std::rc::Rc;
3276 /// let evens: Rc<[u8]> = (0..10).filter(|&x| x % 2 == 0)
3277 /// .collect::<Vec<_>>() // The first set of allocations happens here.
3278 /// .into(); // A second allocation for `Rc<[T]>` happens here.
3279 /// # assert_eq!(&*evens, &[0, 2, 4, 6, 8]);
3280 /// ```
3281 ///
3282 /// This will allocate as many times as needed for constructing the `Vec<T>`
3283 /// and then it will allocate once for turning the `Vec<T>` into the `Rc<[T]>`.
3284 ///
3285 /// ## Iterators of known length
3286 ///
3287 /// When your `Iterator` implements `TrustedLen` and is of an exact size,
3288 /// a single allocation will be made for the `Rc<[T]>`. For example:
3289 ///
3290 /// ```rust
3291 /// # use std::rc::Rc;
3292 /// let evens: Rc<[u8]> = (0..10).collect(); // Just a single allocation happens here.
3293 /// # assert_eq!(&*evens, &*(0..10).collect::<Vec<_>>());
3294 /// ```
3295 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
3296 ToRcSlice::to_rc_slice(iter.into_iter())
3297 }
3298}
3299
3300/// Specialization trait used for collecting into `Rc<[T]>`.
3301#[cfg(not(no_global_oom_handling))]
3302trait ToRcSlice<T>: Iterator<Item = T> + Sized {
3303 fn to_rc_slice(self) -> Rc<[T]>;
3304}
3305
3306#[cfg(not(no_global_oom_handling))]
3307impl<T, I: Iterator<Item = T>> ToRcSlice<T> for I {
3308 default fn to_rc_slice(self) -> Rc<[T]> {
3309 self.collect::<Vec<T>>().into()
3310 }
3311}
3312
3313#[cfg(not(no_global_oom_handling))]
3314impl<T, I: iter::TrustedLen<Item = T>> ToRcSlice<T> for I {
3315 fn to_rc_slice(self) -> Rc<[T]> {
3316 // This is the case for a `TrustedLen` iterator.
3317 let (low, high) = self.size_hint();
3318 if let Some(high) = high {
3319 debug_assert_eq!(
3320 low,
3321 high,
3322 "TrustedLen iterator's size hint is not exact: {:?}",
3323 (low, high)
3324 );
3325
3326 // SAFETY: We need to ensure that the iterator has an exact length and we have.
3327 unsafe { Rc::from_iter_exact(self, low) }
3328 } else {
3329 // TrustedLen contract guarantees that `upper_bound == None` implies an iterator
3330 // length exceeding `usize::MAX`.
3331 // The default implementation would collect into a vec which would panic.
3332 // Thus we panic here immediately without invoking `Vec` code.
3333 panic!("capacity overflow");
3334 }
3335 }
3336}
3337
3338/// `Weak` is a version of [`Rc`] that holds a non-owning reference to the
3339/// managed allocation.
3340///
3341/// The allocation is accessed by calling [`upgrade`] on the `Weak`
3342/// pointer, which returns an <code>[Option]<[Rc]\<T>></code>.
3343///
3344/// Since a `Weak` reference does not count towards ownership, it will not
3345/// prevent the value stored in the allocation from being dropped, and `Weak` itself makes no
3346/// guarantees about the value still being present. Thus it may return [`None`]
3347/// when [`upgrade`]d. Note however that a `Weak` reference *does* prevent the allocation
3348/// itself (the backing store) from being deallocated.
3349///
3350/// A `Weak` pointer is useful for keeping a temporary reference to the allocation
3351/// managed by [`Rc`] without preventing its inner value from being dropped. It is also used to
3352/// prevent circular references between [`Rc`] pointers, since mutual owning references
3353/// would never allow either [`Rc`] to be dropped. For example, a tree could
3354/// have strong [`Rc`] pointers from parent nodes to children, and `Weak`
3355/// pointers from children back to their parents.
3356///
3357/// The typical way to obtain a `Weak` pointer is to call [`Rc::downgrade`].
3358///
3359/// [`upgrade`]: Weak::upgrade
3360#[stable(feature = "rc_weak", since = "1.4.0")]
3361#[rustc_diagnostic_item = "RcWeak"]
3362pub struct Weak<
3363 T: ?Sized,
3364 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")] A: Allocator = Global,
3365> {
3366 // This is a `NonNull` to allow optimizing the size of this type in enums,
3367 // but it is not necessarily a valid pointer.
3368 // `Weak::new` sets this to `usize::MAX` so that it doesn’t need
3369 // to allocate space on the heap. That's not a value a real pointer
3370 // will ever have because RcInner has alignment at least 2.
3371 ptr: NonNull<RcInner<T>>,
3372 alloc: A,
3373}
3374
3375#[stable(feature = "rc_weak", since = "1.4.0")]
3376impl<T: ?Sized, A: Allocator> !Send for Weak<T, A> {}
3377#[stable(feature = "rc_weak", since = "1.4.0")]
3378impl<T: ?Sized, A: Allocator> !Sync for Weak<T, A> {}
3379
3380#[unstable(feature = "coerce_unsized", issue = "18598")]
3381impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Weak<U, A>> for Weak<T, A> {}
3382
3383#[unstable(feature = "dispatch_from_dyn", issue = "none")]
3384impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Weak<U>> for Weak<T> {}
3385
3386// SAFETY: `Weak::clone` doesn't access any `Cell`s which could contain the `Weak` being cloned.
3387#[unstable(feature = "cell_get_cloned", issue = "145329")]
3388unsafe impl<T: ?Sized> CloneFromCell for Weak<T> {}
3389
3390impl<T> Weak<T> {
3391 /// Constructs a new `Weak<T>`, without allocating any memory.
3392 /// Calling [`upgrade`] on the return value always gives [`None`].
3393 ///
3394 /// [`upgrade`]: Weak::upgrade
3395 ///
3396 /// # Examples
3397 ///
3398 /// ```
3399 /// use std::rc::Weak;
3400 ///
3401 /// let empty: Weak<i64> = Weak::new();
3402 /// assert!(empty.upgrade().is_none());
3403 /// ```
3404 #[inline]
3405 #[stable(feature = "downgraded_weak", since = "1.10.0")]
3406 #[rustc_const_stable(feature = "const_weak_new", since = "1.73.0")]
3407 #[must_use]
3408 pub const fn new() -> Weak<T> {
3409 Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc: Global }
3410 }
3411}
3412
3413impl<T, A: Allocator> Weak<T, A> {
3414 /// Constructs a new `Weak<T>`, without allocating any memory, technically in the provided
3415 /// allocator.
3416 /// Calling [`upgrade`] on the return value always gives [`None`].
3417 ///
3418 /// [`upgrade`]: Weak::upgrade
3419 ///
3420 /// # Examples
3421 ///
3422 /// ```
3423 /// use std::rc::Weak;
3424 ///
3425 /// let empty: Weak<i64> = Weak::new();
3426 /// assert!(empty.upgrade().is_none());
3427 /// ```
3428 #[inline]
3429 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
3430 pub fn new_in(alloc: A) -> Weak<T, A> {
3431 Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc }
3432 }
3433}
3434
3435pub(crate) fn is_dangling<T: ?Sized>(ptr: *const T) -> bool {
3436 (ptr.cast::<()>()).addr() == usize::MAX
3437}
3438
3439/// Helper type to allow accessing the reference counts without
3440/// making any assertions about the data field.
3441struct WeakInner<'a> {
3442 weak: &'a Cell<usize>,
3443 strong: &'a Cell<usize>,
3444}
3445
3446impl<T: ?Sized> Weak<T> {
3447 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
3448 ///
3449 /// This can be used to safely get a strong reference (by calling [`upgrade`]
3450 /// later) or to deallocate the weak count by dropping the `Weak<T>`.
3451 ///
3452 /// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
3453 /// as these don't own anything; the method still works on them).
3454 ///
3455 /// # Safety
3456 ///
3457 /// The pointer must have originated from the [`into_raw`] and must still own its potential
3458 /// weak reference, and `ptr` must point to a block of memory allocated by the global allocator.
3459 ///
3460 /// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
3461 /// takes ownership of one weak reference currently represented as a raw pointer (the weak
3462 /// count is not modified by this operation) and therefore it must be paired with a previous
3463 /// call to [`into_raw`].
3464 ///
3465 /// # Examples
3466 ///
3467 /// ```
3468 /// use std::rc::{Rc, Weak};
3469 ///
3470 /// let strong = Rc::new("hello".to_owned());
3471 ///
3472 /// let raw_1 = Rc::downgrade(&strong).into_raw();
3473 /// let raw_2 = Rc::downgrade(&strong).into_raw();
3474 ///
3475 /// assert_eq!(2, Rc::weak_count(&strong));
3476 ///
3477 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
3478 /// assert_eq!(1, Rc::weak_count(&strong));
3479 ///
3480 /// drop(strong);
3481 ///
3482 /// // Decrement the last weak count.
3483 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
3484 /// ```
3485 ///
3486 /// [`into_raw`]: Weak::into_raw
3487 /// [`upgrade`]: Weak::upgrade
3488 /// [`new`]: Weak::new
3489 #[inline]
3490 #[stable(feature = "weak_into_raw", since = "1.45.0")]
3491 pub unsafe fn from_raw(ptr: *const T) -> Self {
3492 // SAFETY: Upheld by caller.
3493 unsafe { Self::from_raw_in(ptr, Global) }
3494 }
3495
3496 /// Consumes the `Weak<T>` and turns it into a raw pointer.
3497 ///
3498 /// This converts the weak pointer into a raw pointer, while still preserving the ownership of
3499 /// one weak reference (the weak count is not modified by this operation). It can be turned
3500 /// back into the `Weak<T>` with [`from_raw`].
3501 ///
3502 /// The same restrictions of accessing the target of the pointer as with
3503 /// [`as_ptr`] apply.
3504 ///
3505 /// # Examples
3506 ///
3507 /// ```
3508 /// use std::rc::{Rc, Weak};
3509 ///
3510 /// let strong = Rc::new("hello".to_owned());
3511 /// let weak = Rc::downgrade(&strong);
3512 /// let raw = weak.into_raw();
3513 ///
3514 /// assert_eq!(1, Rc::weak_count(&strong));
3515 /// assert_eq!("hello", unsafe { &*raw });
3516 ///
3517 /// drop(unsafe { Weak::from_raw(raw) });
3518 /// assert_eq!(0, Rc::weak_count(&strong));
3519 /// ```
3520 ///
3521 /// [`from_raw`]: Weak::from_raw
3522 /// [`as_ptr`]: Weak::as_ptr
3523 #[must_use = "losing the pointer will leak memory"]
3524 #[stable(feature = "weak_into_raw", since = "1.45.0")]
3525 pub fn into_raw(self) -> *const T {
3526 mem::ManuallyDrop::new(self).as_ptr()
3527 }
3528}
3529
3530impl<T: ?Sized, A: Allocator> Weak<T, A> {
3531 /// Returns a reference to the underlying allocator.
3532 #[inline]
3533 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
3534 pub fn allocator(&self) -> &A {
3535 &self.alloc
3536 }
3537
3538 /// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
3539 ///
3540 /// The pointer is valid only if there are some strong references. The pointer may be dangling,
3541 /// unaligned or even [`null`] otherwise.
3542 ///
3543 /// # Examples
3544 ///
3545 /// ```
3546 /// use std::rc::Rc;
3547 /// use std::ptr;
3548 ///
3549 /// let strong = Rc::new("hello".to_owned());
3550 /// let weak = Rc::downgrade(&strong);
3551 /// // Both point to the same object
3552 /// assert!(ptr::eq(&*strong, weak.as_ptr()));
3553 /// // The strong here keeps it alive, so we can still access the object.
3554 /// assert_eq!("hello", unsafe { &*weak.as_ptr() });
3555 ///
3556 /// drop(strong);
3557 /// // But not any more. We can do weak.as_ptr(), but accessing the pointer would lead to
3558 /// // undefined behavior.
3559 /// // assert_eq!("hello", unsafe { &*weak.as_ptr() });
3560 /// ```
3561 ///
3562 /// [`null`]: ptr::null
3563 #[must_use]
3564 #[stable(feature = "rc_as_ptr", since = "1.45.0")]
3565 pub fn as_ptr(&self) -> *const T {
3566 let ptr: *mut RcInner<T> = NonNull::as_ptr(self.ptr);
3567
3568 if is_dangling(ptr) {
3569 // If the pointer is dangling, we return the sentinel directly. This cannot be
3570 // a valid payload address, as the payload is at least as aligned as RcInner (usize).
3571 ptr as *const T
3572 } else {
3573 // SAFETY: if is_dangling returns false, then the pointer is dereferenceable.
3574 // The payload may be dropped at this point, and we have to maintain provenance,
3575 // so use raw pointer manipulation.
3576 unsafe { &raw mut (*ptr).value }
3577 }
3578 }
3579
3580 /// Consumes the `Weak<T>`, returning the wrapped pointer and allocator.
3581 ///
3582 /// This converts the weak pointer into a raw pointer, while still preserving the ownership of
3583 /// one weak reference (the weak count is not modified by this operation). It can be turned
3584 /// back into the `Weak<T>` with [`from_raw_in`].
3585 ///
3586 /// The same restrictions of accessing the target of the pointer as with
3587 /// [`as_ptr`] apply.
3588 ///
3589 /// # Examples
3590 ///
3591 /// ```
3592 /// #![feature(allocator_ext)]
3593 /// use std::rc::{Rc, Weak};
3594 /// use std::alloc::System;
3595 ///
3596 /// let strong = Rc::new_in("hello".to_owned(), System);
3597 /// let weak = Rc::downgrade(&strong);
3598 /// let (raw, alloc) = weak.into_raw_with_allocator();
3599 ///
3600 /// assert_eq!(1, Rc::weak_count(&strong));
3601 /// assert_eq!("hello", unsafe { &*raw });
3602 ///
3603 /// drop(unsafe { Weak::from_raw_in(raw, alloc) });
3604 /// assert_eq!(0, Rc::weak_count(&strong));
3605 /// ```
3606 ///
3607 /// [`from_raw_in`]: Weak::from_raw_in
3608 /// [`as_ptr`]: Weak::as_ptr
3609 #[must_use = "losing the pointer will leak memory"]
3610 #[inline]
3611 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
3612 pub fn into_raw_with_allocator(self) -> (*const T, A) {
3613 let this = mem::ManuallyDrop::new(self);
3614 let result = this.as_ptr();
3615 // SAFETY: `this` is ManuallyDrop so the allocator will not be double-dropped
3616 let alloc = unsafe { ptr::read(&this.alloc) };
3617 (result, alloc)
3618 }
3619
3620 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
3621 ///
3622 /// This can be used to safely get a strong reference (by calling [`upgrade`]
3623 /// later) or to deallocate the weak count by dropping the `Weak<T>`.
3624 ///
3625 /// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
3626 /// as these don't own anything; the method still works on them).
3627 ///
3628 /// # Safety
3629 ///
3630 /// The pointer must have originated from the [`into_raw`] and must still own its potential
3631 /// weak reference, and `ptr` must point to a block of memory allocated by `alloc`.
3632 ///
3633 /// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
3634 /// takes ownership of one weak reference currently represented as a raw pointer (the weak
3635 /// count is not modified by this operation) and therefore it must be paired with a previous
3636 /// call to [`into_raw`].
3637 ///
3638 /// # Examples
3639 ///
3640 /// ```
3641 /// use std::rc::{Rc, Weak};
3642 ///
3643 /// let strong = Rc::new("hello".to_owned());
3644 ///
3645 /// let raw_1 = Rc::downgrade(&strong).into_raw();
3646 /// let raw_2 = Rc::downgrade(&strong).into_raw();
3647 ///
3648 /// assert_eq!(2, Rc::weak_count(&strong));
3649 ///
3650 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
3651 /// assert_eq!(1, Rc::weak_count(&strong));
3652 ///
3653 /// drop(strong);
3654 ///
3655 /// // Decrement the last weak count.
3656 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
3657 /// ```
3658 ///
3659 /// [`into_raw`]: Weak::into_raw
3660 /// [`upgrade`]: Weak::upgrade
3661 /// [`new`]: Weak::new
3662 #[inline]
3663 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
3664 pub unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self {
3665 // See Weak::as_ptr for context on how the input pointer is derived.
3666
3667 let ptr = if is_dangling(ptr) {
3668 // This is a dangling Weak.
3669 ptr as *mut RcInner<T>
3670 } else {
3671 // Otherwise, we're guaranteed the pointer came from a nondangling Weak.
3672 // SAFETY: data_offset is safe to call, as ptr references a real (potentially dropped) T.
3673 let offset = unsafe { data_offset(ptr) };
3674 // Thus, we reverse the offset to get the whole RcInner.
3675 // SAFETY: the pointer originated from a Weak, so this offset is safe.
3676 unsafe { ptr.byte_sub(offset) as *mut RcInner<T> }
3677 };
3678
3679 // SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
3680 Weak { ptr: unsafe { NonNull::new_unchecked(ptr) }, alloc }
3681 }
3682
3683 /// Attempts to upgrade the `Weak` pointer to an [`Rc`], delaying
3684 /// dropping of the inner value if successful.
3685 ///
3686 /// Returns [`None`] in the following cases:
3687 ///
3688 /// 1. The inner value has since been dropped or moved out.
3689 ///
3690 /// 2. This `Weak` does not point to an allocation.
3691 ///
3692 /// 3. The owning reference this `Weak` is associated with is either not fully-constructed or does not allow an upgrade.
3693 ///
3694 /// # Examples
3695 ///
3696 /// ```
3697 /// use std::rc::Rc;
3698 ///
3699 /// let five = Rc::new(5);
3700 ///
3701 /// let weak_five = Rc::downgrade(&five);
3702 ///
3703 /// let strong_five: Option<Rc<_>> = weak_five.upgrade();
3704 /// assert!(strong_five.is_some());
3705 ///
3706 /// // Destroy all strong pointers.
3707 /// drop(strong_five);
3708 /// drop(five);
3709 ///
3710 /// assert!(weak_five.upgrade().is_none());
3711 /// ```
3712 #[must_use = "this returns a new `Rc`, \
3713 without modifying the original weak pointer"]
3714 #[stable(feature = "rc_weak", since = "1.4.0")]
3715 pub fn upgrade(&self) -> Option<Rc<T, A>>
3716 where
3717 A: AllocatorClone,
3718 {
3719 let inner = self.inner()?;
3720
3721 if inner.strong() == 0 {
3722 None
3723 } else {
3724 // ignore-tidy-undocumented-unsafe
3725 unsafe {
3726 inner.inc_strong();
3727 Some(Rc::from_inner_in(self.ptr, self.alloc.clone()))
3728 }
3729 }
3730 }
3731
3732 /// Gets the number of strong (`Rc`) pointers pointing to this allocation.
3733 ///
3734 /// If `self` was created using [`Weak::new`], this will return 0.
3735 #[must_use]
3736 #[stable(feature = "weak_counts", since = "1.41.0")]
3737 pub fn strong_count(&self) -> usize {
3738 if let Some(inner) = self.inner() { inner.strong() } else { 0 }
3739 }
3740
3741 /// Gets the number of `Weak` pointers pointing to this allocation.
3742 ///
3743 /// If no strong pointers remain, this will return zero.
3744 #[must_use]
3745 #[stable(feature = "weak_counts", since = "1.41.0")]
3746 pub fn weak_count(&self) -> usize {
3747 if let Some(inner) = self.inner() {
3748 if inner.strong() > 0 {
3749 inner.weak() - 1 // subtract the implicit weak ptr
3750 } else {
3751 0
3752 }
3753 } else {
3754 0
3755 }
3756 }
3757
3758 /// Returns `None` when the pointer is dangling and there is no allocated `RcInner`,
3759 /// (i.e., when this `Weak` was created by `Weak::new`).
3760 #[inline]
3761 fn inner(&self) -> Option<WeakInner<'_>> {
3762 if is_dangling(self.ptr.as_ptr()) {
3763 None
3764 } else {
3765 // We are careful to *not* create a reference covering the "data" field, as
3766 // the field may be mutated concurrently (for example, if the last `Rc`
3767 // is dropped, the data field will be dropped in-place).
3768 // ignore-tidy-undocumented-unsafe
3769 Some(unsafe {
3770 let ptr = self.ptr.as_ptr();
3771 WeakInner { strong: &(*ptr).strong, weak: &(*ptr).weak }
3772 })
3773 }
3774 }
3775
3776 /// Returns `true` if the two `Weak`s point to the same allocation similar to [`ptr::eq`], or if
3777 /// both don't point to any allocation (because they were created with `Weak::new()`). However,
3778 /// this function ignores the metadata of `dyn Trait` pointers.
3779 ///
3780 /// # Notes
3781 ///
3782 /// Since this compares pointers it means that `Weak::new()` will equal each
3783 /// other, even though they don't point to any allocation.
3784 ///
3785 /// # Examples
3786 ///
3787 /// ```
3788 /// use std::rc::Rc;
3789 ///
3790 /// let first_rc = Rc::new(5);
3791 /// let first = Rc::downgrade(&first_rc);
3792 /// let second = Rc::downgrade(&first_rc);
3793 ///
3794 /// assert!(first.ptr_eq(&second));
3795 ///
3796 /// let third_rc = Rc::new(5);
3797 /// let third = Rc::downgrade(&third_rc);
3798 ///
3799 /// assert!(!first.ptr_eq(&third));
3800 /// ```
3801 ///
3802 /// Comparing `Weak::new`.
3803 ///
3804 /// ```
3805 /// use std::rc::{Rc, Weak};
3806 ///
3807 /// let first = Weak::new();
3808 /// let second = Weak::new();
3809 /// assert!(first.ptr_eq(&second));
3810 ///
3811 /// let third_rc = Rc::new(());
3812 /// let third = Rc::downgrade(&third_rc);
3813 /// assert!(!first.ptr_eq(&third));
3814 /// ```
3815 #[inline]
3816 #[must_use]
3817 #[stable(feature = "weak_ptr_eq", since = "1.39.0")]
3818 pub fn ptr_eq(&self, other: &Self) -> bool {
3819 ptr::addr_eq(self.ptr.as_ptr(), other.ptr.as_ptr())
3820 }
3821}
3822
3823#[stable(feature = "rc_weak", since = "1.4.0")]
3824unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Weak<T, A> {
3825 /// Drops the `Weak` pointer.
3826 ///
3827 /// # Examples
3828 ///
3829 /// ```
3830 /// use std::rc::{Rc, Weak};
3831 ///
3832 /// struct Foo;
3833 ///
3834 /// impl Drop for Foo {
3835 /// fn drop(&mut self) {
3836 /// println!("dropped!");
3837 /// }
3838 /// }
3839 ///
3840 /// let foo = Rc::new(Foo);
3841 /// let weak_foo = Rc::downgrade(&foo);
3842 /// let other_weak_foo = Weak::clone(&weak_foo);
3843 ///
3844 /// drop(weak_foo); // Doesn't print anything
3845 /// drop(foo); // Prints "dropped!"
3846 ///
3847 /// assert!(other_weak_foo.upgrade().is_none());
3848 /// ```
3849 fn drop(&mut self) {
3850 let inner = if let Some(inner) = self.inner() { inner } else { return };
3851
3852 inner.dec_weak();
3853 // the weak count starts at 1, and will only go to zero if all
3854 // the strong pointers have disappeared.
3855 if inner.weak() == 0 {
3856 // ignore-tidy-undocumented-unsafe
3857 unsafe {
3858 self.alloc.deallocate(self.ptr.cast(), Layout::for_value_raw(self.ptr.as_ptr()));
3859 }
3860 }
3861 }
3862}
3863
3864#[stable(feature = "rc_weak", since = "1.4.0")]
3865impl<T: ?Sized, A: AllocatorClone> Clone for Weak<T, A> {
3866 /// Makes a clone of the `Weak` pointer that points to the same allocation.
3867 ///
3868 /// # Examples
3869 ///
3870 /// ```
3871 /// use std::rc::{Rc, Weak};
3872 ///
3873 /// let weak_five = Rc::downgrade(&Rc::new(5));
3874 ///
3875 /// let _ = Weak::clone(&weak_five);
3876 /// ```
3877 #[inline]
3878 fn clone(&self) -> Weak<T, A> {
3879 if let Some(inner) = self.inner() {
3880 inner.inc_weak()
3881 }
3882 Weak { ptr: self.ptr, alloc: self.alloc.clone() }
3883 }
3884}
3885
3886#[unstable(feature = "ergonomic_clones", issue = "132290")]
3887impl<T: ?Sized, A: AllocatorClone> UseCloned for Weak<T, A> {}
3888
3889#[stable(feature = "rc_weak", since = "1.4.0")]
3890impl<T: ?Sized, A: Allocator> fmt::Debug for Weak<T, A> {
3891 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3892 write!(f, "(Weak)")
3893 }
3894}
3895
3896#[stable(feature = "downgraded_weak", since = "1.10.0")]
3897impl<T> Default for Weak<T> {
3898 /// Constructs a new `Weak<T>`, without allocating any memory.
3899 /// Calling [`upgrade`] on the return value always gives [`None`].
3900 ///
3901 /// [`upgrade`]: Weak::upgrade
3902 ///
3903 /// # Examples
3904 ///
3905 /// ```
3906 /// use std::rc::Weak;
3907 ///
3908 /// let empty: Weak<i64> = Default::default();
3909 /// assert!(empty.upgrade().is_none());
3910 /// ```
3911 fn default() -> Weak<T> {
3912 Weak::new()
3913 }
3914}
3915
3916// NOTE: If you mem::forget Rcs (or Weaks), drop is skipped and the ref-count
3917// is not decremented, meaning the ref-count can overflow, and then you can
3918// free the allocation while outstanding Rcs (or Weaks) exist, which would be
3919// unsound. We abort because this is such a degenerate scenario that we don't
3920// care about what happens -- no real program should ever experience this.
3921//
3922// This should have negligible overhead since you don't actually need to
3923// clone these much in Rust thanks to ownership and move-semantics.
3924
3925#[doc(hidden)]
3926trait RcInnerPtr {
3927 fn weak_ref(&self) -> &Cell<usize>;
3928 fn strong_ref(&self) -> &Cell<usize>;
3929
3930 #[inline]
3931 fn strong(&self) -> usize {
3932 self.strong_ref().get()
3933 }
3934
3935 #[inline]
3936 fn inc_strong(&self) {
3937 let strong = self.strong();
3938
3939 // We insert an `assume` here to hint LLVM at an otherwise
3940 // missed optimization.
3941 // SAFETY: The reference count will never be zero when this is
3942 // called.
3943 unsafe {
3944 hint::assert_unchecked(strong != 0);
3945 }
3946
3947 let strong = strong.wrapping_add(1);
3948 self.strong_ref().set(strong);
3949
3950 // We want to abort on overflow instead of dropping the value.
3951 // Checking for overflow after the store instead of before
3952 // allows for slightly better code generation.
3953 if intrinsics::unlikely(strong == 0) {
3954 intrinsics::abort_immediate();
3955 }
3956 }
3957
3958 #[inline]
3959 fn dec_strong(&self) {
3960 self.strong_ref().set(self.strong() - 1);
3961 }
3962
3963 #[inline]
3964 fn weak(&self) -> usize {
3965 self.weak_ref().get()
3966 }
3967
3968 #[inline]
3969 fn inc_weak(&self) {
3970 let weak = self.weak();
3971
3972 // We insert an `assume` here to hint LLVM at an otherwise
3973 // missed optimization.
3974 // SAFETY: The reference count will never be zero when this is
3975 // called.
3976 unsafe {
3977 hint::assert_unchecked(weak != 0);
3978 }
3979
3980 let weak = weak.wrapping_add(1);
3981 self.weak_ref().set(weak);
3982
3983 // We want to abort on overflow instead of dropping the value.
3984 // Checking for overflow after the store instead of before
3985 // allows for slightly better code generation.
3986 if intrinsics::unlikely(weak == 0) {
3987 intrinsics::abort_immediate();
3988 }
3989 }
3990
3991 #[inline]
3992 fn dec_weak(&self) {
3993 self.weak_ref().set(self.weak() - 1);
3994 }
3995}
3996
3997impl<T: ?Sized> RcInnerPtr for RcInner<T> {
3998 #[inline(always)]
3999 fn weak_ref(&self) -> &Cell<usize> {
4000 &self.weak
4001 }
4002
4003 #[inline(always)]
4004 fn strong_ref(&self) -> &Cell<usize> {
4005 &self.strong
4006 }
4007}
4008
4009impl<'a> RcInnerPtr for WeakInner<'a> {
4010 #[inline(always)]
4011 fn weak_ref(&self) -> &Cell<usize> {
4012 self.weak
4013 }
4014
4015 #[inline(always)]
4016 fn strong_ref(&self) -> &Cell<usize> {
4017 self.strong
4018 }
4019}
4020
4021#[stable(feature = "rust1", since = "1.0.0")]
4022impl<T: ?Sized, A: Allocator> borrow::Borrow<T> for Rc<T, A> {
4023 fn borrow(&self) -> &T {
4024 self
4025 }
4026}
4027
4028#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
4029impl<T: ?Sized, A: Allocator> AsRef<T> for Rc<T, A> {
4030 fn as_ref(&self) -> &T {
4031 self
4032 }
4033}
4034
4035#[stable(feature = "pin", since = "1.33.0")]
4036impl<T: ?Sized, A: Allocator> Unpin for Rc<T, A> {}
4037
4038/// Gets the offset within an `RcInner` for the payload behind a pointer.
4039///
4040/// # Safety
4041///
4042/// The pointer must point to (and have valid metadata for) a previously
4043/// valid instance of T, but the T is allowed to be dropped.
4044unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> usize {
4045 // Align the unsized value to the end of the RcInner.
4046 // Because RcInner is repr(C), it will always be the last field in memory.
4047 // SAFETY: since the only unsized types possible are slices, trait objects,
4048 // and extern types, the input safety requirement is currently enough to
4049 // satisfy the requirements of Alignment::of_val_raw; this is an implementation
4050 // detail of the language that must not be relied upon outside of std.
4051 unsafe { data_offset_alignment(Alignment::of_val_raw(ptr)) }
4052}
4053
4054#[inline]
4055fn data_offset_alignment(alignment: Alignment) -> usize {
4056 let layout = Layout::new::<RcInner<()>>();
4057 layout.size() + layout.padding_needed_for(alignment)
4058}
4059
4060/// A uniquely owned [`Rc`].
4061///
4062/// This represents an `Rc` that is known to be uniquely owned -- that is, have exactly one strong
4063/// reference. Multiple weak pointers can be created, but attempts to upgrade those to strong
4064/// references will fail unless the `UniqueRc` they point to has been converted into a regular `Rc`.
4065///
4066/// Because they are uniquely owned, the contents of a `UniqueRc` can be freely mutated. A common
4067/// use case is to have an object be mutable during its initialization phase but then have it become
4068/// immutable and converted to a normal `Rc`.
4069///
4070/// This can be used as a flexible way to create cyclic data structures, as in the example below.
4071///
4072/// ```
4073/// #![feature(unique_rc_arc)]
4074/// use std::rc::{Rc, Weak, UniqueRc};
4075///
4076/// struct Gadget {
4077/// #[allow(dead_code)]
4078/// me: Weak<Gadget>,
4079/// }
4080///
4081/// fn create_gadget() -> Option<Rc<Gadget>> {
4082/// let mut rc = UniqueRc::new(Gadget {
4083/// me: Weak::new(),
4084/// });
4085/// rc.me = UniqueRc::downgrade(&rc);
4086/// Some(UniqueRc::into_rc(rc))
4087/// }
4088///
4089/// create_gadget().unwrap();
4090/// ```
4091///
4092/// An advantage of using `UniqueRc` over [`Rc::new_cyclic`] to build cyclic data structures is that
4093/// [`Rc::new_cyclic`]'s `data_fn` parameter cannot be async or return a [`Result`]. As shown in the
4094/// previous example, `UniqueRc` allows for more flexibility in the construction of cyclic data,
4095/// including fallible or async constructors.
4096#[unstable(feature = "unique_rc_arc", issue = "112566")]
4097pub struct UniqueRc<
4098 T: ?Sized,
4099 #[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")] A: Allocator = Global,
4100> {
4101 ptr: NonNull<RcInner<T>>,
4102 // Define the ownership of `RcInner<T>` for drop-check
4103 _marker: PhantomData<RcInner<T>>,
4104 // Invariance is necessary for soundness: once other `Weak`
4105 // references exist, we already have a form of shared mutability!
4106 _marker2: PhantomData<*mut T>,
4107 alloc: A,
4108}
4109
4110// Not necessary for correctness since `UniqueRc` contains `NonNull`,
4111// but having an explicit negative impl is nice for documentation purposes
4112// and results in nicer error messages.
4113#[unstable(feature = "unique_rc_arc", issue = "112566")]
4114impl<T: ?Sized, A: Allocator> !Send for UniqueRc<T, A> {}
4115
4116// Not necessary for correctness since `UniqueRc` contains `NonNull`,
4117// but having an explicit negative impl is nice for documentation purposes
4118// and results in nicer error messages.
4119#[unstable(feature = "unique_rc_arc", issue = "112566")]
4120impl<T: ?Sized, A: Allocator> !Sync for UniqueRc<T, A> {}
4121
4122#[unstable(feature = "unique_rc_arc", issue = "112566")]
4123impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<UniqueRc<U, A>>
4124 for UniqueRc<T, A>
4125{
4126}
4127
4128//#[unstable(feature = "unique_rc_arc", issue = "112566")]
4129#[unstable(feature = "dispatch_from_dyn", issue = "none")]
4130impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<UniqueRc<U>> for UniqueRc<T> {}
4131
4132#[unstable(feature = "unique_rc_arc", issue = "112566")]
4133impl<T: ?Sized + fmt::Display, A: Allocator> fmt::Display for UniqueRc<T, A> {
4134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4135 fmt::Display::fmt(&**self, f)
4136 }
4137}
4138
4139#[unstable(feature = "unique_rc_arc", issue = "112566")]
4140impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for UniqueRc<T, A> {
4141 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4142 fmt::Debug::fmt(&**self, f)
4143 }
4144}
4145
4146#[unstable(feature = "unique_rc_arc", issue = "112566")]
4147impl<T: ?Sized, A: Allocator> fmt::Pointer for UniqueRc<T, A> {
4148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4149 fmt::Pointer::fmt(&(&raw const **self), f)
4150 }
4151}
4152
4153#[unstable(feature = "unique_rc_arc", issue = "112566")]
4154impl<T: ?Sized, A: Allocator> borrow::Borrow<T> for UniqueRc<T, A> {
4155 fn borrow(&self) -> &T {
4156 self
4157 }
4158}
4159
4160#[unstable(feature = "unique_rc_arc", issue = "112566")]
4161impl<T: ?Sized, A: Allocator> borrow::BorrowMut<T> for UniqueRc<T, A> {
4162 fn borrow_mut(&mut self) -> &mut T {
4163 self
4164 }
4165}
4166
4167#[unstable(feature = "unique_rc_arc", issue = "112566")]
4168impl<T: ?Sized, A: Allocator> AsRef<T> for UniqueRc<T, A> {
4169 fn as_ref(&self) -> &T {
4170 self
4171 }
4172}
4173
4174#[unstable(feature = "unique_rc_arc", issue = "112566")]
4175impl<T: ?Sized, A: Allocator> AsMut<T> for UniqueRc<T, A> {
4176 fn as_mut(&mut self) -> &mut T {
4177 self
4178 }
4179}
4180
4181#[unstable(feature = "unique_rc_arc", issue = "112566")]
4182impl<T: ?Sized, A: Allocator> Unpin for UniqueRc<T, A> {}
4183
4184#[cfg(not(no_global_oom_handling))]
4185#[unstable(feature = "unique_rc_arc", issue = "112566")]
4186impl<T> From<T> for UniqueRc<T> {
4187 #[inline(always)]
4188 fn from(value: T) -> Self {
4189 Self::new(value)
4190 }
4191}
4192
4193#[unstable(feature = "unique_rc_arc", issue = "112566")]
4194impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for UniqueRc<T, A> {
4195 /// Equality for two `UniqueRc`s.
4196 ///
4197 /// Two `UniqueRc`s are equal if their inner values are equal.
4198 ///
4199 /// # Examples
4200 ///
4201 /// ```
4202 /// #![feature(unique_rc_arc)]
4203 /// use std::rc::UniqueRc;
4204 ///
4205 /// let five = UniqueRc::new(5);
4206 ///
4207 /// assert!(five == UniqueRc::new(5));
4208 /// ```
4209 #[inline]
4210 fn eq(&self, other: &Self) -> bool {
4211 PartialEq::eq(&**self, &**other)
4212 }
4213
4214 /// Inequality for two `UniqueRc`s.
4215 ///
4216 /// Two `UniqueRc`s are not equal if their inner values are not equal.
4217 ///
4218 /// # Examples
4219 ///
4220 /// ```
4221 /// #![feature(unique_rc_arc)]
4222 /// use std::rc::UniqueRc;
4223 ///
4224 /// let five = UniqueRc::new(5);
4225 ///
4226 /// assert!(five != UniqueRc::new(6));
4227 /// ```
4228 #[inline]
4229 fn ne(&self, other: &Self) -> bool {
4230 PartialEq::ne(&**self, &**other)
4231 }
4232}
4233
4234#[unstable(feature = "unique_rc_arc", issue = "112566")]
4235impl<T: ?Sized + PartialOrd, A: Allocator> PartialOrd for UniqueRc<T, A> {
4236 /// Partial comparison for two `UniqueRc`s.
4237 ///
4238 /// The two are compared by calling `partial_cmp()` on their inner values.
4239 ///
4240 /// # Examples
4241 ///
4242 /// ```
4243 /// #![feature(unique_rc_arc)]
4244 /// use std::rc::UniqueRc;
4245 /// use std::cmp::Ordering;
4246 ///
4247 /// let five = UniqueRc::new(5);
4248 ///
4249 /// assert_eq!(Some(Ordering::Less), five.partial_cmp(&UniqueRc::new(6)));
4250 /// ```
4251 #[inline(always)]
4252 fn partial_cmp(&self, other: &UniqueRc<T, A>) -> Option<Ordering> {
4253 (**self).partial_cmp(&**other)
4254 }
4255
4256 /// Less-than comparison for two `UniqueRc`s.
4257 ///
4258 /// The two are compared by calling `<` on their inner values.
4259 ///
4260 /// # Examples
4261 ///
4262 /// ```
4263 /// #![feature(unique_rc_arc)]
4264 /// use std::rc::UniqueRc;
4265 ///
4266 /// let five = UniqueRc::new(5);
4267 ///
4268 /// assert!(five < UniqueRc::new(6));
4269 /// ```
4270 #[inline(always)]
4271 fn lt(&self, other: &UniqueRc<T, A>) -> bool {
4272 **self < **other
4273 }
4274
4275 /// 'Less than or equal to' comparison for two `UniqueRc`s.
4276 ///
4277 /// The two are compared by calling `<=` on their inner values.
4278 ///
4279 /// # Examples
4280 ///
4281 /// ```
4282 /// #![feature(unique_rc_arc)]
4283 /// use std::rc::UniqueRc;
4284 ///
4285 /// let five = UniqueRc::new(5);
4286 ///
4287 /// assert!(five <= UniqueRc::new(5));
4288 /// ```
4289 #[inline(always)]
4290 fn le(&self, other: &UniqueRc<T, A>) -> bool {
4291 **self <= **other
4292 }
4293
4294 /// Greater-than comparison for two `UniqueRc`s.
4295 ///
4296 /// The two are compared by calling `>` on their inner values.
4297 ///
4298 /// # Examples
4299 ///
4300 /// ```
4301 /// #![feature(unique_rc_arc)]
4302 /// use std::rc::UniqueRc;
4303 ///
4304 /// let five = UniqueRc::new(5);
4305 ///
4306 /// assert!(five > UniqueRc::new(4));
4307 /// ```
4308 #[inline(always)]
4309 fn gt(&self, other: &UniqueRc<T, A>) -> bool {
4310 **self > **other
4311 }
4312
4313 /// 'Greater than or equal to' comparison for two `UniqueRc`s.
4314 ///
4315 /// The two are compared by calling `>=` on their inner values.
4316 ///
4317 /// # Examples
4318 ///
4319 /// ```
4320 /// #![feature(unique_rc_arc)]
4321 /// use std::rc::UniqueRc;
4322 ///
4323 /// let five = UniqueRc::new(5);
4324 ///
4325 /// assert!(five >= UniqueRc::new(5));
4326 /// ```
4327 #[inline(always)]
4328 fn ge(&self, other: &UniqueRc<T, A>) -> bool {
4329 **self >= **other
4330 }
4331}
4332
4333#[unstable(feature = "unique_rc_arc", issue = "112566")]
4334impl<T: ?Sized + Ord, A: Allocator> Ord for UniqueRc<T, A> {
4335 /// Comparison for two `UniqueRc`s.
4336 ///
4337 /// The two are compared by calling `cmp()` on their inner values.
4338 ///
4339 /// # Examples
4340 ///
4341 /// ```
4342 /// #![feature(unique_rc_arc)]
4343 /// use std::rc::UniqueRc;
4344 /// use std::cmp::Ordering;
4345 ///
4346 /// let five = UniqueRc::new(5);
4347 ///
4348 /// assert_eq!(Ordering::Less, five.cmp(&UniqueRc::new(6)));
4349 /// ```
4350 #[inline]
4351 fn cmp(&self, other: &UniqueRc<T, A>) -> Ordering {
4352 (**self).cmp(&**other)
4353 }
4354}
4355
4356#[unstable(feature = "unique_rc_arc", issue = "112566")]
4357impl<T: ?Sized + Eq, A: Allocator> Eq for UniqueRc<T, A> {}
4358
4359#[unstable(feature = "unique_rc_arc", issue = "112566")]
4360impl<T: ?Sized + Hash, A: Allocator> Hash for UniqueRc<T, A> {
4361 fn hash<H: Hasher>(&self, state: &mut H) {
4362 (**self).hash(state);
4363 }
4364}
4365
4366// Depends on A = Global
4367impl<T> UniqueRc<T> {
4368 /// Creates a new `UniqueRc`.
4369 ///
4370 /// Weak references to this `UniqueRc` can be created with [`UniqueRc::downgrade`]. Upgrading
4371 /// these weak references will fail before the `UniqueRc` has been converted into an [`Rc`].
4372 /// After converting the `UniqueRc` into an [`Rc`], any weak references created beforehand will
4373 /// point to the new [`Rc`].
4374 #[cfg(not(no_global_oom_handling))]
4375 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4376 pub fn new(value: T) -> Self {
4377 Self::new_in(value, Global)
4378 }
4379
4380 /// Like [`new`](Self::new), but returns an error if the allocation
4381 /// fails, instead of calling [`handle_alloc_error`].
4382 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4383 pub fn try_new(value: T) -> Result<Self, AllocError> {
4384 Self::try_new_in(value, Global)
4385 }
4386}
4387
4388impl<T, A: Allocator> UniqueRc<T, A> {
4389 /// Creates a new `UniqueRc` in the provided allocator.
4390 ///
4391 /// Weak references to this `UniqueRc` can be created with [`UniqueRc::downgrade`]. Upgrading
4392 /// these weak references will fail before the `UniqueRc` has been converted into an [`Rc`].
4393 /// After converting the `UniqueRc` into an [`Rc`], any weak references created beforehand will
4394 /// point to the new [`Rc`].
4395 #[cfg(not(no_global_oom_handling))]
4396 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4397 // #[unstable(feature = "allocator_api", issue = "163177")]
4398 #[must_use]
4399 pub fn new_in(value: T, alloc: A) -> Self {
4400 let (ptr, alloc) = Box::into_non_null_with_allocator(Box::new_in(
4401 RcInner {
4402 strong: Cell::new(0),
4403 // keep one weak reference so if all the weak pointers that are created are dropped
4404 // the UniqueRc still stays valid.
4405 weak: Cell::new(1),
4406 value,
4407 },
4408 alloc,
4409 ));
4410 Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc }
4411 }
4412
4413 /// Like [`new_in`](Self::new_in), but returns an error if the allocation
4414 /// fails, instead of calling [`handle_alloc_error`].
4415 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4416 // #[unstable(feature = "allocator_api", issue = "163177")]
4417 pub fn try_new_in(value: T, alloc: A) -> Result<Self, AllocError> {
4418 let (ptr, alloc) = Box::into_non_null_with_allocator(Box::try_new_in(
4419 RcInner {
4420 strong: Cell::new(0),
4421 // keep one weak reference so if all the weak pointers that are created are dropped
4422 // the UniqueRc still stays valid.
4423 weak: Cell::new(1),
4424 value,
4425 },
4426 alloc,
4427 )?);
4428 Ok(Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc })
4429 }
4430
4431 /// Consumes the `UniqueRc`, returning its wrapped value and allocator.
4432 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4433 // #[unstable(feature = "allocator_api", issue = "163177")]
4434 #[must_use]
4435 pub fn unwrap_with_allocator(this: Self) -> (T, A) {
4436 let inner_ptr = this.ptr;
4437 let (data_ptr, alloc) = Self::into_raw_with_allocator(this);
4438
4439 // SAFETY: Conceptually moves out of the `UniqueRc`.
4440 // We do not use the data inside ever again.
4441 let val = unsafe { data_ptr.read() };
4442
4443 // Drop the strong-weak ref
4444 drop(Weak { ptr: inner_ptr, alloc: &alloc });
4445
4446 (val, alloc)
4447 }
4448
4449 /// Consumes the `UniqueRc`, returning its wrapped value.
4450 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4451 #[must_use]
4452 pub fn unwrap(this: Self) -> T {
4453 Self::unwrap_with_allocator(this).0
4454 }
4455
4456 /// Maps the value in a `UniqueRc`, reusing the allocation if possible.
4457 ///
4458 /// `f` is called on a reference to the value in the `UniqueRc`, and the result is returned,
4459 /// also in a `UniqueRc`.
4460 ///
4461 /// Note: this is an associated function, which means that you have
4462 /// to call it as `UniqueRc::map(u, f)` instead of `u.map(f)`. This
4463 /// is so that there is no conflict with a method on the inner type.
4464 ///
4465 /// # Examples
4466 ///
4467 /// ```
4468 /// #![feature(unique_rc_arc)]
4469 ///
4470 /// use std::rc::UniqueRc;
4471 ///
4472 /// let r = UniqueRc::new(7);
4473 /// let new = UniqueRc::map(r, |i| i + 7);
4474 /// assert_eq!(*new, 14);
4475 /// ```
4476 #[cfg(not(no_global_oom_handling))]
4477 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4478 pub fn map<U>(this: Self, f: impl FnOnce(T) -> U) -> UniqueRc<U, A> {
4479 if size_of::<T>() == size_of::<U>()
4480 && align_of::<T>() == align_of::<U>()
4481 && UniqueRc::weak_count(&this) == 0
4482 {
4483 // ignore-tidy-undocumented-unsafe
4484 unsafe {
4485 let (ptr, alloc) = UniqueRc::into_raw_with_allocator(this);
4486 let value = ptr.read();
4487 let allocation =
4488 UniqueRc::from_raw_with_allocator(ptr.cast::<mem::MaybeUninit<U>>(), alloc);
4489
4490 UniqueRc::write(allocation, f(value))
4491 }
4492 } else {
4493 let (val, alloc) = UniqueRc::unwrap_with_allocator(this);
4494 UniqueRc::new_in(f(val), alloc)
4495 }
4496 }
4497
4498 /// Attempts to map the value in a `UniqueRc`, reusing the allocation if possible.
4499 ///
4500 /// `f` is called on a reference to the value in the `UniqueRc`, and if the operation succeeds,
4501 /// the result is returned, also in a `UniqueRc`.
4502 ///
4503 /// Note: this is an associated function, which means that you have
4504 /// to call it as `UniqueRc::try_map(u, f)` instead of `u.try_map(f)`. This
4505 /// is so that there is no conflict with a method on the inner type.
4506 ///
4507 /// # Examples
4508 ///
4509 /// ```
4510 /// #![feature(smart_pointer_try_map)]
4511 /// #![feature(unique_rc_arc)]
4512 ///
4513 /// use std::rc::UniqueRc;
4514 ///
4515 /// let b = UniqueRc::new(7);
4516 /// let new = UniqueRc::try_map(b, u32::try_from).unwrap();
4517 /// assert_eq!(*new, 7);
4518 /// ```
4519 #[cfg(not(no_global_oom_handling))]
4520 #[unstable(feature = "smart_pointer_try_map", issue = "144419")]
4521 pub fn try_map<R>(
4522 this: Self,
4523 f: impl FnOnce(T) -> R,
4524 ) -> <R::Residual as Residual<UniqueRc<R::Output, A>>>::TryType
4525 where
4526 R: Try,
4527 R::Residual: Residual<UniqueRc<R::Output, A>>,
4528 {
4529 if size_of::<T>() == size_of::<R::Output>()
4530 && align_of::<T>() == align_of::<R::Output>()
4531 && UniqueRc::weak_count(&this) == 0
4532 {
4533 // ignore-tidy-undocumented-unsafe
4534 unsafe {
4535 let (ptr, alloc) = UniqueRc::into_raw_with_allocator(this);
4536 let value = ptr.read();
4537 let allocation = UniqueRc::from_raw_with_allocator(
4538 ptr.cast::<mem::MaybeUninit<R::Output>>(),
4539 alloc,
4540 );
4541
4542 try { UniqueRc::write(allocation, f(value)?) }
4543 }
4544 } else {
4545 let (val, alloc) = UniqueRc::unwrap_with_allocator(this);
4546 try { UniqueRc::new_in(f(val)?, alloc) }
4547 }
4548 }
4549}
4550
4551impl<T: ?Sized, A: Allocator> UniqueRc<T, A> {
4552 #[cfg(not(no_global_oom_handling))]
4553 unsafe fn from_raw_with_allocator(ptr: *const T, alloc: A) -> Self {
4554 // SAFETY: Upheld by caller
4555 let offset = unsafe { data_offset(ptr) };
4556
4557 // Reverse the offset to find the original RcInner.
4558 // SAFETY: As above.
4559 let rc_ptr = unsafe { ptr.byte_sub(offset) as *mut RcInner<T> };
4560
4561 Self {
4562 // SAFETY: Upheld by caller.
4563 ptr: unsafe { NonNull::new_unchecked(rc_ptr) },
4564 _marker: PhantomData,
4565 _marker2: PhantomData,
4566 alloc,
4567 }
4568 }
4569
4570 fn into_raw_with_allocator(this: Self) -> (*const T, A) {
4571 let this = ManuallyDrop::new(this);
4572 // SAFETY: The copy of the allocator stored in `this` is forgotten
4573 (Self::as_ptr(&this), unsafe { ptr::read(&this.alloc) })
4574 }
4575
4576 /// Converts the `UniqueRc` into a regular [`Rc`].
4577 ///
4578 /// This consumes the `UniqueRc` and returns a regular [`Rc`] that contains the `value` that
4579 /// is passed to `into_rc`.
4580 ///
4581 /// Any weak references created before this method is called can now be upgraded to strong
4582 /// references.
4583 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4584 pub fn into_rc(this: Self) -> Rc<T, A> {
4585 let mut this = ManuallyDrop::new(this);
4586
4587 // Move the allocator out.
4588 // SAFETY: `this.alloc` will not be accessed again, nor dropped because it is in
4589 // a `ManuallyDrop`.
4590 let alloc: A = unsafe { ptr::read(&this.alloc) };
4591
4592 // SAFETY: This pointer was allocated at creation time so we know it is valid.
4593 unsafe {
4594 // Convert our weak reference into a strong reference
4595 this.ptr.as_mut().strong.set(1);
4596 Rc::from_inner_in(this.ptr, alloc)
4597 }
4598 }
4599
4600 #[cfg(not(no_global_oom_handling))]
4601 fn weak_count(this: &Self) -> usize {
4602 this.inner().weak() - 1
4603 }
4604
4605 #[cfg(not(no_global_oom_handling))]
4606 fn inner(&self) -> &RcInner<T> {
4607 // SAFETY: while this UniqueRc is alive we're guaranteed that the inner pointer is valid.
4608 unsafe { self.ptr.as_ref() }
4609 }
4610
4611 fn as_ptr(this: &Self) -> *const T {
4612 let ptr: *mut RcInner<T> = NonNull::as_ptr(this.ptr);
4613
4614 // SAFETY: This cannot go through Deref::deref or UniqueRc::inner because
4615 // this is required to retain raw/mut provenance such that e.g. `get_mut` can
4616 // write through the pointer after the Rc is recovered through `from_raw`.
4617 unsafe { &raw mut (*ptr).value }
4618 }
4619
4620 #[inline]
4621 fn into_inner_with_allocator(this: Self) -> (NonNull<RcInner<T>>, A) {
4622 let this = mem::ManuallyDrop::new(this);
4623 // SAFETY: Pointer is valid for reads.
4624 (this.ptr, unsafe { ptr::read(&this.alloc) })
4625 }
4626
4627 #[inline]
4628 unsafe fn from_inner_in(ptr: NonNull<RcInner<T>>, alloc: A) -> Self {
4629 Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc }
4630 }
4631}
4632
4633impl<T: ?Sized, A: AllocatorClone> UniqueRc<T, A> {
4634 /// Creates a new weak reference to the `UniqueRc`.
4635 ///
4636 /// Attempting to upgrade this weak reference will fail before the `UniqueRc` has been converted
4637 /// to a [`Rc`] using [`UniqueRc::into_rc`].
4638 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4639 pub fn downgrade(this: &Self) -> Weak<T, A> {
4640 // SAFETY: This pointer was allocated at creation time and we guarantee that we only have
4641 // one strong reference before converting to a regular Rc.
4642 unsafe {
4643 this.ptr.as_ref().inc_weak();
4644 }
4645 Weak { ptr: this.ptr, alloc: this.alloc.clone() }
4646 }
4647}
4648
4649impl<T, A: Allocator> UniqueRc<mem::MaybeUninit<T>, A> {
4650 /// Writes the value and converts to `UniqueRc<T, A>`.
4651 ///
4652 /// This method converts similarly to [`assume_init`](Self::assume_init) but
4653 /// writes `value` into it before conversion, thus guaranteeing safety.
4654 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4655 #[must_use]
4656 pub fn write(mut this: Self, value: T) -> UniqueRc<T, A> {
4657 // SAFETY: Writing initialises the wrapped value.
4658 unsafe {
4659 this.write(value);
4660 this.assume_init()
4661 }
4662 }
4663
4664 /// Converts to `UniqueRc<T, A>`.
4665 ///
4666 /// # Safety
4667 ///
4668 /// As with [`MaybeUninit::assume_init`],
4669 /// it is up to the caller to guarantee that the value
4670 /// really is in an initialized state.
4671 /// Calling this when the content is not yet fully initialized
4672 /// causes immediate undefined behavior.
4673 ///
4674 /// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
4675 #[unstable(feature = "unique_rc_arc", issue = "112566")]
4676 #[must_use]
4677 pub unsafe fn assume_init(self) -> UniqueRc<T, A> {
4678 let (ptr, alloc) = UniqueRc::into_inner_with_allocator(self);
4679 // SAFETY: Upheld by caller.
4680 unsafe { UniqueRc::from_inner_in(ptr.cast(), alloc) }
4681 }
4682}
4683
4684#[unstable(feature = "unique_rc_arc", issue = "112566")]
4685impl<T: ?Sized, A: Allocator> Deref for UniqueRc<T, A> {
4686 type Target = T;
4687
4688 fn deref(&self) -> &T {
4689 // SAFETY: This pointer was allocated at creation time so we know it is valid.
4690 unsafe { &self.ptr.as_ref().value }
4691 }
4692}
4693
4694#[unstable(feature = "unique_rc_arc", issue = "112566")]
4695impl<T: ?Sized, A: Allocator> DerefMut for UniqueRc<T, A> {
4696 fn deref_mut(&mut self) -> &mut T {
4697 // SAFETY: This pointer was allocated at creation time so we know it is valid. We know we
4698 // have unique ownership and therefore it's safe to make a mutable reference because
4699 // `UniqueRc` owns the only strong reference to itself.
4700 unsafe { &mut (*self.ptr.as_ptr()).value }
4701 }
4702}
4703
4704#[unstable(feature = "unique_rc_arc", issue = "112566")]
4705unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for UniqueRc<T, A> {
4706 fn drop(&mut self) {
4707 // ignore-tidy-undocumented-unsafe
4708 unsafe {
4709 // destroy the contained object
4710 drop_in_place(DerefMut::deref_mut(self));
4711
4712 // remove the implicit "strong weak" pointer now that we've destroyed the contents.
4713 self.ptr.as_ref().dec_weak();
4714
4715 if self.ptr.as_ref().weak() == 0 {
4716 self.alloc.deallocate(self.ptr.cast(), Layout::for_value_raw(self.ptr.as_ptr()));
4717 }
4718 }
4719 }
4720}
4721
4722/// A unique owning pointer to a [`RcInner`] **that does not imply the contents are initialized,**
4723/// but will deallocate it (without dropping the value) when dropped.
4724///
4725/// This is a helper for [`Rc::make_mut()`] to ensure correct cleanup on panic.
4726/// It is nearly a duplicate of `UniqueRc<MaybeUninit<T>, A>` except that it allows `T: !Sized`,
4727/// which `MaybeUninit` does not.
4728struct UniqueRcUninit<T: ?Sized, A: Allocator> {
4729 ptr: NonNull<RcInner<T>>,
4730 layout_for_value: Layout,
4731 alloc: Option<A>,
4732}
4733
4734impl<T: ?Sized, A: Allocator> UniqueRcUninit<T, A> {
4735 /// Allocates a RcInner with layout suitable to contain `for_value` or a clone of it.
4736 #[cfg(not(no_global_oom_handling))]
4737 fn new(for_value: &T, alloc: A) -> UniqueRcUninit<T, A> {
4738 let layout = Layout::for_value(for_value);
4739 // ignore-tidy-undocumented-unsafe
4740 let ptr = unsafe {
4741 Rc::allocate_for_layout(
4742 layout,
4743 |layout_for_rc_inner| alloc.allocate(layout_for_rc_inner),
4744 |mem| mem.with_metadata_of(ptr::from_ref(for_value) as *const RcInner<T>),
4745 )
4746 };
4747 Self { ptr: NonNull::new(ptr).unwrap(), layout_for_value: layout, alloc: Some(alloc) }
4748 }
4749
4750 /// Allocates a RcInner with layout suitable to contain `for_value` or a clone of it,
4751 /// returning an error if allocation fails.
4752 fn try_new(for_value: &T, alloc: A) -> Result<UniqueRcUninit<T, A>, AllocError> {
4753 let layout = Layout::for_value(for_value);
4754 // ignore-tidy-undocumented-unsafe
4755 let ptr = unsafe {
4756 Rc::try_allocate_for_layout(
4757 layout,
4758 |layout_for_rc_inner| alloc.allocate(layout_for_rc_inner),
4759 |mem| mem.with_metadata_of(ptr::from_ref(for_value) as *const RcInner<T>),
4760 )?
4761 };
4762 Ok(Self { ptr: NonNull::new(ptr).unwrap(), layout_for_value: layout, alloc: Some(alloc) })
4763 }
4764
4765 /// Returns the pointer to be written into to initialize the [`Rc`].
4766 fn data_ptr(&mut self) -> *mut T {
4767 let offset = data_offset_alignment(self.layout_for_value.alignment());
4768 // ignore-tidy-undocumented-unsafe
4769 unsafe { self.ptr.as_ptr().byte_add(offset) as *mut T }
4770 }
4771
4772 /// Upgrade this into a normal [`Rc`].
4773 ///
4774 /// # Safety
4775 ///
4776 /// The data must have been initialized (by writing to [`Self::data_ptr()`]).
4777 unsafe fn into_rc(self) -> Rc<T, A> {
4778 let mut this = ManuallyDrop::new(self);
4779 let ptr = this.ptr;
4780 let alloc = this.alloc.take().unwrap();
4781
4782 // SAFETY: The pointer is valid as per `UniqueRcUninit::new`, and the caller is responsible
4783 // for having initialized the data.
4784 unsafe { Rc::from_ptr_in(ptr.as_ptr(), alloc) }
4785 }
4786}
4787
4788impl<T: ?Sized, A: Allocator> Drop for UniqueRcUninit<T, A> {
4789 fn drop(&mut self) {
4790 // SAFETY:
4791 // * new() produced a pointer safe to deallocate.
4792 // * We own the pointer unless into_rc() was called, which forgets us.
4793 unsafe {
4794 self.alloc.take().unwrap().deallocate(
4795 self.ptr.cast(),
4796 rc_inner_layout_for_value_layout(self.layout_for_value),
4797 );
4798 }
4799 }
4800}
4801
4802#[stable(feature = "allocator_api", since = "1.100.0")]
4803unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Rc<T, A> {
4804 #[inline]
4805 fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
4806 (**self).allocate(layout)
4807 }
4808
4809 #[inline]
4810 fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
4811 (**self).allocate_zeroed(layout)
4812 }
4813
4814 #[inline]
4815 unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
4816 // SAFETY: the safety contract must be upheld by the caller
4817 unsafe { (**self).deallocate(ptr, layout) }
4818 }
4819
4820 #[inline]
4821 unsafe fn grow(
4822 &self,
4823 ptr: NonNull<u8>,
4824 old_layout: Layout,
4825 new_layout: Layout,
4826 ) -> Result<NonNull<[u8]>, AllocError> {
4827 // SAFETY: the safety contract must be upheld by the caller
4828 unsafe { (**self).grow(ptr, old_layout, new_layout) }
4829 }
4830
4831 #[inline]
4832 unsafe fn grow_zeroed(
4833 &self,
4834 ptr: NonNull<u8>,
4835 old_layout: Layout,
4836 new_layout: Layout,
4837 ) -> Result<NonNull<[u8]>, AllocError> {
4838 // SAFETY: the safety contract must be upheld by the caller
4839 unsafe { (**self).grow_zeroed(ptr, old_layout, new_layout) }
4840 }
4841
4842 #[inline]
4843 unsafe fn shrink(
4844 &self,
4845 ptr: NonNull<u8>,
4846 old_layout: Layout,
4847 new_layout: Layout,
4848 ) -> Result<NonNull<[u8]>, AllocError> {
4849 // SAFETY: the safety contract must be upheld by the caller
4850 unsafe { (**self).shrink(ptr, old_layout, new_layout) }
4851 }
4852}
4853#[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
4854#[unstable_feature_bound(allocator_ext)]
4855impl<T: ?Sized + Allocator, A: Allocator> AllocatorNightly for Rc<T, A> {}
4856
4857#[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
4858unsafe impl<T: Allocator + ?Sized, A: AllocatorClone> AllocatorClone for Rc<T, A> {}