Lines Matching defs:Weak

3302 use std::sync::{Arc, Weak};
3307 let mut a = Weak::<Void>::new();
3311 let mut a: Weak<dyn Any> = a; // Unsizing
3339 let mut b = Weak::<u32>::new();
3342 let mut b: Weak<dyn Any> = b; // Unsizing
9059 use std::rc::{Rc, Weak};
9064 let mut a = Weak::<Void>::new();
9068 let mut a: Weak<dyn Any> = a; // Unsizing
9096 let mut b = Weak::<u32>::new();
9099 let mut b: Weak<dyn Any> = b; // Unsizing
15618 assert_eq!(Weak::weak_count(&Weak::<u64>::new()), 0);
15619 assert_eq!(Weak::strong_count(&Weak::<u64>::new()), 0);
15623 assert_eq!(Weak::strong_count(&w), 1);
15624 assert_eq!(Weak::weak_count(&w), 1);
15626 assert_eq!(Weak::strong_count(&w), 1);
15627 assert_eq!(Weak::weak_count(&w), 2);
15628 assert_eq!(Weak::strong_count(&w2), 1);
15629 assert_eq!(Weak::weak_count(&w2), 2);
15631 assert_eq!(Weak::strong_count(&w2), 1);
15632 assert_eq!(Weak::weak_count(&w2), 1);
15634 assert_eq!(Weak::strong_count(&w2), 2);
15635 assert_eq!(Weak::weak_count(&w2), 1);
15638 assert_eq!(Weak::strong_count(&w2), 0);
15639 assert_eq!(Weak::weak_count(&w2), 0);
15699 let y_ptr = Weak::into_raw(y);
15703 let y = Weak::from_raw(y_ptr);
15704 let y_up = Weak::upgrade(&y).unwrap();
15718 let weak: Weak<str> = Arc::downgrade(&arc);
15720 let ptr = Weak::into_raw(weak.clone());
15721 let weak2 = unsafe { Weak::from_raw(ptr) };
15727 let weak: Weak<dyn Display> = Arc::downgrade(&arc);
15729 let ptr = Weak::into_raw(weak.clone());
15730 let weak2 = unsafe { Weak::from_raw(ptr) };
15814 x: Mutex<Option<Weak<Cycle>>>,
15914 let y: Weak<CStr> = Arc::downgrade(&x);
15933 let foo: Weak<usize> = Weak::new();
16109 inner: Weak<ZeroRefs>,
16114 ZeroRefs { inner: Weak::new() }
16126 inner: Weak<OneRef>,
16137 let one_ref2 = Weak::upgrade(&one_ref.inner).unwrap();
16147 inner1: Weak<TwoRefs>,
16148 inner2: Weak<TwoRefs>,
16163 let two_refs1 = Weak::upgrade(&two_refs.inner1).unwrap();
16166 let two_refs2 = Weak::upgrade(&two_refs.inner2).unwrap();
19519 //! [`Weak`] pointer. A [`Weak`] pointer can be [`upgrade`][upgrade]d
19521 //! already been dropped. In other words, `Weak` pointers do not keep the value
19526 //! [`Weak`] is used to break cycles. For example, a tree could have strong
19527 //! [`Rc`] pointers from parent nodes to children, and [`Weak`] pointers from
19556 //! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the inner value may have
19562 //! is done using the `Clone` trait implemented for [`Rc<T>`][`Rc`] and [`Weak<T>`][`Weak`].
19642 //! a memory leak. In order to get around this, we can use [`Weak`]
19656 //! use std::rc::Weak;
19661 //! gadgets: RefCell<Vec<Weak<Gadget>>>,
19708 //! // `gadget_weak` is a `Weak<Gadget>`. Since `Weak` pointers can't
19734 //! [upgrade]: Weak::upgrade
19857 /// use std::rc::{Rc, Weak};
19860 /// self_weak: Weak<Self>,
19872 pub fn new_cyclic(data_fn: impl FnOnce(&Weak<T>) -> T) -> Rc<T> {
19884 let weak = Weak { ptr: init_ptr };
20099 // fake Weak.
20101 let _weak = Weak { ptr: this.ptr };
20353 /// Creates a new [`Weak`] pointer to this allocation.
20365 pub fn downgrade(this: &Self) -> Weak<T> {
20367 // Make sure we do not create a dangling Weak
20369 Weak { ptr: this.ptr }
20372 /// Gets the number of [`Weak`] pointers to this allocation.
20475 /// Returns `true` if there are no other `Rc` or [`Weak`] pointers to
20483 /// no other `Rc` or [`Weak`] pointers to the same allocation.
20521 /// Any other `Rc` or [`Weak`] pointers to the same allocation must not be dereferenced
20543 // this would conflict with accesses to the reference counts (e.g. by `Weak`).
20578 /// If there are no other `Rc` pointers to this allocation, then [`Weak`]
20604 /// [`Weak`] pointers will be disassociated:
20641 // Weak here -- we know other Weaks can clean up for us)
20887 /// [`Weak`], so we `drop` the inner value.
21422 /// `Weak` is a version of [`Rc`] that holds a non-owning reference to the
21423 /// managed allocation. The allocation is accessed by calling [`upgrade`] on the `Weak`
21426 /// Since a `Weak` reference does not count towards ownership, it will not
21427 /// prevent the value stored in the allocation from being dropped, and `Weak` itself makes no
21429 /// when [`upgrade`]d. Note however that a `Weak` reference *does* prevent the allocation
21432 /// A `Weak` pointer is useful for keeping a temporary reference to the allocation
21436 /// have strong [`Rc`] pointers from parent nodes to children, and `Weak`
21439 /// The typical way to obtain a `Weak` pointer is to call [`Rc::downgrade`].
21441 /// [`upgrade`]: Weak::upgrade
21443 pub struct Weak<T: ?Sized> {
21446 // `Weak::new` sets this to `usize::MAX` so that it doesn’t need
21454 impl<T: ?Sized> !marker::Send for Weak<T> {}
21456 impl<T: ?Sized> !marker::Sync for Weak<T> {}
21459 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
21462 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Weak<U>> for Weak<T> {}
21464 impl<T> Weak<T> {
21465 /// Constructs a new `Weak<T>`, without allocating any memory.
21468 /// [`upgrade`]: Weak::upgrade
21473 /// use std::rc::Weak;
21475 /// let empty: Weak<i64> = Weak::new();
21479 pub fn new() -> Weak<T> {
21480 Weak { ptr: NonNull::new(usize::MAX as *mut RcBox<T>).expect("MAX is not 0") }
21496 impl<T: ?Sized> Weak<T> {
21497 /// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
21538 /// Consumes the `Weak<T>` and turns it into a raw pointer.
21542 /// back into the `Weak<T>` with [`from_raw`].
21550 /// use std::rc::{Rc, Weak};
21559 /// drop(unsafe { Weak::from_raw(raw) });
21563 /// [`from_raw`]: Weak::from_raw
21564 /// [`as_ptr`]: Weak::as_ptr
21572 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
21575 /// later) or to deallocate the weak count by dropping the `Weak<T>`.
21593 /// use std::rc::{Rc, Weak};
21602 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
21608 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
21611 /// [`into_raw`]: Weak::into_raw
21612 /// [`upgrade`]: Weak::upgrade
21613 /// [`new`]: Weak::new
21616 // See Weak::as_ptr for context on how the input pointer is derived.
21619 // This is a dangling Weak.
21622 // Otherwise, we're guaranteed the pointer came from a nondangling Weak.
21626 // SAFETY: the pointer originated from a Weak, so this offset is safe.
21630 // SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
21631 Weak { ptr: unsafe { NonNull::new_unchecked(ptr) } }
21634 /// Attempts to upgrade the `Weak` pointer to an [`Rc`], delaying
21670 /// If `self` was created using [`Weak::new`], this will return 0.
21676 /// Gets the number of `Weak` pointers pointing to this allocation.
21693 /// (i.e., when this `Weak` was created by `Weak::new`).
21709 /// Returns `true` if the two `Weak`s point to the same allocation (similar to
21711 /// (because they were created with `Weak::new()`).
21715 /// Since this compares pointers it means that `Weak::new()` will equal each
21735 /// Comparing `Weak::new`.
21738 /// use std::rc::{Rc, Weak};
21740 /// let first = Weak::new();
21741 /// let second = Weak::new();
21758 impl<T: ?Sized> Drop for Weak<T> {
21759 /// Drops the `Weak` pointer.
21764 /// use std::rc::{Rc, Weak};
21776 /// let other_weak_foo = Weak::clone(&weak_foo);
21798 impl<T: ?Sized> Clone for Weak<T> {
21799 /// Makes a clone of the `Weak` pointer that points to the same allocation.
21804 /// use std::rc::{Rc, Weak};
21808 /// let _ = Weak::clone(&weak_five);
21811 fn clone(&self) -> Weak<T> {
21815 Weak { ptr: self.ptr }
21820 impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
21822 write!(f, "(Weak)")
21827 impl<T> Default for Weak<T> {
21828 /// Constructs a new `Weak<T>`, without allocating any memory.
21832 /// [`upgrade`]: Weak::upgrade
21837 /// use std::rc::Weak;
21839 /// let empty: Weak<i64> = Default::default();
21842 fn default() -> Weak<T> {
21843 Weak::new()
23321 // reports in Arc / Weak implementation use atomic loads for synchronization
23371 /// ## Breaking cycles with `Weak`
23374 /// [`Weak`] pointer. A [`Weak`] pointer can be [`upgrade`][upgrade]d
23376 /// already been dropped. In other words, `Weak` pointers do not keep the value
23381 /// [`Weak`] is used to break cycles. For example, a tree could have
23382 /// strong `Arc` pointers from parent nodes to children, and [`Weak`]
23388 /// `Clone` trait implemented for [`Arc<T>`][Arc] and [`Weak<T>`][Weak].
23427 /// [`Weak<T>`][Weak] does not auto-dereference to `T`, because the inner value may have
23439 /// [upgrade]: Weak::upgrade
23521 /// `Weak` is a version of [`Arc`] that holds a non-owning reference to the
23522 /// managed allocation. The allocation is accessed by calling [`upgrade`] on the `Weak`
23525 /// Since a `Weak` reference does not count towards ownership, it will not
23526 /// prevent the value stored in the allocation from being dropped, and `Weak` itself makes no
23528 /// when [`upgrade`]d. Note however that a `Weak` reference *does* prevent the allocation
23531 /// A `Weak` pointer is useful for keeping a temporary reference to the allocation
23535 /// have strong [`Arc`] pointers from parent nodes to children, and `Weak`
23538 /// The typical way to obtain a `Weak` pointer is to call [`Arc::downgrade`].
23540 /// [`upgrade`]: Weak::upgrade
23542 pub struct Weak<T: ?Sized> {
23545 // `Weak::new` sets this to `usize::MAX` so that it doesn’t need
23553 unsafe impl<T: ?Sized + Sync + Send> Send for Weak<T> {}
23555 unsafe impl<T: ?Sized + Sync + Send> Sync for Weak<T> {}
23558 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
23560 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Weak<U>> for Weak<T> {}
23563 impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
23565 write!(f, "(Weak)")
23620 /// use std::sync::{Arc, Weak};
23623 /// me: Weak<Foo>,
23632 pub fn new_cyclic(data_fn: impl FnOnce(&Weak<T>) -> T) -> Arc<T> {
23643 let weak = Weak { ptr: init_ptr };
23661 // in order to synchronize with the `compare_exchange_weak` in `Weak::upgrade`.
23665 // non-upgradeable `Weak`:
23666 // - It can *clone* the `Weak`, increasing the weak reference count.
23876 let _weak = Weak { ptr: this.ptr };
24125 /// Creates a new [`Weak`] pointer to this allocation.
24137 pub fn downgrade(this: &Self) -> Weak<T> {
24159 // Make sure we do not create a dangling Weak
24161 return Weak { ptr: this.ptr };
24168 /// Gets the number of [`Weak`] pointers to this allocation.
24185 /// // the `Arc` or `Weak` between threads.
24312 drop(Weak { ptr: self.ptr });
24584 /// If there are other `Arc` or [`Weak`] pointers to the same allocation,
24589 /// any remaining `Weak` pointers.
24649 let _weak = Weak { ptr: this.ptr };
24672 /// no other `Arc` or [`Weak`] pointers to the same allocation.
24719 /// Any other `Arc` or [`Weak`] pointers to the same allocation must not be dereferenced
24741 // this would alias with concurrent access to the reference counts (e.g. by `Weak`).
24754 // writes to `strong` (in particular in `Weak::upgrade`) prior to decrements
24755 // of the `weak` count (via `Weak::drop`, which uses release). If the upgraded
24780 /// [`Weak`], so we `drop` the inner value.
24881 impl<T> Weak<T> {
24882 /// Constructs a new `Weak<T>`, without allocating any memory.
24885 /// [`upgrade`]: Weak::upgrade
24890 /// use std::sync::Weak;
24892 /// let empty: Weak<i64> = Weak::new();
24896 pub fn new() -> Weak<T> {
24897 Weak { ptr: NonNull::new(usize::MAX as *mut ArcInner<T>).expect("MAX is not 0") }
24908 impl<T: ?Sized> Weak<T> {
24909 /// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
24950 /// Consumes the `Weak<T>` and turns it into a raw pointer.
24954 /// back into the `Weak<T>` with [`from_raw`].
24962 /// use std::sync::{Arc, Weak};
24971 /// drop(unsafe { Weak::from_raw(raw) });
24975 /// [`from_raw`]: Weak::from_raw
24976 /// [`as_ptr`]: Weak::as_ptr
24984 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
24987 /// later) or to deallocate the weak count by dropping the `Weak<T>`.
25004 /// use std::sync::{Arc, Weak};
25013 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
25019 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
25022 /// [`new`]: Weak::new
25023 /// [`into_raw`]: Weak::into_raw
25024 /// [`upgrade`]: Weak::upgrade
25028 // See Weak::as_ptr for context on how the input pointer is derived.
25031 // This is a dangling Weak.
25034 // Otherwise, we're guaranteed the pointer came from a nondangling Weak.
25038 // SAFETY: the pointer originated from a Weak, so this offset is safe.
25042 // SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
25043 Weak { ptr: unsafe { NonNull::new_unchecked(ptr) } }
25047 impl<T: ?Sized> Weak<T> {
25048 /// Attempts to upgrade the `Weak` pointer to an [`Arc`], delaying
25096 // value can be initialized after `Weak` references have already been created. In that case, we
25107 /// If `self` was created using [`Weak::new`], this will return 0.
25113 /// Gets an approximation of the number of `Weak` pointers pointing to this
25116 /// If `self` was created using [`Weak::new`], or if there are no remaining
25123 /// `Weak`s pointing to the same allocation.
25145 /// (i.e., when this `Weak` was created by `Weak::new`).
25161 /// Returns `true` if the two `Weak`s point to the same allocation (similar to
25163 /// (because they were created with `Weak::new()`).
25167 /// Since this compares pointers it means that `Weak::new()` will equal each
25187 /// Comparing `Weak::new`.
25190 /// use std::sync::{Arc, Weak};
25192 /// let first = Weak::new();
25193 /// let second = Weak::new();
25210 impl<T: ?Sized> Clone for Weak<T> {
25211 /// Makes a clone of the `Weak` pointer that points to the same allocation.
25216 /// use std::sync::{Arc, Weak};
25220 /// let _ = Weak::clone(&weak_five);
25223 fn clone(&self) -> Weak<T> {
25227 return Weak { ptr: self.ptr };
25240 Weak { ptr: self.ptr }
25245 impl<T> Default for Weak<T> {
25246 /// Constructs a new `Weak<T>`, without allocating memory.
25250 /// [`upgrade`]: Weak::upgrade
25255 /// use std::sync::Weak;
25257 /// let empty: Weak<i64> = Default::default();
25260 fn default() -> Weak<T> {
25261 Weak::new()
25266 impl<T: ?Sized> Drop for Weak<T> {
25267 /// Drops the `Weak` pointer.
25272 /// use std::sync::{Arc, Weak};
25284 /// let other_weak_foo = Weak::clone(&weak_foo);
26440 x: RefCell<Option<Weak<Cycle>>>,
26500 assert_eq!(Weak::weak_count(&Weak::<u64>::new()), 0);
26501 assert_eq!(Weak::strong_count(&Weak::<u64>::new()), 0);
26505 assert_eq!(Weak::strong_count(&w), 1);
26506 assert_eq!(Weak::weak_count(&w), 1);
26508 assert_eq!(Weak::strong_count(&w), 1);
26509 assert_eq!(Weak::weak_count(&w), 2);
26510 assert_eq!(Weak::strong_count(&w2), 1);
26511 assert_eq!(Weak::weak_count(&w2), 2);
26513 assert_eq!(Weak::strong_count(&w2), 1);
26514 assert_eq!(Weak::weak_count(&w2), 1);
26516 assert_eq!(Weak::strong_count(&w2), 2);
26517 assert_eq!(Weak::weak_count(&w2), 1);
26520 assert_eq!(Weak::strong_count(&w2), 0);
26521 assert_eq!(Weak::weak_count(&w2), 0);
26581 let y_ptr = Weak::into_raw(y);
26585 let y = Weak::from_raw(y_ptr);
26586 let y_up = Weak::upgrade(&y).unwrap();
26600 let weak: Weak<str> = Rc::downgrade(&arc);
26602 let ptr = Weak::into_raw(weak.clone());
26603 let weak2 = unsafe { Weak::from_raw(ptr) };
26609 let weak: Weak<dyn Display> = Rc::downgrade(&arc);
26611 let ptr = Weak::into_raw(weak.clone());
26612 let weak2 = unsafe { Weak::from_raw(ptr) };
26711 let y: Weak<CStr> = Rc::downgrade(&x);
26730 let foo: Weak<usize> = Weak::new();
26883 inner: Weak<ZeroRefs>,
26889 ZeroRefs { inner: Weak::new() }
26901 inner: Weak<OneRef>,
26913 let one_ref2 = Weak::upgrade(&one_ref.inner).unwrap();
26923 inner: Weak<TwoRefs>,
26924 inner1: Weak<TwoRefs>,
26936 let two_ref3 = Weak::upgrade(&two_refs.inner).unwrap();
26939 let two_ref2 = Weak::upgrade(&two_refs.inner1).unwrap();