Skip to main content

Inventory

Struct Inventory 

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

Source

pub fn new() -> Self

Create an empty inventory with N slots and no per-slot quantity cap.

Source

pub fn with_capacity(max_per_slot: u32) -> Self

Create an empty inventory with N slots and the given per-slot quantity cap.

Source

pub fn count(&self) -> usize

Number of distinct item slots (not total item count).

Source

pub fn is_empty(&self) -> bool

Returns true if the inventory holds no items.

Source

pub fn capacity(&self) -> usize

Maximum number of distinct item slots (N).

Source

pub fn contains(&self, item: &I, quantity: u32) -> bool

Returns true if the inventory holds at least quantity of item.

Source

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.

Source

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

Source

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

Quantity of item in the inventory (0 if not owned).

Source

pub fn is_full(&self) -> bool

Returns true if the inventory has reached its slot limit.

Source

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.

Source

pub fn filter<F>(&self, pred: F) -> Vec<&(I, u32)>
where F: Fn(&I) -> bool,

Return all slot entries matching a predicate.

Source

pub fn sort_by<F>(&mut self, cmp: F)
where F: FnMut(&(I, u32), &(I, u32)) -> Ordering,

Sort slots by a custom comparator.

Source

pub fn sort_by_name<F>(&mut self, name_fn: F)
where F: Fn(&I) -> &str,

Sort slots by item name, using the provided name_fn to extract a string name from each item.

Source

pub fn into_inner(self) -> Vec<(I, u32)>

Consume the inventory and return its occupied slots as a Vec.

Source

pub fn iter(&self) -> impl Iterator<Item = &(I, u32)>

Iterate over all (item, quantity) entries.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (I, u32)>

Mutably iterate over all (item, quantity) entries.

Source

pub fn get(&self, index: usize) -> Option<&(I, u32)>

The (item, quantity) entry at index, if occupied.

Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut (I, u32)>

Mutable access to the (item, quantity) entry at index.

Source

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.

Source

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

Source

pub fn swap(&mut self, a: usize, b: usize)

Swap the two slots at a and b (both must be occupied).

§Panics

Panics if either index is out of bounds.

Source

pub fn clear(&mut self)

Remove all items.

Trait Implementations§

Source§

impl<I: Clone + Copy + Eq + Hash + Debug, const N: usize> Clone for Inventory<I, N>

Source§

fn clone(&self) -> Inventory<I, N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<I: Debug + Copy + Eq + Hash + Debug, const N: usize> Debug for Inventory<I, N>

Source§

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

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

impl<I: Copy + Eq + Hash + Debug, const N: usize> Default for Inventory<I, N>

Source§

fn default() -> Self

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

impl<I: Eq + Copy + Eq + Hash + Debug, const N: usize> Eq for Inventory<I, N>

Source§

impl<I: PartialEq + Copy + Eq + Hash + Debug, const N: usize> PartialEq for Inventory<I, N>

Source§

fn eq(&self, other: &Inventory<I, N>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<I: PartialEq + Copy + Eq + Hash + Debug, const N: usize> StructuralPartialEq for Inventory<I, N>

Auto Trait Implementations§

§

impl<I, const N: usize> Freeze for Inventory<I, N>
where I: Freeze,

§

impl<I, const N: usize> RefUnwindSafe for Inventory<I, N>
where I: RefUnwindSafe,

§

impl<I, const N: usize> Send for Inventory<I, N>
where I: Send,

§

impl<I, const N: usize> Sync for Inventory<I, N>
where I: Sync,

§

impl<I, const N: usize> Unpin for Inventory<I, N>
where I: Unpin,

§

impl<I, const N: usize> UnsafeUnpin for Inventory<I, N>
where I: UnsafeUnpin,

§

impl<I, const N: usize> UnwindSafe for Inventory<I, N>
where I: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.