rune/compile/v1/
display_named.rs

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
use core::fmt;

/// Used to ergonomically display an address.
pub(super) struct DisplayNamed<T> {
    value: T,
    name: Option<&'static str>,
}

impl<T> DisplayNamed<T> {
    #[inline]
    pub(super) const fn new(value: T, name: Option<&'static str>) -> Self {
        Self { value, name }
    }
}

impl<T> fmt::Display for DisplayNamed<T>
where
    T: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.name {
            Some(name) => write!(f, "{} ({})", self.value, name),
            None => self.value.fmt(f),
        }
    }
}