pub trait Container<I: ItemType> {
// Required methods
fn add(&mut self, item: I, count: u32) -> u32;
fn remove(&mut self, item: I, count: u32) -> u32;
fn count_of(&self, item: I) -> u32;
fn capacity(&self) -> usize;
fn is_full(&self) -> bool;
fn iter_slots(&self) -> impl Iterator<Item = Option<&InventorySlot<I>>>;
fn slot_at(&self, index: usize) -> Option<&InventorySlot<I>>;
// Provided method
fn try_exchange_atomic(
&mut self,
_remove: &[(I, u32)],
_add: &[(I, u32)],
) -> Option<Result<(), ContainerExchangeError>> { ... }
}Expand description
Abstraction over slot-based item storage.
Any type that holds items in indexed slots can implement this trait to be usable with the framework’s pickup, hotbar, and crafting systems.
The framework provides two built-in implementors:
Inventory<I>— the standalone resource form (backward-compatible).ContainerComponent<I>— the Bevy component form used by the new entity-based pipeline.
Required Methods§
Sourcefn add(&mut self, item: I, count: u32) -> u32
fn add(&mut self, item: I, count: u32) -> u32
Adds count of item. Returns the number of items that could NOT be
inserted due to capacity (0 = all inserted).
Sourcefn remove(&mut self, item: I, count: u32) -> u32
fn remove(&mut self, item: I, count: u32) -> u32
Removes up to count of item. Returns the number actually removed.
Sourcefn iter_slots(&self) -> impl Iterator<Item = Option<&InventorySlot<I>>>
fn iter_slots(&self) -> impl Iterator<Item = Option<&InventorySlot<I>>>
Iterates over all slots, yielding Some(&InventorySlot<I>) for occupied
slots and None for empty ones. Yields exactly capacity() items.
Sourcefn slot_at(&self, index: usize) -> Option<&InventorySlot<I>>
fn slot_at(&self, index: usize) -> Option<&InventorySlot<I>>
Returns the slot at index, or None if the index is out of bounds or
the slot is empty. Never panics.
Provided Methods§
Sourcefn try_exchange_atomic(
&mut self,
_remove: &[(I, u32)],
_add: &[(I, u32)],
) -> Option<Result<(), ContainerExchangeError>>
fn try_exchange_atomic( &mut self, _remove: &[(I, u32)], _add: &[(I, u32)], ) -> Option<Result<(), ContainerExchangeError>>
Optionally consume and produce a complete exchange atomically.
None means unsupported; None and Some(Err(_)) MUST leave the
container unchanged. Some(Ok(())) means every quantity was exchanged.
Normalize duplicates by equality in first-occurrence order, ignore zero
quantities, allow empty lists, and reject positive default/empty sentinels
and checked u32 sums that overflow. Evaluate capacity after consumption.
The default keeps existing implementors compatible; legacy crafting may
partially mutate when their individual operations fail.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".