diff --git a/src/layout.rs b/src/layout.rs index 54a75ca..b28db48 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -7,7 +7,7 @@ use cassowary::strength::{REQUIRED, WEAK}; use terminal::{Terminal, Backend}; -#[derive(Debug, Hash, PartialEq, Eq)] +#[derive(Debug, Hash, Clone, PartialEq, Eq)] pub enum Direction { Horizontal, Vertical, @@ -285,7 +285,7 @@ impl Element { /// .sizes(&[Size::Percent(50), Size::Percent(50)]); /// # } /// ``` -#[derive(Debug, PartialEq, Eq, Hash)] +#[derive(Debug, PartialEq, Clone, Eq, Hash)] pub struct Group { pub direction: Direction, pub margin: u16, diff --git a/src/lib.rs b/src/lib.rs index 17e0291..9f4fc71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,6 @@ extern crate unicode_segmentation; extern crate unicode_width; pub mod buffer; -mod util; pub mod symbols; pub mod terminal; pub mod widgets; diff --git a/src/terminal.rs b/src/terminal.rs index dc5fa5f..056912b 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -11,7 +11,6 @@ use buffer::{Buffer, Cell}; use layout::{Rect, Group, split}; use widgets::Widget; use style::{Color, Modifier, Style}; -use util::hash; pub trait Backend { fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error> @@ -190,7 +189,7 @@ pub struct Terminal { backend: B, /// Cache to prevent the layout to be computed at each draw call - layout_cache: HashMap, + layout_cache: HashMap<(Group, Rect), LayoutEntry>, /// Holds the results of the current and previous draw calls. The two are compared at the end /// of each draw pass to output the necessary updates to the terminal buffers: [Buffer; 2], @@ -225,15 +224,13 @@ impl Terminal /// add it to the layout cache. Moreover the function marks the queried entries so that we can /// clean outdated ones at the end of the draw call. pub fn compute_layout(&mut self, group: &Group, area: &Rect) -> Vec { - let hash = hash(group, area); let entry = self.layout_cache - .entry(hash) + .entry((group.clone(), area.clone())) .or_insert_with(|| { let chunks = split(area, &group.direction, group.margin, &group.sizes); - debug!("New layout computed:\n* Group = {:?}\n* Chunks = {:?}\n* Hash = {}", + debug!("New layout computed:\n* Group = {:?}\n* Chunks = {:?}", group, - chunks, - hash); + chunks); LayoutEntry { chunks: chunks, hot: true, @@ -288,13 +285,13 @@ impl Terminal try!(self.flush()); // Clean layout cache - let to_remove = self.layout_cache - .iter() - .filter_map(|(h, e)| if !e.hot { Some(*h) } else { None }) - .collect::>(); + let hot = self.layout_cache + .drain() + .filter(|&(_, ref v)| v.hot == true) + .collect::>(); - for h in to_remove { - self.layout_cache.remove(&h); + for (key, value) in hot { + self.layout_cache.insert(key, value); } for (_, e) in &mut self.layout_cache {