scsys_traits/
symbolic.rs

1/*
2    Appellation: symbolic <module>
3    Contrib: @FL03
4*/
5/// [`Displayable`] simply combines the two major traits [`core::fmt::Debug`] and
6/// [`core::fmt::Display`] into a single trait. This is useful for types that need to be
7/// displayed in a human-readable format, such as in debugging or logging.
8///
9/// **Note:** This trait is sealed, meaning it cannot be implemented outside of this crate,
10/// however, it is automatically implemented for any type that implements both
11/// [`core::fmt::Debug`] & [`core::fmt::Display`].
12pub trait Displayable: core::fmt::Debug + core::fmt::Display {
13    private!();
14}
15
16/// [`Symbolic`] denotes a type consisting of some single or set of value(s) that can are
17/// considered displayable. Essentially, this trait is a wrapper around the [`Displayable`]
18/// trait enabling additional implementations
19pub trait Symbolic {
20    type Item: Displayable;
21}
22
23macro_rules! impl_symbolic {
24    (<$tag:ident>: [$($t:ty),* $(,)?]) => {
25        $(
26            impl_symbolic!(@impl <$tag>: $t );
27        )*
28    };
29    (@impl <Self>: $t:ty ) => {
30        impl Symbolic for $t {
31            type Item = Self;
32        }
33    };
34    (@impl <$T:ident>: $t:ty ) => {
35        impl<$T> Symbolic for $t
36        where
37            $T: Displayable,
38        {
39            type Item = $T;
40        }
41    };
42}
43
44/*
45 ************* Implementations *************
46*/
47
48impl<T> Displayable for T
49where
50    T: core::fmt::Debug + core::fmt::Display,
51{
52    seal!();
53}
54
55impl Symbolic for &str {
56    type Item = Self;
57}
58
59impl_symbolic! {
60    <Self>: [u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64, char, bool]
61}
62
63#[cfg(feature = "alloc")]
64impl_symbolic! {
65    <Self>: [
66        alloc::string::String,
67    ]
68}
69
70#[cfg(feature = "alloc")]
71impl_symbolic! {
72    <T>: [
73        alloc::boxed::Box<T>,
74        alloc::rc::Rc<T>,
75        alloc::sync::Arc<T>,
76        alloc::vec::Vec<T>,
77    ]
78}