Iterate over rows without generating a temporary Vec

macos-click-through
Jon Valdés 5 years ago
parent 9450ea62d8
commit ddad13be2d

@ -78,11 +78,10 @@ impl CharacterGrid {
self.dirty.resize_with((self.width * self.height) as usize, || value); 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<Item=&'a[GridCell]> {
(0..self.height) (0..self.height)
.map(|row| { .map(move |row| {
&self.characters[(row * self.width) as usize..((row + 1) * self.width) as usize] &self.characters[(row * self.width) as usize..((row + 1) * self.width) as usize]
}) })
.collect()
} }
} }

@ -93,7 +93,7 @@ impl Editor {
pub fn build_draw_commands(&mut self) -> (Vec<DrawCommand>, bool) { pub fn build_draw_commands(&mut self) -> (Vec<DrawCommand>, bool) {
let mut draw_commands = Vec::new(); 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; let mut command = None;
fn add_command(commands_list: &mut Vec<DrawCommand>, command: Option<DrawCommand>) { fn add_command(commands_list: &mut Vec<DrawCommand>, command: Option<DrawCommand>) {

Loading…
Cancel
Save