From ddad13be2d5d65edec190afe31f836826c521a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Vald=C3=A9s?= Date: Mon, 3 Feb 2020 20:09:24 +0100 Subject: [PATCH] Iterate over rows without generating a temporary Vec --- src/editor/grid.rs | 5 ++--- src/editor/mod.rs | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/editor/grid.rs b/src/editor/grid.rs index 5ae67c8..9a71443 100644 --- a/src/editor/grid.rs +++ b/src/editor/grid.rs @@ -78,11 +78,10 @@ impl CharacterGrid { self.dirty.resize_with((self.width * self.height) as usize, || value); } - pub fn rows<'a>(&'a self) -> Vec<&'a [GridCell]> { + pub fn rows<'a>(&'a self) -> impl Iterator { (0..self.height) - .map(|row| { + .map(move |row| { &self.characters[(row * self.width) as usize..((row + 1) * self.width) as usize] }) - .collect() } } diff --git a/src/editor/mod.rs b/src/editor/mod.rs index a53d54c..c7310cc 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -93,7 +93,7 @@ impl Editor { pub fn build_draw_commands(&mut self) -> (Vec, bool) { let mut draw_commands = Vec::new(); - for (row_index, row) in self.grid.rows().iter().enumerate() { + for (row_index, row) in self.grid.rows().enumerate() { let mut command = None; fn add_command(commands_list: &mut Vec, command: Option) {