Struct components_arena::Arena
source · pub struct Arena<C: Component + 'static> { /* private fields */ }
Expand description
Unordered container with random access.
Implementations§
§impl<C: Component> Arena<C>
impl<C: Component> Arena<C>
pub const fn new() -> Selfwhere
C::Alloc: ConstDefault,
pub const fn new() -> Selfwhere
C::Alloc: ConstDefault,
Creates an arena instance.
pub fn with_capacity(capacity: usize) -> Selfwhere
C::Alloc: ConstDefault,
pub fn with_capacity(capacity: usize) -> Selfwhere
C::Alloc: ConstDefault,
Creates an arena instance with the specified initial capacity.
source§impl<C: Component> Arena<C>
impl<C: Component> Arena<C>
sourcepub fn with_capacity_in(capacity: usize, alloc: C::Alloc) -> Self
pub fn with_capacity_in(capacity: usize, alloc: C::Alloc) -> Self
Creates an arena instance with the specified initial capacity.
sourcepub fn into_items(self) -> ArenaItems<C>
pub fn into_items(self) -> ArenaItems<C>
Returns contained items packed in a special container. While arena itself is unique (i.e. non-clonable) object, this special container could be cloned.
sourcepub fn items(&self) -> &ArenaItems<C>
pub fn items(&self) -> &ArenaItems<C>
Returns reference to contained items packed in a special container. While arena itself is unique (i.e. non-clonable) object, this special container could be cloned.
sourcepub fn items_mut(&mut self) -> &mut ArenaItems<C>
pub fn items_mut(&mut self) -> &mut ArenaItems<C>
Returns mutable reference to contained items packed in a (mostly read-only) special container. While arena itself is unique (i.e. non-clonable) object, this special container could be cloned.
sourcepub fn reserve(&mut self)
pub fn reserve(&mut self)
Reserves capacity for at least one more element.
The collection may reserve more space to avoid frequent reallocations.
After calling reserve
, capacity will be greater than or equal to
self.items().len() + 1
. Does nothing if capacity is already sufficient.
§Panics
Panics if the new capacity overflows usize.
sourcepub fn reserve_exact(&mut self)
pub fn reserve_exact(&mut self)
Reserves the minimum capacity for exactly one more element.
After calling reserve_exact
, capacity will be greater than or equal to
self.items().len() + 1
. Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it requests.
Therefore, capacity can not be relied upon to be precisely minimal.
Prefer reserve
if future insertions are expected.
§Panics
Panics if the new capacity overflows usize.
sourcepub fn try_reserve(&mut self) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self) -> Result<(), TryReserveError>
Tries to reserve capacity for at least one more element.
The collection may reserve more space to avoid frequent reallocations.
After calling try_reserve
, capacity will be greater than or equal
to self.items().len() + 1
. Does nothing if capacity is already sufficient.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
sourcepub fn try_reserve_exact(&mut self) -> Result<(), TryReserveError>
pub fn try_reserve_exact(&mut self) -> Result<(), TryReserveError>
Tries to reserve capacity for exactly one more element.
The collection may reserve more space to avoid frequent reallocations.
After calling try_reserve_exact
, capacity will be greater than or equal
to self.items().len() + 1
. Does nothing if capacity is already sufficient.
Note that the allocator may give the collection more space than it requests.
Therefore, capacity can not be relied upon to be precisely minimal.
Prefer try_reserve
if future insertions are expected.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
sourcepub fn insert<T>(&mut self, component: impl FnOnce(Id<C>) -> (C, T)) -> T
pub fn insert<T>(&mut self, component: impl FnOnce(Id<C>) -> (C, T)) -> T
Place new component into the arena.
§Examples
let mut arena = Arena::new();
let new_component_id = arena.insert(|id| (MyComponent { /* ... */ }, id));
sourcepub fn remove(&mut self, id: Id<C>) -> C
pub fn remove(&mut self, id: Id<C>) -> C
Removes component with provided id.
The arena tries to detect invalid provided id (i.e. foreign, or previously dropped), and panics if such detection hits. But it is important to pay respect to the fact there is small probability that invalid id will not be intercepted.