Skip to main content

NodePool

Struct NodePool 

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

A reusable pool of A11yNode allocations, keyed by NodeId.

Reduces per-frame heap allocations when the accessibility tree is rebuilt on every frame: rather than dropping and re-allocating every node, the pool keeps previously-allocated structs (and their internal Vec allocations) on a free list.

§Invariants

  • A node is either active (in active) or free (in free_list), never both simultaneously.
  • After recycle, active is empty and free_list holds all previously-active nodes.
  • After clear, both maps are empty.

Implementations§

Source§

impl NodePool

Source

pub fn new() -> Self

Create a new, empty pool.

Source

pub fn alloc(&mut self, id: NodeId, node: A11yNode)

Place node into the active map under id.

If a node with the same id already exists it is silently replaced and the old node is dropped (not returned to the free list, to avoid a same-id duplicate).

Source

pub fn alloc_recycled( &mut self, id: NodeId, role: WidgetRole, label: Option<String>, ) -> &mut A11yNode

Allocate a node slot, optionally reusing memory from the free list.

If a free node is available its struct is taken from the free list and reset to the supplied id, role, and label before being placed in the active map. Otherwise a fresh node is created.

This is the preferred allocation path for hot paths that care about minimising heap allocation churn.

Source

pub fn get(&self, id: &NodeId) -> Option<&A11yNode>

Retrieve an active node by its NodeId.

Source

pub fn get_mut(&mut self, id: &NodeId) -> Option<&mut A11yNode>

Retrieve a mutable reference to an active node.

Source

pub fn recycle(&mut self)

Move all active nodes back to the free list for reuse next frame.

After this call active_count() == 0 and free_count() reflects the total number of recycled nodes.

Source

pub fn active_count(&self) -> usize

Number of currently-active nodes.

Source

pub fn free_count(&self) -> usize

Number of nodes available for reuse.

Source

pub fn clear(&mut self)

Discard all nodes (active and free).

Source

pub fn iter_active(&self) -> impl Iterator<Item = (&NodeId, &A11yNode)>

Iterate over all active nodes.

Trait Implementations§

Source§

impl Debug for NodePool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NodePool

Source§

fn default() -> NodePool

Returns the “default value” for a 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.