Make inventory::get return a reference

This commit is contained in:
timokoesters 2019-08-30 22:42:43 +02:00
parent 082ccd7c46
commit 5d02d32ec6
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097
2 changed files with 7 additions and 6 deletions

View File

@ -81,7 +81,7 @@ pub enum Debug {
Boost,
}
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Item {
Tool {
kind: Tool,

View File

@ -45,24 +45,25 @@ impl Inventory {
}
}
// Get info about an item slot
pub fn get(&self, cell: usize) -> Option<Item> {
self.slots.get(cell).cloned().flatten()
/// Get content of a slot
pub fn get(&self, cell: usize) -> Option<&Item> {
self.slots.get(cell).and_then(Option::as_ref)
}
// Insert an item to a slot if its empty
/// Insert an item into a slot if its empty
pub fn swap(&mut self, cell: usize, item: Item) -> Option<Item> {
//TODO: Check if a slot is empty first.
self.slots.get_mut(cell).and_then(|cell| cell.replace(item))
}
/// Swap the items inside of two slots
pub fn swap_slots(&mut self, a: usize, b: usize) {
if a.max(b) < self.slots.len() {
self.slots.swap(a, b);
}
}
// Remove an item from the slot
/// Remove an item from the slot
pub fn remove(&mut self, cell: usize) -> Option<Item> {
self.slots.get_mut(cell).and_then(|item| item.take())
}