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.
});

Creates a pinned Gc<T> with the given value.

Whether two Gcs point to the same allocation.

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 clones 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.

Make a mutable reference into the given Gc.

If there are more references to the value, it will be cloned.

Panics

If the value has already been dropped, this function will panic.

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

Formats the value using the given formatter. 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));

The resulting type after dereferencing.

Formats the value using the given formatter. Read more

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!"

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Resets the Gc for another trace cycle.

Runs a trace cycle on the Gc.

Sets the done flags to false.

Checks whether the reference counts match.

Whether the type could contain cycles.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.