pub struct Gc<T: Trace + 'static> { /* private fields */ }
Expand description
A cycle-collected reference-counted smart pointer.
Gc<T>
provides shared ownership of a value of type T
, allocated on the heap. Cloning it
will produce a new Gc
instance which points to the same allocation as the original Gc
.
Gc
provides RefCell
like behavior for its data so there is no need to wrap inner data in a
Cell
or RefCell
type in order to achieve interior mutability. You may use Gc::borrow
and
Gc::borrow_mut
Unlike std::rc::Rc
, Gc
pointers may refer to each other in a way that creates cycles
arbitrarily without causing leaks, provided that the program calls collect
to collect those
cycles.
It’s important to note a few things about collection:
- The default
collect
implementation only performs cycle-tracing collection if a node has been in waiting for collection for a while. This is so that we don’t pay the cost of tracing for acyclic nodes which may be marked for collection due to the number of outstanding references, but don’t actually participate in a cycle. You may usecollect_full
to force tracing collection of all pending nodes if you prefer. - Dropping of data is deferred until a call to
collect
orcollect_full
is made, and the collector is able to determine that theGc
is actually dead. The collector guarantees that (in the absence of bugs) theDrop
implementation for your type will be executed if it is determined to be dead, but cannot provide any guarantees on when it will be executed.collect
has weak guarantees even in the presence of acyclic pointers - if a node containing your type is acyclic, but has N strong references, it may take up tomin(N,
CollectOptions::old_gen_threshold
+ 1)
calls tocollect
for the value to be cleaned up even if all parents are dropped.- The collector does guarantee that if
collect_full
is used, theDrop
implementation for your data will have been executed by the timecollect_full
completes if the value is dead.
- The collector does not make any guarantees about the relative order of drops for dead nodes. Even if the order appears stable, you may not rely on it for program correctness.
Implementations§
Source§impl<T> Gc<T>where
T: Trace + 'static,
impl<T> Gc<T>where
T: Trace + 'static,
Sourcepub fn strong_count(this: &Self) -> usize
pub fn strong_count(this: &Self) -> usize
Retrieve the current number of strong references outstanding.
Sourcepub fn is_live(this: &Self) -> bool
pub fn is_live(this: &Self) -> bool
Returns true if the data in Self hasn’t been dropped yet. This will almost always be the
case unless it is called inside of a Drop implementation or if there is a bug present in a
Trace
impl.
Sourcepub fn borrow(&self) -> Ref<'_, T>
pub fn borrow(&self) -> Ref<'_, T>
Get a reference into this Gc
.
The borrow of the data ends until the returned Ref
exists the scope. Multiple immutable
borrows may be taken out at the same time.
§Panics
Panics if the value is currently mutably borrowed.
Sourcepub fn try_borrow(&self) -> Option<Ref<'_, T>>
pub fn try_borrow(&self) -> Option<Ref<'_, T>>
Try to get a reference into this Gc
.
Returns None if the pointer is dead or immutably borrowed, as the data contained in this pointer is possibly cleaned up or cannot be aliased.
Sourcepub fn borrow_mut(&self) -> RefMut<'_, T>
pub fn borrow_mut(&self) -> RefMut<'_, T>
Get a mutable reference into this Gc
.
Similar to RefCell
, the mutable borrow of the data lasts until the returned RefMut or all
RefMuts derived from it exit scope.
§Panics
Panics if the value is currently borrowed.
Sourcepub fn try_borrow_mut(&self) -> Option<RefMut<'_, T>>
pub fn try_borrow_mut(&self) -> Option<RefMut<'_, T>>
Try to get a mutable referenced into this Gc
.
Will return None if there are oustanding borrows, or if the pointer is dead.
Trait Implementations§
Source§impl<T> Ord for Gc<T>
impl<T> Ord for Gc<T>
Source§impl<T> PartialOrd for Gc<T>where
T: PartialOrd + Trace + 'static,
impl<T> PartialOrd for Gc<T>where
T: PartialOrd + Trace + 'static,
Source§impl<T> Trace for Gc<T>where
T: Trace + 'static,
impl<T> Trace for Gc<T>where
T: Trace + 'static,
Source§fn visit_children(&self, visitor: &mut GcVisitor<'_>)
fn visit_children(&self, visitor: &mut GcVisitor<'_>)
visit_children(visitor)
on each value owned by the
implementor which may participate in a reference cycle. The default implementation for
Gc
will appropriately notify the collector when it is visited. Read moreimpl<T> Eq for Gc<T>
Auto Trait Implementations§
impl<T> Freeze for Gc<T>
impl<T> !RefUnwindSafe for Gc<T>
impl<T> !Send for Gc<T>
impl<T> !Sync for Gc<T>
impl<T> Unpin for Gc<T>
impl<T> !UnwindSafe for Gc<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.