From 0fd6a6830a8a7bc58c4257bc9f69057eb1e2eb86 Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Thu, 6 Dec 2018 09:54:45 -0800 Subject: [PATCH] Remove most old NLL workarounds Will need full NLL to handle a couple of remaining overlapping mutable borrows. --- font/src/ft/mod.rs | 4 +- src/event.rs | 3 +- src/grid/storage.rs | 3 +- src/index.rs | 18 +++------ src/meter.rs | 4 +- src/renderer/mod.rs | 6 +-- src/term/mod.rs | 98 ++++++++++++++++++++------------------------- 7 files changed, 56 insertions(+), 80 deletions(-) diff --git a/font/src/ft/mod.rs b/font/src/ft/mod.rs index c67665ab..1d3f64bd 100644 --- a/font/src/ft/mod.rs +++ b/font/src/ft/mod.rs @@ -270,9 +270,7 @@ impl FreeTypeRasterizer { fn face_for_glyph(&mut self, glyph_key: GlyphKey, have_recursed: bool) -> Result { let c = glyph_key.c; - let use_initial_face = if self.faces.contains_key(&glyph_key.font_key) { - // Get face and unwrap since we just checked for presence. - let face = &self.faces[&glyph_key.font_key]; + let use_initial_face = if let Some(face) = self.faces.get(&glyph_key.font_key) { let index = face.ft_face.get_char_index(c as usize); index != 0 || have_recursed diff --git a/src/event.rs b/src/event.rs index c4da86a5..b0b6b333 100644 --- a/src/event.rs +++ b/src/event.rs @@ -20,6 +20,7 @@ use crate::input::{self, MouseBinding, KeyBinding}; use crate::selection::Selection; use crate::sync::FairMutex; use crate::term::{Term, SizeInfo, TermMode, Search}; +use crate::term::cell::Cell; use crate::util::limit; use crate::util::fmt::Red; use crate::window::Window; @@ -336,7 +337,7 @@ impl Processor { if ref_test { // dump grid state let mut grid = processor.ctx.terminal.grid().clone(); - grid.initialize_all(&::term::cell::Cell::default()); + grid.initialize_all(&Cell::default()); grid.truncate(); let serialized_grid = json::to_string(&grid) diff --git a/src/grid/storage.rs b/src/grid/storage.rs index 7301f560..6a8253e3 100644 --- a/src/grid/storage.rs +++ b/src/grid/storage.rs @@ -277,8 +277,7 @@ impl Index for Storage { type Output = Row; #[inline] fn index(&self, index: usize) -> &Self::Output { - let index = self.compute_index(index); // borrowck - &self.inner[index] + &self.inner[self.compute_index(index)] } } diff --git a/src/index.rs b/src/index.rs index 4cb25527..8f998bf0 100644 --- a/src/index.rs +++ b/src/index.rs @@ -257,12 +257,8 @@ macro_rules! inclusive { fn next(&mut self) -> Option<$ty> { use crate::index::RangeInclusive::*; - // this function has a sort of odd structure due to borrowck issues - // we may need to replace self.range, so borrows of start and end need to end early - - let at_end; match *self { - Empty { .. } => return None, // empty iterators yield no values + Empty { .. } => None, // empty iterators yield no values NonEmpty { ref mut start, ref mut end } => { @@ -270,15 +266,13 @@ macro_rules! inclusive { if start <= end { let old = *start; *start = old + 1; - return Some(old); + Some(old) + } else { + *self = Empty { at: *end }; + None } - at_end = *end; } - }; - - // got this far; the range is empty, replace it - *self = Empty { at: at_end }; - None + } } #[inline] diff --git a/src/meter.rs b/src/meter.rs index 1b6003db..952dedf8 100644 --- a/src/meter.rs +++ b/src/meter.rs @@ -75,9 +75,7 @@ impl<'a> Sampler<'a> { impl<'a> Drop for Sampler<'a> { fn drop(&mut self) { - // Work around borrowck - let duration = self.alive_duration(); - self.meter.add_sample(duration); + self.meter.add_sample(self.alive_duration()); } } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index dfea3eae..85de1fd6 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -897,10 +897,8 @@ impl<'a> RenderApi<'a> { }; // Add cell to batch - { - let glyph = glyph_cache.get(glyph_key, self); - self.add_render_item(&cell, glyph); - } + let glyph = glyph_cache.get(glyph_key, self); // borrowck multiple mutable borrows + self.add_render_item(&cell, glyph); // Render zero-width characters for c in (&chars[1..]).iter().filter(|c| **c != ' ') { diff --git a/src/term/mod.rs b/src/term/mod.rs index 3d212c28..fe6724c9 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -1366,53 +1366,47 @@ impl ansi::Handler for Term { self.input_needs_wrap = false; } - { - // Number of cells the char will occupy - if let Some(width) = c.width() { - // Sigh, borrowck making us check the width twice. Hopefully the - // optimizer can fix it. - let num_cols = self.grid.num_cols(); - { - // If in insert mode, first shift cells to the right. - if self.mode.contains(mode::TermMode::INSERT) - && self.cursor.point.col + width < num_cols - { - let line = self.cursor.point.line; // borrowck - let col = self.cursor.point.col; - let line = &mut self.grid[line]; + // Number of cells the char will occupy + if let Some(width) = c.width() { + let num_cols = self.grid.num_cols(); - let src = line[col..].as_ptr(); - let dst = line[(col + width)..].as_mut_ptr(); - unsafe { - // memmove - ptr::copy(src, dst, (num_cols - col - width).0); - } - } - if width == 0 { - let mut col = self.cursor.point.col.0.saturating_sub(1); - let line = self.cursor.point.line; - if self.grid[line][Column(col)] - .flags - .contains(cell::Flags::WIDE_CHAR_SPACER) - { - col.saturating_sub(1); - } - self.grid[line][Column(col)].push_extra(c); - return; - } + // If in insert mode, first shift cells to the right. + if self.mode.contains(mode::TermMode::INSERT) + && self.cursor.point.col + width < num_cols + { + let line = self.cursor.point.line; + let col = self.cursor.point.col; + let line = &mut self.grid[line]; - let cell = &mut self.grid[&self.cursor.point]; - *cell = self.cursor.template; - cell.c = self.cursor.charsets[self.active_charset].map(c); - - // Handle wide chars - if width == 2 { - cell.flags.insert(cell::Flags::WIDE_CHAR); - } + let src = line[col..].as_ptr(); + let dst = line[(col + width)..].as_mut_ptr(); + unsafe { + // memmove + ptr::copy(src, dst, (num_cols - col - width).0); } + } - // Set spacer cell for wide chars. - if width == 2 && self.cursor.point.col + 1 < num_cols { + // Handle zero-width characters + if width == 0 { + let col = self.cursor.point.col.0.saturating_sub(1); + let line = self.cursor.point.line; + if self.grid[line][Column(col)].flags.contains(cell::Flags::WIDE_CHAR_SPACER) + { + col.saturating_sub(1); + } + self.grid[line][Column(col)].push_extra(c); + return; + } + + let cell = &mut self.grid[&self.cursor.point]; + *cell = self.cursor.template; + cell.c = self.cursor.charsets[self.active_charset].map(c); + + // Handle wide chars + if width == 2 { + cell.flags.insert(cell::Flags::WIDE_CHAR); + + if self.cursor.point.col + 1 < num_cols { self.cursor.point.col += 1; let spacer = &mut self.grid[&self.cursor.point]; *spacer = self.cursor.template; @@ -1455,15 +1449,13 @@ impl ansi::Handler for Term { #[inline] fn goto_line(&mut self, line: Line) { trace!("goto_line: {}", line); - let col = self.cursor.point.col; // borrowck - self.goto(line, col) + self.goto(line, self.cursor.point.col) } #[inline] fn goto_col(&mut self, col: Column) { trace!("goto_col: {}", col); - let line = self.cursor.point.line; // borrowck - self.goto(line, col) + self.goto(self.cursor.point.line, col) } #[inline] @@ -1476,8 +1468,7 @@ impl ansi::Handler for Term { let destination = self.cursor.point.col + count; let num_cells = (self.size_info.cols() - destination).0; - let line = self.cursor.point.line; // borrowck - let line = &mut self.grid[line]; + let line = &mut self.grid[self.cursor.point.line]; unsafe { let src = line[source..].as_ptr(); @@ -1498,16 +1489,14 @@ impl ansi::Handler for Term { fn move_up(&mut self, lines: Line) { trace!("move_up: {}", lines); let move_to = Line(self.cursor.point.line.0.saturating_sub(lines.0)); - let col = self.cursor.point.col; // borrowck - self.goto(move_to, col) + self.goto(move_to, self.cursor.point.col) } #[inline] fn move_down(&mut self, lines: Line) { trace!("move_down: {}", lines); let move_to = self.cursor.point.line + lines; - let col = self.cursor.point.col; // borrowck - self.goto(move_to, col) + self.goto(move_to, self.cursor.point.col) } #[inline] @@ -1715,8 +1704,7 @@ impl ansi::Handler for Term { let end = min(start + count, self.grid.num_cols() - 1); let n = (self.size_info.cols() - end).0; - let line = self.cursor.point.line; // borrowck - let line = &mut self.grid[line]; + let line = &mut self.grid[self.cursor.point.line]; unsafe { let src = line[end..].as_ptr();