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§
Trait Implementations§
source§impl<T: Collectable + ?Sized> Collectable for Gc<T>
impl<T: Collectable + ?Sized> Collectable for Gc<T>
source§impl<T: Collectable + ?Sized> Deref for Gc<T>
impl<T: Collectable + ?Sized> Deref for Gc<T>
source§fn deref(&self) -> &Self::Target
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),
});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> 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
Mutably borrows from an owned value. Read more