1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// @TODO: Make an iterator over ListEntry.

/// Link of Doubly-linked list.
#[derive(Debug)]
#[repr(C)]
pub struct ListEntry {
    /// Next
    pub flink: *mut ListEntry,
    /// Previous
    pub blink: *mut ListEntry,
}

impl Clone for ListEntry {
    fn clone(&self) -> Self {
        Self {
            flink: self.flink,
            blink: self.blink,
        }
    }
}

impl Copy for ListEntry {}

/// Resolves next link in doubly-linked list.
#[macro_export]
macro_rules! containing_record {
    ($next:expr, $type:ty, $field:tt) => {
        $next
            .flink
            .cast::<u8>()
            .sub($crate::offset_of!($type, $field))
            .cast::<$type>()
    };
}

/// Creates an iterator over doubly-linked list.
#[macro_export]
macro_rules! list_iter {
    ($head:expr, $type:ty, $field:tt) => {{
        let __first = $crate::containing_record!($head, $type, $field);
        let mut __next = __first;
        let mut __started = false;
        core::iter::from_fn(move || {
            if __first == __next && __started {
                None
            } else {
                __started = true;

                let val = __next.read();
                __next = $crate::containing_record!((*__next).$field, $type, $field);
                Some(val)
            }
        })
    }};
}