Fix layout cache on nightly

pull/3/head
Florian Dehau 8 years ago
parent 662e2dd102
commit b19c014889

@ -7,7 +7,7 @@ use cassowary::strength::{REQUIRED, WEAK};
use terminal::{Terminal, Backend}; use terminal::{Terminal, Backend};
#[derive(Debug, Hash, PartialEq, Eq)] #[derive(Debug, Hash, Clone, PartialEq, Eq)]
pub enum Direction { pub enum Direction {
Horizontal, Horizontal,
Vertical, Vertical,
@ -285,7 +285,7 @@ impl Element {
/// .sizes(&[Size::Percent(50), Size::Percent(50)]); /// .sizes(&[Size::Percent(50), Size::Percent(50)]);
/// # } /// # }
/// ``` /// ```
#[derive(Debug, PartialEq, Eq, Hash)] #[derive(Debug, PartialEq, Clone, Eq, Hash)]
pub struct Group { pub struct Group {
pub direction: Direction, pub direction: Direction,
pub margin: u16, pub margin: u16,

@ -9,7 +9,6 @@ extern crate unicode_segmentation;
extern crate unicode_width; extern crate unicode_width;
pub mod buffer; pub mod buffer;
mod util;
pub mod symbols; pub mod symbols;
pub mod terminal; pub mod terminal;
pub mod widgets; pub mod widgets;

@ -11,7 +11,6 @@ use buffer::{Buffer, Cell};
use layout::{Rect, Group, split}; use layout::{Rect, Group, split};
use widgets::Widget; use widgets::Widget;
use style::{Color, Modifier, Style}; use style::{Color, Modifier, Style};
use util::hash;
pub trait Backend { pub trait Backend {
fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error> fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error>
@ -190,7 +189,7 @@ pub struct Terminal<B>
{ {
backend: B, backend: B,
/// Cache to prevent the layout to be computed at each draw call /// Cache to prevent the layout to be computed at each draw call
layout_cache: HashMap<u64, LayoutEntry>, layout_cache: HashMap<(Group, Rect), LayoutEntry>,
/// Holds the results of the current and previous draw calls. The two are compared at the end /// 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 /// of each draw pass to output the necessary updates to the terminal
buffers: [Buffer; 2], buffers: [Buffer; 2],
@ -225,15 +224,13 @@ impl<B> Terminal<B>
/// add it to the layout cache. Moreover the function marks the queried entries so that we can /// 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. /// clean outdated ones at the end of the draw call.
pub fn compute_layout(&mut self, group: &Group, area: &Rect) -> Vec<Rect> { pub fn compute_layout(&mut self, group: &Group, area: &Rect) -> Vec<Rect> {
let hash = hash(group, area);
let entry = self.layout_cache let entry = self.layout_cache
.entry(hash) .entry((group.clone(), area.clone()))
.or_insert_with(|| { .or_insert_with(|| {
let chunks = split(area, &group.direction, group.margin, &group.sizes); 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, group,
chunks, chunks);
hash);
LayoutEntry { LayoutEntry {
chunks: chunks, chunks: chunks,
hot: true, hot: true,
@ -288,13 +285,13 @@ impl<B> Terminal<B>
try!(self.flush()); try!(self.flush());
// Clean layout cache // Clean layout cache
let to_remove = self.layout_cache let hot = self.layout_cache
.iter() .drain()
.filter_map(|(h, e)| if !e.hot { Some(*h) } else { None }) .filter(|&(_, ref v)| v.hot == true)
.collect::<Vec<u64>>(); .collect::<Vec<((Group, Rect), LayoutEntry)>>();
for h in to_remove { for (key, value) in hot {
self.layout_cache.remove(&h); self.layout_cache.insert(key, value);
} }
for (_, e) in &mut self.layout_cache { for (_, e) in &mut self.layout_cache {

Loading…
Cancel
Save