From 5d02d32ec64e7b81b94337c1017ab87e240bd4d4 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Fri, 30 Aug 2019 22:42:43 +0200 Subject: [PATCH] Make inventory::get return a reference --- common/src/comp/inventory/item.rs | 2 +- common/src/comp/inventory/mod.rs | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/common/src/comp/inventory/item.rs b/common/src/comp/inventory/item.rs index 69dc627ffc..deb885dad6 100644 --- a/common/src/comp/inventory/item.rs +++ b/common/src/comp/inventory/item.rs @@ -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, diff --git a/common/src/comp/inventory/mod.rs b/common/src/comp/inventory/mod.rs index 94eba9b21a..6bcf46d79e 100644 --- a/common/src/comp/inventory/mod.rs +++ b/common/src/comp/inventory/mod.rs @@ -45,24 +45,25 @@ impl Inventory { } } - // Get info about an item slot - pub fn get(&self, cell: usize) -> Option { - 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 { //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 { self.slots.get_mut(cell).and_then(|item| item.take()) }