Skip to main content

Container

Trait Container 

Source
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:

Required Methods§

Source

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).

Source

fn remove(&mut self, item: I, count: u32) -> u32

Removes up to count of item. Returns the number actually removed.

Source

fn count_of(&self, item: I) -> u32

Total number of item across all slots.

Source

fn capacity(&self) -> usize

Maximum number of distinct item stacks this container can hold.

Source

fn is_full(&self) -> bool

Returns true if every slot is occupied.

Source

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.

Source

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§

Source

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".

Implementors§