Skip to main content

gc/
list.rs

1use crate::header::GcId;
2use crate::runtime::GcRuntime;
3
4/// Intrusive doubly-linked list head/tail, indexed by `GcId`.
5/// Rust equivalent of QuickJS `list.h` + embedded `list_head`.
6#[derive(Debug, Default, Clone)]
7pub struct GcList {
8    pub(crate) head: Option<GcId>,
9    pub(crate) tail: Option<GcId>,
10}
11
12impl GcList {
13    pub fn new() -> Self {
14        GcList {
15            head: None,
16            tail: None,
17        }
18    }
19
20    pub fn is_empty(&self) -> bool {
21        self.head.is_none()
22    }
23
24    pub fn head(&self) -> Option<GcId> {
25        self.head
26    }
27
28    pub fn clear(&mut self) {
29        self.head = None;
30        self.tail = None;
31    }
32}
33
34pub struct GcListIter<'a> {
35    pub(crate) rt: &'a GcRuntime,
36    pub(crate) current: Option<GcId>,
37}
38
39impl<'a> Iterator for GcListIter<'a> {
40    type Item = GcId;
41
42    fn next(&mut self) -> Option<Self::Item> {
43        let id = self.current?;
44        self.current = self.rt.header(id).list_next;
45        Some(id)
46    }
47}