Struct jrsonnet_gcmodule::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 jrsonnet_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.

source

pub fn leak(&self)

Leak all objects allocated in this space

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>,

§

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>,

§

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.