Skip to main content

Heap

Struct Heap 

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

Owns the live-object registry for one JavaScript runtime.

Implementations§

Source§

impl Heap

Source

pub fn new() -> Heap

Create an empty heap. Runs the collector only when asked (see Heap::set_threshold).

Examples found in repository?
examples/cycle.rs (line 20)
19fn main() {
20    let heap = Heap::new();
21    let a = Gc::new(&heap, Node::default());
22    let b = Gc::new(&heap, Node::default());
23    *a.next.borrow_mut() = Some(b.clone());
24    *b.next.borrow_mut() = Some(a.clone()); // a cycle
25    drop((a, b));
26
27    let stats = heap.collect();
28    println!("collected={} live={}", stats.collected, stats.after);
29    assert_eq!(stats.collected, 2, "the cycle should be collected");
30}
Source

pub fn set_threshold(&self, n: usize)

Trigger a collection automatically when more than n boxes are live. usize::MAX disables automatic collection (the default).

Source

pub fn threshold(&self) -> usize

Current automatic-collection threshold.

Source

pub fn live_count(&self) -> usize

Number of live boxes.

Source

pub fn allocated_bytes(&self) -> usize

Approximate number of bytes held by live boxes (excluding external allocations made by the values themselves).

Source

pub fn collect(&self) -> CollectStats

Run the cycle collector.

Objects that are only reachable from garbage are dropped and freed; everything reachable from a handle living outside the heap survives.

Examples found in repository?
examples/cycle.rs (line 27)
19fn main() {
20    let heap = Heap::new();
21    let a = Gc::new(&heap, Node::default());
22    let b = Gc::new(&heap, Node::default());
23    *a.next.borrow_mut() = Some(b.clone());
24    *b.next.borrow_mut() = Some(a.clone()); // a cycle
25    drop((a, b));
26
27    let stats = heap.collect();
28    println!("collected={} live={}", stats.collected, stats.after);
29    assert_eq!(stats.collected, 2, "the cycle should be collected");
30}

Trait Implementations§

Source§

impl Default for Heap

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl !Freeze for Heap

§

impl !RefUnwindSafe for Heap

§

impl !Send for Heap

§

impl !Sync for Heap

§

impl !UnwindSafe for Heap

§

impl Forget for Heap

§

impl Move for Heap

§

impl Unpin for Heap

§

impl UnsafeUnpin for Heap

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

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

fn try_from(value: U) -> Result<T, !>

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.