pub struct Inventory<const N: usize> { /* private fields */ }Implementations§
Source§impl<const N: usize> Inventory<N>
impl<const N: usize> Inventory<N>
pub fn new() -> Result<Inventory<N>, LoopError>
pub fn stacks(&self) -> impl Iterator<Item = Stack>
pub fn count(&self, item: u32) -> u32
pub fn add(&mut self, stack: Stack) -> Result<(), LoopError>
pub fn remove(&mut self, item: u32, amount: u32) -> Result<(), LoopError>
Sourcepub fn check_exchange<const M: usize>(
&self,
other: &Inventory<M>,
give: &[(u32, u32)],
take: &[(u32, u32)],
) -> Result<(), ExchangeError>
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,
}));Sourcepub fn exchange<const M: usize>(
&mut self,
other: &mut Inventory<M>,
give: &[(u32, u32)],
take: &[(u32, u32)],
) -> Result<(), ExchangeError>
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));Sourcepub fn check_transfer_to<const M: usize>(
&self,
destination: &Inventory<M>,
items: &[(u32, u32)],
) -> Result<(), ExchangeError>
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));Sourcepub fn transfer_to<const M: usize>(
&mut self,
destination: &mut Inventory<M>,
items: &[(u32, u32)],
) -> Result<(), ExchangeError>
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);