Inventory

Struct Inventory 

Source
pub struct Inventory<K, S, U>
where S: SlotType, U: Default,
{ pub content: Vec<Option<ItemInstance<K, U>>>, pub slot_restriction: Vec<Option<S>>, pub move_to_front: MoveToFrontMode, pub sizing_mode: InventorySizingMode, }
Expand description

§Generics

  • K: Item Type
  • S: Type of inventory location
  • U: Custom item data

Fields§

§content: Vec<Option<ItemInstance<K, U>>>

The contents of the Inventory. None values indicate empty but existing inventory slots.

§slot_restriction: Vec<Option<S>>

Restricts what kind of item can go in different slots. This is not compatible with InventorySizingMode::Dynamic.

Maps to the inventory content using the index. None values indicate that there are no restrictions for that slot.

§move_to_front: MoveToFrontMode

Configures how item deletion is handled.

§sizing_mode: InventorySizingMode

Configures if the inventory resizes when item are inserted/removed or not.

Implementations§

Source§

impl<K, S, U> Inventory<K, S, U>
where S: SlotType, U: Default,

Source

pub fn new( content: Vec<Option<ItemInstance<K, U>>>, slot_restriction: Vec<Option<S>>, move_to_front: MoveToFrontMode, sizing_mode: InventorySizingMode, ) -> Inventory<K, S, U>

Constructs a new Inventory.

Source§

impl<K, S, U> Inventory<K, S, U>
where K: PartialEq + Clone + Debug + Hash + Eq, S: SlotType, U: Default + Clone + Debug + PartialEq,

Source

pub fn new_fixed(count: usize) -> Inventory<K, S, U>

Creates a new Inventory with a fixed slot count.

Source

pub fn new_dynamic(minimum: usize, maximum: usize) -> Inventory<K, S, U>

Creates a new dynamically sized Inventory. A minimum of minimum slots are garanteed to be present at all time. The quantity of slots will not go over maximum.

Source

pub fn use_item(&mut self, idx: usize) -> Result<Option<usize>, ItemError<K, U>>

Will attempt to decrease the durability of the item at the specified index. If the item has no durability value (None) or a non zero durability, it will return this value. If the item has a durability of 0 when using it, it will break and ItemError::ItemDestroyed will be returned.

Source

pub fn consume(&mut self, idx: usize) -> Result<usize, ItemError<K, U>>

Decreases the stack size by one and returns the current value. Once the stack size hits zero, it will return ItemError::StackConsumed.

Source

pub fn has_space(&self) -> bool

Looks if there is enough space to add another item stack.

Source

pub fn transfer<U2>( &mut self, from_idx: usize, target: &mut Inventory<K, S, U>, to_idx: usize, quantity: usize, _with_overflow: bool, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>
where U2: Default,

Transfers a specified quantity of item from one slot of this inventory to a specified slot of the provided target inventory. with_overflow indicates if the item can be spread out in free slots in case that the target slot does not have enough free space.

Errors: See Transform::delete and Transform::insert_into.

Source

pub fn transfer_stack<U2>( &mut self, from_idx: usize, target: &mut Inventory<K, S, U>, to_idx: usize, with_overflow: bool, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>
where U2: Default,

Transfers a whole stack from the specified slot into a specified slot of the provided target directory. with_overflow indicates if the item can be spread out in free slots in case that the target slot does not have enough free space.

Errors: See Transform::delete and Transform::insert_into.

Source

pub fn move_item<U2>( &mut self, from_idx: usize, to_idx: usize, quantity: usize, _with_overflow: bool, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>
where U2: Default,

Moves a specified quantity of item from a slot to another. with_overflow indicates if the item can be spread out in free slots in case that the target slot does not have enough free space.

Errors: See Inventory::delete and Inventory::insert_into.

Source

pub fn move_stack<U2>( &mut self, from_idx: usize, to_idx: usize, with_overflow: bool, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>
where U2: Default,

Moves a full stack of item from a slot to another. with_overflow indicates if the item can be spread out in free slots in case that the target slot does not have enough free space.

Errors:

  • SlotEmpty: Nothing is present in the specified slot.
Source

pub fn delete( &mut self, idx: usize, quantity: usize, ) -> Result<ItemInstance<K, U>, ItemError<K, U>>

Deletes a specified quantity of item from the specified slot.

Errors:

  • NotEnoughQuantity: Not enough items are present in the item stack.
  • SlotEmpty: Nothing is present in the specified slot.
Source

pub fn delete_stack( &mut self, idx: usize, ) -> Result<ItemInstance<K, U>, ItemError<K, U>>

Deletes a full stack of item at the provided index and returns it.

Errors: See Transform::delete.

Source

pub fn delete_key( &mut self, key: &K, quantity: usize, ) -> Result<ItemInstance<K, U>, ItemError<K, U>>

Deletes items by matching the key until the deleted quantity reaches the specified quantity.

Errors:

  • NotEnoughQuantity: Not enough items with the specified key are present in the inventory.
Source

pub fn has_quantity(&self, key: &K, quantity: usize) -> bool

Checks if the total quantity of items of the specified key are present in the inventory.

Source

pub fn has(&self, key: &K) -> bool

Checks if the inventory contains at least one ItemInstance of the specified key.

Source

pub fn get(&self, idx: usize) -> &Option<ItemInstance<K, U>>

Gets an immutable reference to the ItemInstance at the specified index.

Source

pub fn get_mut(&mut self, idx: usize) -> Option<&mut ItemInstance<K, U>>

Gets a mutable reference to the ItemInstance at the specified index.

Source

pub fn get_key(&self, key: &K) -> impl Iterator<Item = &ItemInstance<K, U>>

Finds the item instances using the specified key. Returns an iterator of immutable references.

Source

pub fn get_key_mut( &mut self, key: &K, ) -> impl Iterator<Item = &mut ItemInstance<K, U>>

Finds the item instances using the specified key. Returns an iterator of mutable references.

Source

pub fn insert_into<U2>( &mut self, idx: usize, item: ItemInstance<K, U>, _item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>
where U2: Default,

Inserts the ItemInstance into the specified index.

It will eventually attempt to merge stacks together, but this is not implemented yet.

Errors:

  • SlotOccupied: The slot is currently occupied by another item type.
Source

pub fn insert<U2>( &mut self, item: ItemInstance<K, U>, item_defs: &ItemDefinitions<K, S, U2>, ) -> Result<(), ItemError<K, U>>
where U2: Default,

Inserts the ItemInstance at the first available inventory space. If the inventory is dynamically size, it will attempt to create a slot and insert into it.

It will eventually attempt to merge stacks together, but this is not implemented yet.

Errors:

  • InventoryFull: The inventory is full and no more space can be created.
Source

pub fn first_empty_slot(&self) -> Option<usize>

Returns the first empty slot if any is available.

Trait Implementations§

Source§

impl<K, S, U> Clone for Inventory<K, S, U>
where K: Clone, S: Clone + SlotType, U: Clone + Default,

Source§

fn clone(&self) -> Inventory<K, S, U>

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<K, S, U> Debug for Inventory<K, S, U>
where K: Debug, S: Debug + SlotType, U: Debug + Default,

Source§

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

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

impl<'de, K, S, U> Deserialize<'de> for Inventory<K, S, U>
where S: SlotType + Deserialize<'de>, U: Default + Deserialize<'de>, K: Deserialize<'de>,

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Inventory<K, S, U>, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K, S, U> Serialize for Inventory<K, S, U>

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<K, S, U> Freeze for Inventory<K, S, U>

§

impl<K, S, U> RefUnwindSafe for Inventory<K, S, U>

§

impl<K, S, U> Send for Inventory<K, S, U>
where S: Send, K: Send, U: Send,

§

impl<K, S, U> Sync for Inventory<K, S, U>
where S: Sync, K: Sync, U: Sync,

§

impl<K, S, U> Unpin for Inventory<K, S, U>
where S: Unpin, K: Unpin, U: Unpin,

§

impl<K, S, U> UnwindSafe for Inventory<K, S, U>
where S: UnwindSafe, K: UnwindSafe, U: 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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,