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
//! Module defining script identifiers.

use crate::{ImmutableString, Position};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{borrow::Borrow, fmt, hash::Hash};

/// _(internals)_ An identifier containing a name and a [position][Position].
/// Exported under the `internals` feature only.
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct Ident {
    /// Identifier name.
    pub name: ImmutableString,
    /// Position.
    pub pos: Position,
}

impl fmt::Debug for Ident {
    #[cold]
    #[inline(never)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.name)?;
        self.pos.debug_print(f)
    }
}

impl Borrow<str> for Ident {
    #[inline(always)]
    #[must_use]
    fn borrow(&self) -> &str {
        self.name.as_ref()
    }
}

impl AsRef<str> for Ident {
    #[inline(always)]
    #[must_use]
    fn as_ref(&self) -> &str {
        self.name.as_ref()
    }
}

impl Ident {
    /// Get the name of the identifier as a string slice.
    #[inline(always)]
    #[must_use]
    pub fn as_str(&self) -> &str {
        self.name.as_str()
    }
    /// Is the identifier empty?
    #[inline(always)]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.name.is_empty()
    }
}