Struct ObjectSpace

Source
pub struct ObjectSpace { /* private fields */ }
Expand description

Provides advanced explicit control about where to store Cc objects.

An ObjectSpace provides an alternative place for a collection of Cc objects. Those objects are isolated from the default thread-local space used by objects crated via Cc::new. The objects in this space can be collected by ObjectSpace::collect_cycles(), but not collect_thread_cycles.

Use ObjectSpace::create to create new objects within the space.

Objects within a space should not refer to objects in a different space. Failing to do so might cause memory leak.

§Example

use gcmodule::{Cc, ObjectSpace, Trace};
use std::cell::RefCell;

let mut space = ObjectSpace::default();
assert_eq!(space.count_tracked(), 0);

{
    type List = Cc<RefCell<Vec<Box<dyn Trace>>>>;
    let a: List = space.create(Default::default());
    let b: List = space.create(Default::default());
    a.borrow_mut().push(Box::new(b.clone()));
    b.borrow_mut().push(Box::new(a.clone()));
}

assert_eq!(space.count_tracked(), 2);
assert_eq!(space.collect_cycles(), 2);

Implementations§

Source§

impl ObjectSpace

Source

pub fn count_tracked(&self) -> usize

Count objects tracked by this ObjectSpace.

Source

pub fn collect_cycles(&self) -> usize

Collect cyclic garbage tracked by this ObjectSpace. Return the number of objects collected.

Source

pub fn create<T: Trace>(&self, value: T) -> Cc<T>

Constructs a new Cc<T> in this ObjectSpace.

The returned object should only refer to objects in the same space. Otherwise the collector might fail to collect cycles.

Trait Implementations§

Source§

impl Default for ObjectSpace

Source§

fn default() -> Self

Constructs an empty ObjectSpace.

Source§

impl Drop for ObjectSpace

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.