quickphf_codegen/
const_instantiable.rs

1use core::fmt;
2
3/// Provides a way to generate code which instantiates values of the
4/// implementing type in a `const` context.
5pub trait ConstInstantiable {
6    /// Print a `const` expression that can be used to instantiate this value.
7    #[allow(clippy::missing_errors_doc)]
8    fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
9}
10
11/// Provides blanket implementation of [`ConstInstantiable`] which defers to
12/// [`Debug`](core::fmt::Debug) representation of type.
13pub trait DebugInstantiable: std::fmt::Debug {}
14
15impl<T: DebugInstantiable> ConstInstantiable for T {
16    fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        write!(f, "{:?}", self)
18    }
19}
20
21impl DebugInstantiable for () {}
22impl DebugInstantiable for bool {}
23impl DebugInstantiable for &str {}
24impl DebugInstantiable for char {}
25impl DebugInstantiable for i8 {}
26impl DebugInstantiable for i16 {}
27impl DebugInstantiable for i32 {}
28impl DebugInstantiable for i64 {}
29impl DebugInstantiable for i128 {}
30impl DebugInstantiable for isize {}
31impl DebugInstantiable for u8 {}
32impl DebugInstantiable for u16 {}
33impl DebugInstantiable for u32 {}
34impl DebugInstantiable for u64 {}
35impl DebugInstantiable for u128 {}
36impl DebugInstantiable for usize {}
37
38impl<T1, T2> ConstInstantiable for (T1, T2)
39where
40    T1: ConstInstantiable,
41    T2: ConstInstantiable,
42{
43    fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "(")?;
45        self.0.fmt_const_new(f)?;
46        write!(f, ", ")?;
47        self.1.fmt_const_new(f)?;
48        write!(f, ")")
49    }
50}
51
52impl<T1, T2, T3> ConstInstantiable for (T1, T2, T3)
53where
54    T1: ConstInstantiable,
55    T2: ConstInstantiable,
56    T3: ConstInstantiable,
57{
58    fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        write!(f, "(")?;
60        self.0.fmt_const_new(f)?;
61        write!(f, ", ")?;
62        self.1.fmt_const_new(f)?;
63        write!(f, ", ")?;
64        self.2.fmt_const_new(f)?;
65        write!(f, ")")
66    }
67}
68
69impl<T1, T2, T3, T4> ConstInstantiable for (T1, T2, T3, T4)
70where
71    T1: ConstInstantiable,
72    T2: ConstInstantiable,
73    T3: ConstInstantiable,
74    T4: ConstInstantiable,
75{
76    fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(f, "(")?;
78        self.0.fmt_const_new(f)?;
79        write!(f, ", ")?;
80        self.1.fmt_const_new(f)?;
81        write!(f, ", ")?;
82        self.2.fmt_const_new(f)?;
83        write!(f, ", ")?;
84        self.3.fmt_const_new(f)?;
85        write!(f, ")")
86    }
87}
88
89impl<T: ConstInstantiable> ConstInstantiable for Option<T> {
90    fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        match self {
92            Some(x) => {
93                write!(f, "Some(")?;
94                x.fmt_const_new(f)?;
95                write!(f, ")")
96            }
97            None => write!(f, "None"),
98        }
99    }
100}
101
102impl<T1, T2> ConstInstantiable for Result<T1, T2>
103where
104    T1: ConstInstantiable,
105    T2: ConstInstantiable,
106{
107    fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        match self {
109            Ok(x) => {
110                write!(f, "Ok(")?;
111                x.fmt_const_new(f)?;
112                write!(f, ")")
113            }
114            Err(e) => {
115                write!(f, "Err(")?;
116                e.fmt_const_new(f)?;
117                write!(f, ")")
118            }
119        }
120    }
121}