From 7aa3d2c2d6c60c9d4696bf2fb91982a5be64f9b0 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 26 Jul 2021 00:29:37 +0200 Subject: No more excessive region cloning --- src/layout/mod.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/layout/mod.rs') diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 22e6e8c8..0f88d150 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -140,14 +140,21 @@ impl Regions { self.backlog.is_empty() && self.last.map_or(true, |size| self.current == size) } + /// An iterator that returns pairs of `(current, base)` that are equivalent + /// to what would be produced by calling [`next()`](Self::next) repeatedly + /// until all regions are exhausted. + pub fn iter(&self) -> impl Iterator + '_ { + let first = std::iter::once((self.current, self.base)); + let backlog = self.backlog.iter().rev(); + let last = self.last.iter().cycle(); + first.chain(backlog.chain(last).map(|&s| (s, s))) + } + /// Advance to the next region if there is any. - pub fn next(&mut self) -> bool { + pub fn next(&mut self) { if let Some(size) = self.backlog.pop().or(self.last) { self.current = size; self.base = size; - true - } else { - false } } -- cgit v1.2.3