cargo fmt the previous changes.

This commit is contained in:
Joseph Gerardot 2019-10-31 10:26:25 -04:00
parent eb1b42be0f
commit 7325757066
2 changed files with 57 additions and 34 deletions

View File

@ -41,7 +41,7 @@ impl Inventory {
} }
/// Add a series of items to inventory, returning any which do not fit as an error. /// Add a series of items to inventory, returning any which do not fit as an error.
pub fn push_all<I: Iterator<Item=Item>>(&mut self, mut items: I) -> Result<(), Error> { pub fn push_all<I: Iterator<Item = Item>>(&mut self, mut items: I) -> Result<(), Error> {
// Vec doesn't allocate for zero elements so this should be cheap // Vec doesn't allocate for zero elements so this should be cheap
let mut leftovers = Vec::new(); let mut leftovers = Vec::new();
let mut slots = self.slots.iter_mut(); let mut slots = self.slots.iter_mut();
@ -66,12 +66,11 @@ impl Inventory {
* than necessary (n^2) and with the proper structure wouldn't need to iterate at all, but because * than necessary (n^2) and with the proper structure wouldn't need to iterate at all, but because
* this should be fairly cold code, clarity has been favored over efficiency. * this should be fairly cold code, clarity has been favored over efficiency.
*/ */
pub fn push_all_unique<I: Iterator<Item=Item>>(&mut self, mut items: I) -> Result<(), Error> { pub fn push_all_unique<I: Iterator<Item = Item>>(&mut self, mut items: I) -> Result<(), Error> {
let mut leftovers = Vec::new(); let mut leftovers = Vec::new();
for item in &mut items { for item in &mut items {
if self.contains(&item).not() { if self.contains(&item).not() {
self.push(item) self.push(item).map(|overflow| leftovers.push(overflow));
.map(|overflow| leftovers.push(overflow));
} // else drop item if it was already in } // else drop item if it was already in
} }
if leftovers.len() > 0 { if leftovers.len() > 0 {
@ -100,12 +99,18 @@ impl Inventory {
/// O(n) count the number of items in this inventory. /// O(n) count the number of items in this inventory.
pub fn count(&self) -> usize { pub fn count(&self) -> usize {
self.slots.iter().fold(0, |count, slot| count + if slot.is_some() { 1 } else { 0 }) self.slots
.iter()
.fold(0, |count, slot| count + if slot.is_some() { 1 } else { 0 })
} }
/// O(n) check if an item is in this inventory. /// O(n) check if an item is in this inventory.
pub fn contains(&self, item: &Item) -> bool { pub fn contains(&self, item: &Item) -> bool {
self.slots.iter().any(|slot| slot.as_ref().map(|other_item| item == other_item).unwrap_or(false)) self.slots.iter().any(|slot| {
slot.as_ref()
.map(|other_item| item == other_item)
.unwrap_or(false)
})
} }
/// Get content of a slot /// Get content of a slot
@ -152,14 +157,13 @@ impl Component for InventoryUpdate {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use lazy_static::lazy_static;
use super::*; use super::*;
use lazy_static::lazy_static;
lazy_static! { lazy_static! {
static ref TEST_ITEMS: Vec<Item> = static ref TEST_ITEMS: Vec<Item> = vec![
vec![ assets::load_expect_cloned("common.items.debug.boost"),
assets::load_expect_cloned("common.items.debug.boost"), assets::load_expect_cloned("common.items.debug.possess")
assets::load_expect_cloned("common.items.debug.possess") ];
];
} }
/// The `Default` inventory should contain two items /// The `Default` inventory should contain two items
#[test] #[test]
@ -170,7 +174,9 @@ mod test {
/// Attempting to push into a full inventory should return the same item. /// Attempting to push into a full inventory should return the same item.
#[test] #[test]
fn push_full() { fn push_full() {
let mut inv = Inventory { slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect() }; let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
assert_eq!( assert_eq!(
inv.push(TEST_ITEMS[0].clone()).unwrap(), inv.push(TEST_ITEMS[0].clone()).unwrap(),
TEST_ITEMS[0].clone() TEST_ITEMS[0].clone()
@ -180,27 +186,33 @@ mod test {
/// Attempting to push a series into a full inventory should return them all. /// Attempting to push a series into a full inventory should return them all.
#[test] #[test]
fn push_all_full() { fn push_all_full() {
let mut inv = Inventory { slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect() }; let mut inv = Inventory {
let Error::Full(leftovers) = inv.push_all(TEST_ITEMS.iter().map(|a| a.clone())) slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
let Error::Full(leftovers) = inv
.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
.expect_err("Pushing into a full inventory somehow worked!"); .expect_err("Pushing into a full inventory somehow worked!");
assert_eq!( assert_eq!(leftovers, TEST_ITEMS.clone())
leftovers,
TEST_ITEMS.clone()
)
} }
/// Attempting to push uniquely into an inventory containing all the items should work fine. /// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test] #[test]
fn push_unique_all_full() { fn push_unique_all_full() {
let mut inv = Inventory { slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect() }; let mut inv = Inventory {
slots: TEST_ITEMS.iter().map(|a| Some(a.clone())).collect(),
};
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone())) inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
.expect("Pushing unique items into an inventory that already contains them didn't work!"); .expect(
"Pushing unique items into an inventory that already contains them didn't work!",
);
} }
/// Attempting to push uniquely into an inventory containing all the items should work fine. /// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test] #[test]
fn push_all_empty() { fn push_all_empty() {
let mut inv = Inventory { slots: vec![None, None] }; let mut inv = Inventory {
slots: vec![None, None],
};
inv.push_all(TEST_ITEMS.iter().map(|a| a.clone())) inv.push_all(TEST_ITEMS.iter().map(|a| a.clone()))
.expect("Pushing items into an empty inventory didn't work!"); .expect("Pushing items into an empty inventory didn't work!");
} }
@ -208,8 +220,12 @@ mod test {
/// Attempting to push uniquely into an inventory containing all the items should work fine. /// Attempting to push uniquely into an inventory containing all the items should work fine.
#[test] #[test]
fn push_all_unique_empty() { fn push_all_unique_empty() {
let mut inv = Inventory { slots: vec![None, None] }; let mut inv = Inventory {
slots: vec![None, None],
};
inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone())) inv.push_all_unique(TEST_ITEMS.iter().map(|a| a.clone()))
.expect("Pushing unique items into an empty inventory that didn't contain them didn't work!"); .expect(
"Pushing unique items into an empty inventory that didn't contain them didn't work!",
);
} }
} }

View File

@ -997,15 +997,20 @@ fn handle_exp(server: &mut Server, entity: EcsEntity, args: String, action: &Cha
} }
use common::comp::Item; use common::comp::Item;
fn handle_debug_items(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { fn handle_debug_items(
server: &mut Server,
entity: EcsEntity,
_args: String,
_action: &ChatCommand,
) {
if let Ok(items) = assets::load_glob::<Item>("common.items.debug.*") { if let Ok(items) = assets::load_glob::<Item>("common.items.debug.*") {
server server
.state() .state()
.ecs() .ecs()
.write_storage::<comp::Inventory>() .write_storage::<comp::Inventory>()
.get_mut(entity) .get_mut(entity)
// TODO: Consider writing a `load_glob_cloned` in `assets` and using that here // TODO: Consider writing a `load_glob_cloned` in `assets` and using that here
.map(|inv| inv.push_all_unique(items.iter().map(|item| item.as_ref().clone()))); .map(|inv| inv.push_all_unique(items.iter().map(|item| item.as_ref().clone())));
let _ = server let _ = server
.state .state
.ecs() .ecs()
@ -1013,8 +1018,10 @@ fn handle_debug_items(server: &mut Server, entity: EcsEntity, _args: String, _ac
.insert(entity, comp::InventoryUpdate); .insert(entity, comp::InventoryUpdate);
} else { } else {
server.notify_client( server.notify_client(
entity, entity,
ServerMsg::private(String::from("Debug items not found? Something is very broken.")) ServerMsg::private(String::from(
"Debug items not found? Something is very broken.",
)),
); );
} }
} }