Skip to main content

Store

Struct Store 

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

Components of one type, addressed by entity slot.

Implementations§

Source§

impl<T> Store<T>

Source

pub const fn new() -> Self

An empty store.

Source

pub fn len(&self) -> usize

How many components are stored.

Source

pub fn is_empty(&self) -> bool

Whether the store holds nothing.

Source

pub fn contains(&self, slot: u32) -> bool

Whether this entity slot has a component here.

Source

pub fn get(&self, slot: u32) -> Option<&T>

The component for this slot.

Source

pub fn get_mut(&mut self, slot: u32) -> Option<&mut T>

The component for this slot, mutably.

Source

pub fn insert(&mut self, slot: u32, value: T) -> Option<T>

Store a component, returning the one it replaced.

Source

pub fn remove(&mut self, slot: u32) -> Option<T>

Remove a component, returning it.

The last element is swapped into the hole, so this is constant time and values stays contiguous — at the cost of dense losing any order it had, which is the trade Store::iter pays for.

Source

pub fn iter(&self) -> impl Iterator<Item = (u32, &T)> + '_

Every component, in ascending entity-slot order.

This is the order the engine promises, and it is why the store exists in this shape. It walks sparse, so its cost is proportional to the highest occupied slot rather than to the number of components — a store scattered across a wide slot range pays for the gaps. That is the measured cost of a defined order, and the entity allocator reuses low slots first precisely to keep it small.

Source

pub fn for_each_mut(&mut self, visit: impl FnMut(u32, &mut T))

Every component in slot order, mutably.

Collects the visit order first, because handing out &mut while borrowing sparse to decide the order is not something the borrow checker will allow — and the honest fix is one allocation per call, not unsafe.

Source

pub fn iter_unordered(&self) -> impl Iterator<Item = &T> + '_

The components in storage order, which is unspecified.

Offered because it is what a system that does not care about order should use: it is a flat walk of a contiguous array, with none of the gap-skipping Store::iter pays for. Any system whose result depends on the order it sees is wrong to use this, and the name is the warning.

Trait Implementations§

Source§

impl<T: Debug> Debug for Store<T>

Source§

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

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

impl<T> Default for Store<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for Store<T>

§

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

§

impl<T> Send for Store<T>
where T: Send,

§

impl<T> Sync for Store<T>
where T: Sync,

§

impl<T> Unpin for Store<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Store<T>

§

impl<T> UnwindSafe for Store<T>
where T: UnwindSafe,

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.