From 2fac3fd8f8902ecf26a809f9d1a7b2b91091039e Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Fri, 16 Aug 2024 09:45:35 +0300 Subject: [PATCH] refactor(tasks): resolve recursive tasks without intermediate collections --- src/tasks.rs | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/tasks.rs b/src/tasks.rs index 0c903fd..15afc9d 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -298,39 +298,35 @@ impl Tasks { // Helpers - fn resolve_tasks<'a>(&self, iter: impl IntoIterator) -> Vec<&Task> { + fn resolve_tasks<'a>(&'a self, iter: impl Iterator) -> impl Iterator { self.resolve_tasks_rec(iter, self.depth) } fn resolve_tasks_rec<'a>( - &self, - iter: impl IntoIterator, + &'a self, + iter: impl Iterator, depth: i8, - ) -> Vec<&Task> { - iter.into_iter() - .filter_map(|id| self.get_by_id(&id)) - .flat_map(|task| { + ) -> Box> { + iter.filter_map(|id| self.get_by_id(&id)) + .flat_map(move |task| { let new_depth = depth - 1; - if new_depth < 0 { - let tasks = self - .resolve_tasks_rec(task.children.iter(), new_depth) - .into_iter() - .collect::>(); - if tasks.is_empty() { - vec![task] - } else { - tasks - } - } else if new_depth > 0 { - self.resolve_tasks_rec(task.children.iter(), new_depth) - .into_iter() - .chain(once(task)) - .collect() - } else { + if new_depth == 0 { vec![task] + } else { + let tasks_iter = self.resolve_tasks_rec(task.children.iter(), new_depth); + if new_depth < 0 { + let tasks: Vec<&Task> = tasks_iter.collect(); + if tasks.is_empty() { + vec![task] + } else { + tasks + } + } else { + tasks_iter.chain(once(task)).collect() + } } }) - .collect() + .into() } pub(crate) fn referenced_tasks(&mut self, event: &Event, f: F) { @@ -354,8 +350,8 @@ impl Tasks { } pub(crate) fn filtered_tasks(&self, position: Option) -> impl Iterator { - // TODO use ChildrenIterator - self.resolve_tasks(self.children_of(position)).into_iter() + // TODO use ChildIterator + self.resolve_tasks(self.children_of(position)) .filter(|t| { // TODO apply filters in transit self.state.matches(t) && @@ -375,7 +371,7 @@ impl Tasks { return self.get_current_task().into_iter().collect(); } if self.view.len() > 0 { - return self.resolve_tasks(self.view.iter()); + return self.resolve_tasks(self.view.iter()).collect(); } self.filtered_tasks(self.position).collect() }