asn1_rs/
tostatic.rs

1use alloc::borrow::Cow;
2use alloc::string::ToString;
3
4/// Common trait for objects that can be transformed to a `'static` version of `self`
5pub trait ToStatic {
6    type Owned: 'static;
7    fn to_static(&self) -> Self::Owned;
8}
9
10impl ToStatic for Cow<'_, str> {
11    type Owned = Cow<'static, str>;
12    fn to_static(&self) -> <Self as ToStatic>::Owned {
13        Cow::Owned(self.to_string())
14    }
15}
16
17impl ToStatic for Cow<'_, [u8]> {
18    type Owned = Cow<'static, [u8]>;
19    fn to_static(&self) -> <Self as ToStatic>::Owned {
20        Cow::Owned(self.to_vec())
21    }
22}
23
24macro_rules! impl_tostatic_primitive {
25    ($t:ty) => {
26        impl ToStatic for $t {
27            type Owned = $t;
28            fn to_static(&self) -> <Self as ToStatic>::Owned {
29                self.clone()
30            }
31        }
32    };
33    ($t:ty => $to:ty, $closure:expr) => {
34        impl ToStatic for $t {
35            type Owned = $to;
36            fn to_static(&self) -> <Self as ToStatic>::Owned {
37                let f: &dyn Fn(&Self) -> $to = &$closure;
38                f(self)
39            }
40        }
41    };
42    (I $($types: ty)+) => {
43        $(
44            impl_tostatic_primitive!($types);
45        )*
46    };
47}
48
49impl_tostatic_primitive!(bool);
50impl_tostatic_primitive!(I i8 i16 i32 i64 i128 isize);
51impl_tostatic_primitive!(I u8 u16 u32 u64 u128 usize);
52
53impl<T> ToStatic for &'_ T
54where
55    T: ToStatic,
56{
57    type Owned = T::Owned;
58
59    fn to_static(&self) -> Self::Owned {
60        (*self).to_static()
61    }
62}
63
64impl<T> ToStatic for Option<T>
65where
66    T: ToStatic,
67{
68    type Owned = Option<T::Owned>;
69
70    fn to_static(&self) -> Self::Owned {
71        self.as_ref().map(ToStatic::to_static)
72    }
73}
74
75#[cfg(feature = "std")]
76const _: () = {
77    impl_tostatic_primitive!(I String);
78    impl_tostatic_primitive!(str => String, |s| s.to_string());
79
80    impl<T> ToStatic for Box<T>
81    where
82        T: ToStatic,
83    {
84        type Owned = Box<T::Owned>;
85
86        fn to_static(&self) -> Self::Owned {
87            Box::new(self.as_ref().to_static())
88        }
89    }
90};