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
56
57
#![forbid(
    trivial_casts,
    unconditional_recursion,
    unsafe_code,
    unused_must_use,
    clippy::as_conversions,
    clippy::cast_ptr_alignment
)]
#![deny(unused_variables)]
#![no_std]

use core::{cmp, fmt};

pub struct VarStack<'s, S, V> {
    pub parent: Option<&'s VarStack<'s, S, V>>,
    pub name: S,
    pub value: V,
}

impl<S: Copy + fmt::Debug, V: fmt::Debug> fmt::Debug for VarStack<'_, S, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map().entries(self.iter()).finish()
    }
}

impl<'s, S, V> VarStack<'s, S, V> {
    pub fn find(&self, name: &S) -> Option<&V>
    where
        S: cmp::PartialEq,
    {
        let mut this = self;
        while &this.name != name {
            this = *this.parent.as_ref()?;
        }
        Some(&this.value)
    }
    pub fn iter(&'s self) -> Iter<'s, S, V>
    where
        S: Copy,
    {
        Iter { inner: Some(self) }
    }
}

pub struct Iter<'s, S, V> {
    inner: Option<&'s VarStack<'s, S, V>>,
}

impl<'s, S: Copy, V> Iterator for Iter<'s, S, V> {
    type Item = (S, &'s V);

    fn next(&mut self) -> Option<Self::Item> {
        let inner = self.inner.take()?;
        self.inner = inner.parent;
        Some((inner.name, &inner.value))
    }
}