A robust and effective inventory system for games.
```rust /* * Implement the Stacksize trait for * your type that will act as your Item */ impl super::Stacksize for char { fn getmaxstacksize(&self) -> usize { 3 } }
fn main() {
let mut inv = Inv::
// cant be placed, slot out of bounds assert!( inv.stackat( 666, ItemStack::new('x', 1) ).iserr() );
// overflow, which is returned to you asserteq!( inv.stackat( 2, ItemStack::new('a', 4) ), Ok(Err( StackErr::StackSizeOverflow( ItemStack::new('a', 1) ) )) );
// stack c at pos 1 inv.stack_at( 1, ItemStack::new('c', 1) ).ok();
// item cant be stacked, // item type does not match (c != y) asserteq!( inv.stackat( 1, ItemStack::new('y', 1) ), Ok(Err( StackErr::ItemTypeDoesNotMatch( ItemStack::new('y', 1) ) )) );
// auto stacking // this first fills slot 1 to be at the max of 3 // since slot 1 already had 1c in it // the leftover will be placed in the first available slot, // which, in this case, is 0 assert!( inv.autostack( ItemStack::new('c', 3) ).isok() );
// 1c3c asserteq!( inv.getslot(0), Ok(&Slot::new(ItemStack::new('c', 1))) ); asserteq!( inv.getslot(1), Ok(&Slot::new(ItemStack::new('c', 3))) );
// you can take a stack out of its slot // first, we place 2t at slot 5 inv.stackat(5, ItemStack::new('t', 1)).ok(); inv.autostack(ItemStack::new('t', 1)).ok();
// now we just take the stack asserteq!( inv.takestack(5), Ok(ItemStack::new('t', 2)) );
// slot 5 is empty now asserteq!( inv.takestack(5), Err(InvAccessErr::SlotEmpty) ); } ``` You can look at the unit tests for more examples.
Feel free to open an issue/PR explaining possible improvements or changes
Also, please do not hesitate and open an issue when needed. I am happy to help!