Heap

Struct Heap 

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

A heap for storing objects.

Heap is the centre of broom’s universe. It’s the singleton through with manipulation of objects occurs. It can be used to create, access, mutate and garbage-collect objects.

Note that heaps, and the objects associated with them, are not compatible: this means that you may not create trace routes (see Trace) that cross the boundary between different heaps.

Implementations§

Source§

impl<T: Trace<T>> Heap<T>

Source

pub fn new() -> Self

Create an empty heap.

Source

pub fn insert_temp(&mut self, object: T) -> Handle<T>

Adds a new object to this heap that will be cleared upon the next garbage collection, if not attached to the object tree.

Source

pub fn insert(&mut self, object: T) -> Rooted<T>

Adds a new object to this heap that will not be cleared by garbage collection until all rooted handles have been dropped.

Source

pub fn make_rooted(&mut self, handle: impl AsRef<Handle<T>>) -> Rooted<T>

Upgrade a handle (that will be cleared by the garbage collector) into a rooted handle (that will not).

Source

pub fn len(&self) -> usize

Count the number of heap-allocated objects in this heap

Source

pub fn contains(&self, handle: impl AsRef<Handle<T>>) -> bool

Return true if the heap contains the specified handle

Source

pub fn get(&self, handle: impl AsRef<Handle<T>>) -> Option<&T>

Get a reference to a heap object if it exists on this heap.

Source

pub unsafe fn get_unchecked(&self, handle: impl AsRef<Handle<T>>) -> &T

Get a reference to a heap object without checking whether it is still alive or that it belongs to this heap.

If either invariant is not upheld, calling this function results in undefined behaviour.

Source

pub fn get_mut(&mut self, handle: impl AsRef<Handle<T>>) -> Option<&mut T>

Get a mutable reference to a heap object

Source

pub unsafe fn get_mut_unchecked( &mut self, handle: impl AsRef<Handle<T>>, ) -> &mut T

Get a mutable reference to a heap object without first checking that it is still alive or that it belongs to this heap.

If either invariant is not upheld, calling this function results in undefined behaviour. Provided they are upheld, this function provides zero-cost access.

Source

pub fn clean_excluding( &mut self, excluding: impl IntoIterator<Item = Handle<T>>, )

Clean orphaned objects from the heap, excluding those that can be reached from the given handle iterator.

This function is useful in circumstances in which you wish to keep certain items alive over a garbage collection without the addition cost of a Rooted handle. An example of this might be stack items in a garbage-collected language

Source

pub fn clean(&mut self)

Clean orphaned objects from the heap.

Trait Implementations§

Source§

impl<T> Default for Heap<T>

Source§

fn default() -> Self

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

impl<T> Drop for Heap<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Heap<T>

§

impl<T> RefUnwindSafe for Heap<T>
where T: RefUnwindSafe,

§

impl<T> !Send for Heap<T>

§

impl<T> !Sync for Heap<T>

§

impl<T> Unpin for Heap<T>

§

impl<T> UnwindSafe for Heap<T>
where T: RefUnwindSafe,

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.