Return the item again if slot was not wrong

This commit is contained in:
timokoesters 2019-08-30 21:02:18 +02:00
parent b063694d05
commit 7e66fe792d
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097

View File

@ -21,25 +21,27 @@ impl Inventory {
self.slots.len()
}
/// Adds a new item to the first empty slot of the inventory. Returns the item again if no free
/// slot was found.
pub fn push(&mut self, item: Item) -> Option<Item> {
match self.slots.iter_mut().find(|slot| slot.is_none()) {
Some(slot) => {
let old = slot.take();
*slot = Some(item);
old
None
}
None => None,
None => Some(item),
}
}
/// Replaces an item in a specific slot of the inventory. Returns the item again if that slot
/// was not found.
pub fn insert(&mut self, cell: usize, item: Option<Item>) -> Option<Item> {
match self.slots.get_mut(cell) {
Some(slot) => {
let old = slot.take();
*slot = item;
old
None
}
None => None,
None => item,
}
}