Struct Inventory

Source
pub struct Inventory<const N: usize> { /* private fields */ }

Implementations§

Source§

impl<const N: usize> Inventory<N>

Source

pub fn new() -> Result<Inventory<N>, LoopError>

Source

pub fn stacks(&self) -> impl Iterator<Item = Stack>

Source

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

Source

pub fn add(&mut self, stack: Stack) -> Result<(), LoopError>

Source

pub fn remove(&mut self, item: u32, amount: u32) -> Result<(), LoopError>

Source

pub fn check_exchange<const M: usize>( &self, other: &Inventory<M>, give: &[(u32, u32)], take: &[(u32, u32)], ) -> Result<(), ExchangeError>

Preflight an exchange without changing either inventory.

give contains (item, amount) entries from self; take contains entries from other. Each list may contain at most MAX_EXCHANGE_ITEMS entries. Repeated items are summed in u64 before checking availability, and zero amounts are rejected. Empty lists are valid, including a no-op exchange. Incoming items cannot fund outgoing requirements.

Both inventories release outgoing items before receiving anything, so full inventories can swap stacks. Each incoming stack retains its source limit and must match any remaining stack of that item in the recipient. A fully removed stack imposes no limit on its replacement, just as with Self::remove followed by Self::add.

Checks use bounded local copies with no heap allocation. List lengths are checked first, then outgoing requirements for This and Other, then incoming capacity for This and Other. Within each list, zero amounts are checked before availability; distinct items follow first occurrence order. A later Self::exchange rechecks the current state.

use ferravex_play::gameplay::inventory::{ExchangeError, ExchangeSide, Inventory};
let bag = Inventory::<1>::new().unwrap();
let chest = Inventory::<2>::new().unwrap();
assert_eq!(bag.check_exchange(&chest, &[(7, 2), (7, 3)], &[]),
    Err(ExchangeError::MissingItem {
        side: ExchangeSide::This, item: 7, requested: 5, available: 0,
    }));
Source

pub fn exchange<const M: usize>( &mut self, other: &mut Inventory<M>, give: &[(u32, u32)], take: &[(u32, u32)], ) -> Result<(), ExchangeError>

Atomically trade existing items, using Self::check_exchange’s rules.

Success conserves each item’s combined total. Any error preserves both inventories exactly, including slot order and stack limits. Transactions are synchronous; hosts remain responsible for authorizing the trade.

use ferravex_play::gameplay::inventory::{Inventory, Stack};
let mut bag = Inventory::<1>::new().unwrap();
let mut trader = Inventory::<1>::new().unwrap();
bag.add(Stack { item: 1, amount: 3, limit: 10 }).unwrap();
trader.add(Stack { item: 2, amount: 1, limit: 5 }).unwrap();
bag.exchange(&mut trader, &[(1, 3)], &[(2, 1)]).unwrap();
assert_eq!((bag.count(1), bag.count(2)), (0, 1));
assert_eq!((trader.count(1), trader.count(2)), (3, 0));
Source

pub fn check_transfer_to<const M: usize>( &self, destination: &Inventory<M>, items: &[(u32, u32)], ) -> Result<(), ExchangeError>

Preflight a one-way transfer; equivalent to check_exchange with no take.

use ferravex_play::gameplay::inventory::{Inventory, Stack};
let mut bag = Inventory::<1>::new().unwrap();
let chest = Inventory::<2>::new().unwrap();
bag.add(Stack { item: 7, amount: 3, limit: 5 }).unwrap();
bag.check_transfer_to(&chest, &[(7, 2)]).unwrap();
assert_eq!((bag.count(7), chest.count(7)), (3, 0));
Source

pub fn transfer_to<const M: usize>( &mut self, destination: &mut Inventory<M>, items: &[(u32, u32)], ) -> Result<(), ExchangeError>

Atomically move a batch of items, retaining their original stack limits.

Equivalent to Self::exchange with an empty take list. On failure, neither inventory changes, even if earlier entries would have fitted.

use ferravex_play::gameplay::inventory::{Inventory, Stack};
let mut bag = Inventory::<2>::new().unwrap();
let mut chest = Inventory::<1>::new().unwrap();
bag.add(Stack { item: 7, amount: 3, limit: 5 }).unwrap();
bag.transfer_to(&mut chest, &[(7, 1), (7, 1)]).unwrap();
assert_eq!((bag.count(7), chest.count(7)), (1, 2));
assert_eq!(chest.stacks().next().unwrap().limit, 5);

Trait Implementations§

Source§

impl<const N: usize> Clone for Inventory<N>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for Inventory<N>

Source§

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

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

impl<const N: usize> Copy for Inventory<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Inventory<N>

§

impl<const N: usize> RefUnwindSafe for Inventory<N>

§

impl<const N: usize> Send for Inventory<N>

§

impl<const N: usize> Sync for Inventory<N>

§

impl<const N: usize> Unpin for Inventory<N>

§

impl<const N: usize> UnwindSafe for Inventory<N>

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.