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