Struct dumpster::unsync::Gc

source ·
pub struct Gc<T: Collectable + ?Sized + 'static> { /* private fields */ }
Expand description

A garbage-collected pointer.

This garbage-collected pointer may be used for data which is not safe to share across threads (such as a std::cell::RefCell). It can also be used for variably sized data.

Examples

use dumpster::unsync::Gc;

let x: Gc<u8> = Gc::new(3);

println!("{}", *x); // prints '3'
                    // x is then freed automatically!

Implementations§

source§

impl<T: Collectable + ?Sized> Gc<T>

source

pub fn new(value: T) -> Gc<T>where T: Sized,

Construct a new garbage-collected allocation, with value as its value.

Trait Implementations§

source§

impl<T: Collectable + ?Sized> AsRef<T> for Gc<T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: Collectable + ?Sized> Borrow<T> for Gc<T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T: Collectable + ?Sized> Clone for Gc<T>

source§

fn clone(&self) -> Self

Create a duplicate reference to the same data pointed to by self. This does not duplicate the data.

1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Collectable + ?Sized> Collectable for Gc<T>

source§

fn accept<V: Visitor>(&self, visitor: &mut V) -> Result<(), ()>

Accept a visitor to this garbage-collected value. Read more
source§

impl<T: Debug + Collectable + ?Sized + 'static> Debug for Gc<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Collectable + Default> Default for Gc<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T: Collectable + ?Sized> Deref for Gc<T>

source§

fn deref(&self) -> &Self::Target

Dereference this pointer, creating a reference to the contained value T.

Panics

This function may panic if it is called from within the implementation of std::ops::Drop of its owning value, since returning such a reference could cause a use-after-free. It is not guaranteed to panic.

Examples

The following is a correct time to dereference a Gc.

use dumpster::unsync::Gc;

let my_gc = Gc::new(0u8);
let my_ref: &u8 = &my_gc;

Dereferencing a Gc while dropping is not correct.

// This is wrong!
use std::cell::RefCell;
use dumpster::{unsync::Gc, Collectable};

#[derive(Collectable)]
struct Bad {
    s: String,
    cycle: RefCell<Option<Gc<Bad>>>,
}

impl Drop for Bad {
    fn drop(&mut self) {
        // The second time this `print` is executed it will try to
        // print a `String` that has already been dropped.
        println!("{}", self.cycle.borrow().as_ref().unwrap().s)
    }
}

let foo = Gc::new(Bad {
    s: "foo".to_string(),
    cycle: RefCell::new(None),
});
§

type Target = T

The resulting type after dereferencing.
source§

impl<T: Collectable + ?Sized> Drop for Gc<T>

source§

fn drop(&mut self)

Destroy this garbage-collected pointer.

If this is the last reference which can reach the pointed-to data, the allocation that it points to will be destroyed.

source§

impl<T: Collectable + ?Sized> Pointer for Gc<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Gc<T>

§

impl<T> !Send for Gc<T>

§

impl<T> !Sync for Gc<T>

§

impl<T: ?Sized> Unpin for Gc<T>

§

impl<T> !UnwindSafe for Gc<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.