Skip to main content

uqa_sql/plan/scalar/owned/
iteration.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use super::OwnedScalarExpr;
8
9/// Moves immediate scalar children in copy order without copying their payloads or allocating a work queue. Unvisited children and the original parent containers remain owned until exhaustion or iterator destruction.
10pub struct OwnedScalarChildren {
11    parent: Option<OwnedScalarExpr>,
12    next: usize,
13    end: usize,
14}
15
16impl OwnedScalarExpr {
17    /// Consume the parent while moving its children. Selecting the final child immediately destroys the now-empty parent; abandoning the iterator destroys the unvisited children as well.
18    pub fn into_children(self) -> OwnedScalarChildren {
19        let end = self.child_count();
20        OwnedScalarChildren {
21            parent: (end != 0).then_some(self),
22            next: 0,
23            end,
24        }
25    }
26}
27
28impl Iterator for OwnedScalarChildren {
29    type Item = OwnedScalarExpr;
30
31    fn next(&mut self) -> Option<Self::Item> {
32        let parent = self.parent.as_mut()?;
33        let child = parent.take_child(self.next);
34        self.next += 1;
35        if self.next == self.end {
36            self.parent = None;
37        }
38        Some(child)
39    }
40
41    fn size_hint(&self) -> (usize, Option<usize>) {
42        let remaining = self.end - self.next;
43        (remaining, Some(remaining))
44    }
45}
46
47impl ExactSizeIterator for OwnedScalarChildren {}
48impl std::iter::FusedIterator for OwnedScalarChildren {}