Struct mjb_gc::Gc [−][src]
pub struct Gc<T: Trace> { /* fields omitted */ }
Expand description
A pointer type over a value that provides shared ownership and immediate
cycle collection upon Drop
.
Implementations
Creates a new Gc<T>
with the given value.
Examples
use mjb_gc::Gc; // Why would we ever want to GC an integer? let five = Gc::new(5);
Construct a new Gc<T>
value using a reference to itself.
Attempting to dereference the reference before this function returns
will panic.
Examples
use mjb_gc::{Gc, Trace}; #[derive(Trace)] struct Gadget { self_gc: Gc<Gadget>, // ... } impl Gadget { fn new() -> Gc<Self> { Gc::new_cyclic(|self_gc| Gadget { self_gc, // ... }) } }
use mjb_gc::Gc; let _ = Gc::new_cyclic(|a| { let _ = &*a; // Dereference the Gc, and thus panic. });
Unwraps the Gc<T>
if this is the only reference.
Otherwise, an Err
is returned with the same Gc
that was passed in.
Examples
use mjb_gc::Gc; let x = Gc::new(3); assert_eq!(Gc::try_unwrap(x), Ok(3)); let x = Gc::new(4); let _y = Gc::clone(&x); assert_eq!(*Gc::try_unwrap(x).unwrap_err(), 4);
Gets the amount of references to this Gc<T>
.
Note that this includes both external and internal references.
Whether this reference is the only reference to the value.
If the value is self-referential, those references will cause this never to be true.
Get a mutable reference into the Gc
if there are no other
references to the allocation.
Returns None
otherwise, since mutability xor aliasing.
See also make_mut
, which
clone
s the inner value instead of returning
None
.
Panics
If the value has already been dropped, this function will panic.
Examples
use mjb_gc::Gc; let mut x = Gc::new(3); *Gc::get_mut(&mut x).unwrap() = 4; assert_eq!(*x, 4); let _y = Gc::clone(&x); assert!(Gc::get_mut(&mut x).is_none());
Get the value as mutable, bypassing any safety checks.
Safety
There needs to be no other references in use while this one is in use.
Trait Implementations
Creates a new Gc
pointer to the same allocation.
This doesn’t clone the inner data.
This call will panic if the reference count would overflow.
Performs copy-assignment from source
. Read more
Dereference a Gc<T>
into T
.
If the value has already been dropped, this function will panic.
Examples
use mjb_gc::{Gc, Trace}; #[derive(Trace)] struct Cyclic(Gc<Cyclic>); impl Drop for Cyclic { fn drop(&mut self) { let _a = &*self.0; // Dereference the Gc and panic. } } let a = Gc::new_cyclic(|v| Cyclic(v));
type Target = T
type Target = T
The resulting type after dereferencing.
Drops the Gc
.
This will decrement the reference count. If there are no external references, this will drop the contained value.
Examples
use mjb_gc::{Gc, Trace}; #[derive(Trace)] struct Cyclic(Gc<Cyclic>); impl Drop for Cyclic { fn drop(&mut self) { println!("dropped!"); } } let foo = Gc::new_cyclic(|g| Cyclic(g)); let foo2 = Gc::clone(&foo); drop(foo); // Doesn't print anything drop(foo2); // Prints "dropped!"
Sets the done flags to false.
Checks whether the reference counts match.
Whether the type could contain cycles.
Auto Trait Implementations
Blanket Implementations
Mutably borrows from an owned value. Read more