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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::borrow::Cow;
use std::sync::Arc;

/// A wrapper for `Cow<'a, str>` that is specifically designed for use with the `t!` macro.
///
/// This wrapper provides additional functionality or optimizations when handling strings in the `t!` macro.
pub struct CowStr<'a>(Cow<'a, str>);

impl<'a> CowStr<'a> {
    pub fn as_str(&self) -> &str {
        self.0.as_ref()
    }

    pub fn into_inner(self) -> Cow<'a, str> {
        self.0
    }
}

macro_rules! impl_convert_from_numeric {
    ($typ:ty) => {
        impl<'a> From<$typ> for CowStr<'a> {
            fn from(val: $typ) -> Self {
                Self(Cow::from(format!("{}", val)))
            }
        }
    };
}

impl_convert_from_numeric!(i8);
impl_convert_from_numeric!(i16);
impl_convert_from_numeric!(i32);
impl_convert_from_numeric!(i64);
impl_convert_from_numeric!(i128);
impl_convert_from_numeric!(isize);

impl_convert_from_numeric!(u8);
impl_convert_from_numeric!(u16);
impl_convert_from_numeric!(u32);
impl_convert_from_numeric!(u64);
impl_convert_from_numeric!(u128);
impl_convert_from_numeric!(usize);

impl<'a> From<Arc<str>> for CowStr<'a> {
    #[inline]
    fn from(s: Arc<str>) -> Self {
        Self(Cow::Owned(s.to_string()))
    }
}

impl<'a> From<Box<str>> for CowStr<'a> {
    #[inline]
    fn from(s: Box<str>) -> Self {
        Self(Cow::Owned(s.to_string()))
    }
}

impl<'a> From<&'a str> for CowStr<'a> {
    #[inline]
    fn from(s: &'a str) -> Self {
        Self(Cow::Borrowed(s))
    }
}

impl<'a> From<&&'a str> for CowStr<'a> {
    #[inline]
    fn from(s: &&'a str) -> Self {
        Self(Cow::Borrowed(s))
    }
}

impl<'a> From<Arc<&'a str>> for CowStr<'a> {
    #[inline]
    fn from(s: Arc<&'a str>) -> Self {
        Self(Cow::Borrowed(*s))
    }
}

impl<'a> From<Box<&'a str>> for CowStr<'a> {
    #[inline]
    fn from(s: Box<&'a str>) -> Self {
        Self(Cow::Borrowed(*s))
    }
}

impl<'a> From<String> for CowStr<'a> {
    #[inline]
    fn from(s: String) -> Self {
        Self(Cow::from(s))
    }
}

impl<'a> From<&'a String> for CowStr<'a> {
    #[inline]
    fn from(s: &'a String) -> Self {
        Self(Cow::Borrowed(s))
    }
}

impl<'a> From<Arc<String>> for CowStr<'a> {
    #[inline]
    fn from(s: Arc<String>) -> Self {
        Self(Cow::Owned(s.to_string()))
    }
}

impl<'a> From<Box<String>> for CowStr<'a> {
    #[inline]
    fn from(s: Box<String>) -> Self {
        Self(Cow::from(*s))
    }
}