diff options
-rw-r--r-- | .circleci/config.yml | 14 | ||||
-rw-r--r-- | src/description.rs | 6 | ||||
-rw-r--r-- | src/display/color.rs | 10 | ||||
-rw-r--r-- | src/display/draw_box.rs | 2 | ||||
-rw-r--r-- | src/display/mod.rs | 10 | ||||
-rw-r--r-- | src/display/viewport.rs | 2 | ||||
-rw-r--r-- | src/entities/character.rs | 2 | ||||
-rw-r--r-- | src/entities/creature.rs | 2 | ||||
-rw-r--r-- | src/entities/entity.rs | 2 | ||||
-rw-r--r-- | src/entities/entity_char.rs | 2 | ||||
-rw-r--r-- | src/entities/environment.rs | 2 | ||||
-rw-r--r-- | src/entities/item.rs | 2 | ||||
-rw-r--r-- | src/game.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 10 | ||||
-rw-r--r-- | src/types/entity_map.rs | 51 | ||||
-rw-r--r-- | src/types/mod.rs | 1 | ||||
-rw-r--r-- | src/util/promise.rs | 8 | ||||
-rw-r--r-- | src/util/template.rs | 10 |
18 files changed, 72 insertions, 76 deletions
diff --git a/.circleci/config.yml b/.circleci/config.yml index 08addd550019..ffde5e985462 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -20,16 +20,16 @@ jobs: - checkout - rust/update_toolchain - rust/format - lint: - executor: rust/default - steps: - - checkout - - rust/update_toolchain - - rust/clippy + # lint: + # executor: rust/default + # steps: + # - checkout + # - rust/update_toolchain + # - rust/clippy workflows: default: jobs: - - lint + # - lint - format - build - test: diff --git a/src/description.rs b/src/description.rs index 31f39f7578c2..48c98d76e051 100644 --- a/src/description.rs +++ b/src/description.rs @@ -69,12 +69,12 @@ mod tests { #[test] fn test_describe_list() { assert_eq!( - describe_list(&vec![Description("one".to_string())]), + describe_list(&[Description("one".to_string())]), "one".to_string() ); assert_eq!( - describe_list(&vec![ + describe_list(&[ Description("one".to_string()), Description("two".to_string()) ]), @@ -82,7 +82,7 @@ mod tests { ); assert_eq!( - describe_list(&vec![ + describe_list(&[ Description("one".to_string()), Description("two".to_string()), Description("three".to_string()) diff --git a/src/display/color.rs b/src/display/color.rs index 2a023f1d9564..b1e799c5ed9f 100644 --- a/src/display/color.rs +++ b/src/display/color.rs @@ -16,21 +16,21 @@ impl Color { } impl color::Color for Color { - fn write_fg(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn write_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.write_fg(f) } - fn write_bg(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn write_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.write_bg(f) } } impl<'a> color::Color for &'a Color { - fn write_fg(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn write_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.write_fg(f) } - fn write_bg(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn write_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.write_bg(f) } } @@ -56,7 +56,7 @@ impl ColorVisitor { impl<'de> Visitor<'de> for ColorVisitor { type Value = Color; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("A color") } diff --git a/src/display/draw_box.rs b/src/display/draw_box.rs index 3b2b4aaf4f1d..1b3958cefca8 100644 --- a/src/display/draw_box.rs +++ b/src/display/draw_box.rs @@ -140,7 +140,7 @@ impl Stylable for Line { } impl Stylable for Neighbors<Option<BoxStyle>> { - fn style(&self, style: BoxStyle) -> char { + fn style(&self, _style: BoxStyle) -> char { use BoxStyle::*; match (self.left, self.right, self.top, self.bottom) { (None, None, None, None) => BOX, diff --git a/src/display/mod.rs b/src/display/mod.rs index 2df4277f4fa7..6e37a03d8c55 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -17,17 +17,17 @@ pub fn clear<T: Write>(out: &mut T) -> io::Result<()> { pub trait Draw: Positioned { /// Draw this entity, assuming the character is already at the correct /// position - fn do_draw(&self, out: &mut Write) -> io::Result<()>; + fn do_draw(&self, out: &mut dyn Write) -> io::Result<()>; } impl<T: Draw> Draw for &T { - fn do_draw(&self, out: &mut Write) -> io::Result<()> { + fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> { (**self).do_draw(out) } } impl<T: Draw> Draw for Box<T> { - fn do_draw(&self, out: &mut Write) -> io::Result<()> { + fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> { (**self).do_draw(out) } } @@ -36,7 +36,7 @@ pub trait DrawWithNeighbors: Positioned { #[allow(clippy::borrowed_box)] fn do_draw_with_neighbors<'a, 'b>( &'a self, - out: &'b mut Write, + out: &'b mut dyn Write, neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, ) -> io::Result<()>; } @@ -44,7 +44,7 @@ pub trait DrawWithNeighbors: Positioned { impl<T: Draw> DrawWithNeighbors for T { fn do_draw_with_neighbors<'a, 'b>( &'a self, - out: &'b mut Write, + out: &'b mut dyn Write, _neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, ) -> io::Result<()> { self.do_draw(out) diff --git a/src/display/viewport.rs b/src/display/viewport.rs index 5ff56be0a909..9d17bc87dcff 100644 --- a/src/display/viewport.rs +++ b/src/display/viewport.rs @@ -68,7 +68,7 @@ impl<W> Viewport<W> { } impl<W> Debug for Viewport<W> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "Viewport {{ outer: {:?}, inner: {:?}, out: <OUT> }}", diff --git a/src/entities/character.rs b/src/entities/character.rs index 2b1b6efe47de..360478e8be1c 100644 --- a/src/entities/character.rs +++ b/src/entities/character.rs @@ -42,7 +42,7 @@ impl Character { } impl display::Draw for Character { - fn do_draw(&self, out: &mut Write) -> io::Result<()> { + fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> { write!(out, "@") } } diff --git a/src/entities/creature.rs b/src/entities/creature.rs index 87ffda161e1c..20071c1d88eb 100644 --- a/src/entities/creature.rs +++ b/src/entities/creature.rs @@ -57,7 +57,7 @@ impl Describe for Creature { } impl display::Draw for Creature { - fn do_draw(&self, out: &mut Write) -> io::Result<()> { + fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> { write!(out, "{}", self.typ.chr) } } diff --git a/src/entities/entity.rs b/src/entities/entity.rs index e43175931b68..01075d298f81 100644 --- a/src/entities/entity.rs +++ b/src/entities/entity.rs @@ -103,7 +103,7 @@ impl_downcast!(Entity); impl DrawWithNeighbors for Box<dyn Entity> { fn do_draw_with_neighbors<'a, 'b>( &'a self, - out: &'b mut Write, + out: &'b mut dyn Write, neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, ) -> io::Result<()> { (**self).do_draw_with_neighbors(out, neighbors) diff --git a/src/entities/entity_char.rs b/src/entities/entity_char.rs index 2f845820021e..88ca8a55a823 100644 --- a/src/entities/entity_char.rs +++ b/src/entities/entity_char.rs @@ -12,7 +12,7 @@ pub struct EntityChar { } impl Display for EntityChar { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!( f, "{}{}{}", diff --git a/src/entities/environment.rs b/src/entities/environment.rs index 042873ec5a12..8f8a56706287 100644 --- a/src/entities/environment.rs +++ b/src/entities/environment.rs @@ -21,7 +21,7 @@ impl Wall { impl display::DrawWithNeighbors for Wall { fn do_draw_with_neighbors<'a, 'b>( &'a self, - out: &'b mut Write, + out: &'b mut dyn Write, neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, ) -> io::Result<()> { let neighbor_styles: Neighbors<Option<BoxStyle>> = diff --git a/src/entities/item.rs b/src/entities/item.rs index 6e47a87f5b83..aa99fb42e2c1 100644 --- a/src/entities/item.rs +++ b/src/entities/item.rs @@ -44,7 +44,7 @@ impl Describe for Item { } impl display::Draw for Item { - fn do_draw(&self, out: &mut Write) -> io::Result<()> { + fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> { write!(out, "{}", self.typ.chr) } } diff --git a/src/game.rs b/src/game.rs index add48e8bfd47..2c69061516fd 100644 --- a/src/game.rs +++ b/src/game.rs @@ -69,7 +69,7 @@ impl PromptResolution { use PromptResolution::*; match self { Cancellable(complete) => complete.cancel(), - Uncancellable(complete) => {} + Uncancellable(_complete) => {} } } } @@ -200,12 +200,10 @@ impl<'a> Game<'a> { fn collision_at(&self, pos: Position) -> Option<Collision> { if !pos.within(self.viewport.inner) { Some(Collision::Stop) + } else if self.creatures_at(pos).is_empty() { + None } else { - if self.creatures_at(pos).is_empty() { - None - } else { - Some(Collision::Combat) - } + Some(Collision::Combat) } } @@ -305,7 +303,7 @@ impl<'a> Game<'a> { } /// Step the game forward the given number of ticks - fn tick(&mut self, ticks: Ticks) {} + fn tick(&mut self, _ticks: Ticks) {} /// Get a message from the global map based on the rng in this game fn message<'params>( diff --git a/src/main.rs b/src/main.rs index 2cd0bbc08d05..8004a5739ef5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,7 @@ -extern crate termion; #[macro_use] extern crate log; -extern crate config; -extern crate log4rs; -extern crate serde; -extern crate toml; #[macro_use] extern crate serde_derive; -extern crate serde_json; #[macro_use] extern crate clap; #[macro_use] @@ -19,14 +13,13 @@ extern crate lazy_static; extern crate maplit; #[macro_use] extern crate downcast_rs; -extern crate backtrace; #[macro_use] extern crate include_dir; #[macro_use] extern crate nom; +#[cfg(test)] #[macro_use] extern crate matches; -extern crate futures; #[macro_use] mod util; @@ -53,6 +46,7 @@ use backtrace::Backtrace; use std::io::{self, StdinLock, StdoutLock}; use std::panic; +use termion; use termion::raw::IntoRawMode; use termion::raw::RawTerminal; diff --git a/src/types/entity_map.rs b/src/types/entity_map.rs index bec16cdab28a..202d8b593e15 100644 --- a/src/types/entity_map.rs +++ b/src/types/entity_map.rs @@ -10,7 +10,7 @@ use alga::general::{ use std::collections::{hash_map, BTreeMap, HashMap}; use std::iter::FromIterator; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct EntityMap<A> { by_position: BTreeMap<Position, Vec<EntityID>>, by_id: HashMap<EntityID, A>, @@ -24,7 +24,7 @@ impl<A: PartialEq> PartialEq for EntityMap<A> { } impl<A: Eq> Eq for EntityMap<A> {} -const BY_POS_INVARIANT: &'static str = +const BY_POS_INVARIANT: &str = "Invariant: All references in EntityMap.by_position should point to existent references in by_id"; impl<A> EntityMap<A> { @@ -54,26 +54,26 @@ impl<A> EntityMap<A> { /// Remove all entities at the given position pub fn remove_all_at(&mut self, pos: Position) { - self.by_position.remove(&pos).map(|eids| { + if let Some(eids) = self.by_position.remove(&pos) { for eid in eids { self.by_id.remove(&eid).expect(BY_POS_INVARIANT); } - }); + } } - pub fn get<'a>(&'a self, id: EntityID) -> Option<&'a A> { + pub fn get(&self, id: EntityID) -> Option<&A> { self.by_id.get(&id) } - pub fn get_mut<'a>(&'a mut self, id: EntityID) -> Option<&'a mut A> { + pub fn get_mut(&mut self, id: EntityID) -> Option<&mut A> { self.by_id.get_mut(&id) } - pub fn entities<'a>(&'a self) -> impl Iterator<Item = &'a A> { + pub fn entities(&self) -> impl Iterator<Item = &A> { self.by_id.values() } - pub fn entities_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut A> { + pub fn entities_mut(&mut self) -> impl Iterator<Item = &mut A> { self.by_id.values_mut() } @@ -81,8 +81,8 @@ impl<A> EntityMap<A> { self.by_id.keys() } - pub fn drain<'a>(&'a mut self) -> Drain<'a, A> { - let ids = self.ids().map(|e| *e).collect::<Vec<_>>(); + pub fn drain(&mut self) -> Drain<'_, A> { + let ids = self.ids().copied().collect::<Vec<_>>(); Drain { map: self, ids_iter: Box::new(ids.into_iter()), @@ -103,7 +103,7 @@ impl<A: Positioned + Identified<EntityID>> EntityMap<A> { self.by_id.entry(entity_id).or_insert(entity); self.by_position .entry(pos) - .or_insert(Vec::new()) + .or_insert_with(Vec::new) .push(entity_id); entity_id } @@ -113,12 +113,14 @@ impl<A: Positioned + Identified<EntityID>> EntityMap<A> { self.by_id.remove(&id).map(|e| { let mut empty = false; let position = e.position(); - self.by_position.get_mut(&position).map(|es| { + + if let Some(es) = self.by_position.get_mut(&position) { es.retain(|e| *e != id); - if es.len() == 0 { + if es.is_empty() { empty = true; } - }); + } + if empty { self.by_position.remove(&position); } @@ -172,7 +174,7 @@ impl<'a, A: Positioned + Identified<EntityID>> IntoIterator type Item = (&'a EntityID, &'a A); type IntoIter = std::collections::hash_map::Iter<'a, EntityID, A>; fn into_iter(self) -> Self::IntoIter { - (&self.by_id).into_iter() + (&self.by_id).iter() } } @@ -246,20 +248,21 @@ impl<A: PositionedMut> EntityMap<A> { old_pos = Some(entity.position()); entity.set_position(new_position); } - old_pos.map(|p| { - self.by_position - .get_mut(&p) - .map(|es| es.retain(|e| *e != entity_id)); + + if let Some(p) = old_pos { + if let Some(es) = self.by_position.get_mut(&p) { + es.retain(|e| *e != entity_id); + } self.by_position .entry(new_position) - .or_insert(Vec::new()) + .or_insert_with(Vec::new) .push(entity_id); - }); + } } } -pub struct Drain<'a, A: 'a> { +pub struct Drain<'a, A> { map: &'a mut EntityMap<A>, ids_iter: Box<dyn Iterator<Item = EntityID> + 'a>, } @@ -313,9 +316,7 @@ mod tests { fn gen_entity_map() -> BoxedStrategy<EntityMap<TestEntity>> { any::<Vec<TestEntity>>() .prop_map(|ents| { - ents.iter() - .map(|e| e.clone()) - .collect::<EntityMap<TestEntity>>() + ents.iter().cloned().collect::<EntityMap<TestEntity>>() }) .boxed() } diff --git a/src/types/mod.rs b/src/types/mod.rs index 21748bac9015..31d2ecd29770 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -427,6 +427,7 @@ impl<A> Neighbors<Vec<A>> { #[cfg(test)] mod tests { + #![allow(clippy::unnecessary_operation)] use super::*; use proptest::prelude::*; diff --git a/src/util/promise.rs b/src/util/promise.rs index 63fbca1ddc18..22f1e8b47f58 100644 --- a/src/util/promise.rs +++ b/src/util/promise.rs @@ -3,9 +3,11 @@ use std::pin::Pin; use std::sync::{Arc, RwLock}; use std::task::{Context, Poll, Waker}; +type Waiter<Env, T> = Box<dyn Fn(&mut Env, &T)>; + pub struct Promise<Env, T> { inner: Arc<RwLock<Inner<T>>>, - waiters: Arc<RwLock<Vec<Box<dyn Fn(&mut Env, &T)>>>>, + waiters: Arc<RwLock<Vec<Waiter<Env, T>>>>, } pub struct Complete<T> { @@ -29,7 +31,7 @@ pub fn promise<Env, T>() -> (Complete<T>, Promise<Env, T>) { inner: inner.clone(), waiters: Arc::new(RwLock::new(Vec::new())), }; - let complete = Complete { inner: inner }; + let complete = Complete { inner }; (complete, promise) } @@ -127,7 +129,7 @@ impl<Env, P: Give<Env>> Give<Env> for &P { impl<Env, T> Future for Promise<Env, T> { type Output = Arc<T>; - fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { let mut inner = self.inner.write().unwrap(); match inner.value { Some(ref v) => Poll::Ready(v.clone()), diff --git a/src/util/template.rs b/src/util/template.rs index a3faadc31c91..bb77f9b4d610 100644 --- a/src/util/template.rs +++ b/src/util/template.rs @@ -18,7 +18,7 @@ impl<'a> Path<'a> { } impl<'a> Display for Path<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.head)?; for part in &self.tail { write!(f, ".{}", part)?; @@ -96,7 +96,7 @@ impl<'a> TemplateVisitor<'a> { impl<'a> serde::de::Visitor<'a> for TemplateVisitor<'a> { type Value = Template<'a>; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("a valid template string") } @@ -126,7 +126,7 @@ impl<'a> Template<'a> { input: &'a str, ) -> Result<Template<'a>, Err<(&'a str, ErrorKind)>> { let (remaining, res) = template(input)?; - if remaining.len() > 0 { + if !remaining.is_empty() { unreachable!(); } Ok(res) @@ -157,7 +157,7 @@ pub enum TemplateError<'a> { } impl<'a> Display for TemplateError<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use TemplateError::*; match self { MissingParam(path) => { @@ -179,7 +179,7 @@ impl<'a> TemplateParams<'a> { match self { Direct(_) => None, Nested(m) => m.get(path.head).and_then(|next| { - if path.tail.len() == 0 { + if path.tail.is_empty() { match next { Direct(s) => Some(*s), _ => None, |