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
58
59
60
61
62
63
64
65
66
67
68
use core::fmt;
use core::num::NonZeroU32;

use crate as rune;
use crate::alloc::prelude::*;

/// A non-zero [Id] which definitely contains a value. We keep this distinct
/// from `Id` to allow for safely using this as a key in a hashmap, preventing
/// us from inadvertently storing an empty identifier.
#[derive(Debug, TryClone, Clone, Copy, PartialEq, Eq, Hash)]
#[try_clone(copy)]
#[repr(transparent)]
pub struct NonZeroId(#[try_clone(copy)] NonZeroU32);

impl fmt::Display for NonZeroId {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl From<NonZeroU32> for NonZeroId {
    fn from(value: NonZeroU32) -> Self {
        Self(value)
    }
}

/// An opaque identifier that is associated with AST types.
///
/// The default implementation for an identifier is empty, meaning it does not
/// hold any value. Attempting to perform lookups over it will fail with an
/// error indicating that it's empty with the formatted string `Id(*)`.
#[derive(TryClone, Clone, Copy, Default, PartialEq, Eq)]
#[try_clone(copy)]
#[repr(transparent)]
pub struct Id(Option<NonZeroId>);

impl Id {
    /// Construct a new identifier with a value.
    pub(crate) const fn new(value: NonZeroId) -> Self {
        Self(Some(value))
    }

    /// Test if the identifier is set.
    pub(crate) fn is_set(&self) -> bool {
        self.0.is_some()
    }

    /// Set the value of an identifier.
    pub(crate) fn set(&mut self, value: NonZeroId) {
        debug_assert!(self.0.is_none(), "id should not be set multiple times");
        self.0 = Some(value);
    }

    /// Get the underlying identifier.
    pub(crate) fn get(&self) -> Option<NonZeroId> {
        self.0
    }
}

impl fmt::Debug for Id {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            Some(n) => write!(f, "Id({})", n.0.get()),
            None => write!(f, "Id(*)"),
        }
    }
}