pub struct Inventory<I: Copy + Eq + Hash + Debug, const N: usize> { /* private fields */ }Expand description
Generic item inventory that stores (item, quantity) pairs.
The type parameter I is the item identifier type, which must satisfy
Copy + Eq + Hash + Debug. Items with the same identity are stacked
into a single slot — no duplicate entries.
The const parameter N is the fixed slot capacity: the inventory is
stored inline in an array of N slots (zero heap allocation) and can hold
at most N distinct items. max_per_slot is an optional quantity cap;
when None (the default, via new) quantities are
effectively unlimited.
Occupied slots are kept contiguous at the front of the backing array, so
removal shifts subsequent slots left — identical ordering semantics to the
former Vec-backed implementation.
Implementations§
Source§impl<I: Copy + Eq + Hash + Debug, const N: usize> Inventory<I, N>
impl<I: Copy + Eq + Hash + Debug, const N: usize> Inventory<I, N>
Sourcepub fn with_capacity(max_per_slot: u32) -> Self
pub fn with_capacity(max_per_slot: u32) -> Self
Create an empty inventory with N slots and the given per-slot
quantity cap.
Sourcepub fn contains(&self, item: &I, quantity: u32) -> bool
pub fn contains(&self, item: &I, quantity: u32) -> bool
Returns true if the inventory holds at least quantity of item.
Sourcepub fn add(&mut self, item: I, quantity: u32) -> Result<(), AddError>
pub fn add(&mut self, item: I, quantity: u32) -> Result<(), AddError>
Add quantity copies of item.
If the item already exists in a slot the quantities are merged; otherwise a new slot is appended.
§Errors
Returns AddError::InventoryFull if the inventory has reached its
slot limit and the item would require a new slot.
Returns AddError::PerSlotCapReached if the combined quantity would
exceed the per-slot cap.
Sourcepub fn remove(&mut self, item: &I, quantity: u32) -> bool
pub fn remove(&mut self, item: &I, quantity: u32) -> bool
Remove up to quantity copies of item from the first matching slot.
Returns true if the removal succeeded (item found and quantity
sufficient).
Sourcepub fn would_exceed_per_slot_cap(&self, item: &I, add_quantity: u32) -> bool
pub fn would_exceed_per_slot_cap(&self, item: &I, add_quantity: u32) -> bool
Returns true if adding add_quantity of item would exceed the
per-slot quantity cap.
Sourcepub fn filter<F>(&self, pred: F) -> Vec<&(I, u32)>
pub fn filter<F>(&self, pred: F) -> Vec<&(I, u32)>
Return all slot entries matching a predicate.
Sourcepub fn sort_by_name<F>(&mut self, name_fn: F)
pub fn sort_by_name<F>(&mut self, name_fn: F)
Sort slots by item name, using the provided name_fn to extract a
string name from each item.
Sourcepub fn into_inner(self) -> Vec<(I, u32)>
pub fn into_inner(self) -> Vec<(I, u32)>
Consume the inventory and return its occupied slots as a Vec.
Sourcepub fn iter(&self) -> impl Iterator<Item = &(I, u32)>
pub fn iter(&self) -> impl Iterator<Item = &(I, u32)>
Iterate over all (item, quantity) entries.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (I, u32)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (I, u32)>
Mutably iterate over all (item, quantity) entries.
Sourcepub fn get(&self, index: usize) -> Option<&(I, u32)>
pub fn get(&self, index: usize) -> Option<&(I, u32)>
The (item, quantity) entry at index, if occupied.
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut (I, u32)>
pub fn get_mut(&mut self, index: usize) -> Option<&mut (I, u32)>
Mutable access to the (item, quantity) entry at index.
Sourcepub fn push_slot(&mut self, item: I, quantity: u32) -> Result<(), AddError>
pub fn push_slot(&mut self, item: I, quantity: u32) -> Result<(), AddError>
Append a new slot at the end (caller must ensure !is_full()).
Per-slot quantities are not merged or capped here.
Sourcepub fn remove_at(&mut self, index: usize)
pub fn remove_at(&mut self, index: usize)
Remove the slot at index, shifting subsequent slots left.
§Panics
Panics if index is out of bounds (not an occupied slot).