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

/// Abstract the lifetime of a `Display`.
#[allow(missing_debug_implementations)]
pub enum DisplayCow<'a> {
    /// A boxed `Display`
    Owned(Box<dyn fmt::Display + 'a>),
    /// A borrowed `Display`
    Borrowed(&'a dyn fmt::Display),
}

impl<'a> DisplayCow<'a> {
    fn as_ref(&self) -> &dyn fmt::Display {
        match self {
            DisplayCow::Owned(o) => o.as_ref(),
            DisplayCow::Borrowed(b) => b,
        }
    }
}

impl<'a> fmt::Display for DisplayCow<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}

pub(crate) struct StrDisplay {
    pub(crate) s: &'static str,
}

impl fmt::Display for StrDisplay {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.s)
    }
}