hax_frontend_exporter/
sinto.rs

1#[cfg(not(feature = "rustc"))]
2pub trait SInto<S, To> {
3    fn sinto(&self, s: &S) -> To;
4}
5
6#[cfg(feature = "rustc")]
7pub trait SInto<S, To>: std::marker::PointeeSized {
8    fn sinto(&self, s: &S) -> To;
9}
10
11#[macro_export]
12macro_rules! sinto_todo {
13    ($($mod:ident)::+, $type:ident$(<$($lts:lifetime),*$(,)?>)? as $renamed:ident) => {
14        #[derive_group(Serializers)]
15        #[derive(Clone, Debug, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord)]
16        pub enum $renamed {
17            $type {
18                todo: String
19            },
20        }
21        #[cfg(feature = "rustc")]
22        impl<$($($lts,)*)? S> SInto<S, $renamed> for $($mod)::+::$type$(<$($lts,)*>)? {
23            fn sinto(&self, _: &S) -> $renamed {
24                $renamed::$type{todo: format!("{:?}", self)}
25            }
26        }
27    };
28    ($($mod:ident)::+, $type:ident$(<$($lts:lifetime),*$(,)?>)?) => {
29        sinto_todo!($($mod)::+, $type$(<$($lts),*>)? as $type);
30    }
31}
32
33#[macro_export]
34macro_rules! sinto_as_usize {
35    ($($mod:ident)::+, $type:ident$(<$($lts:lifetime),*$(,)?>)?) => {
36        pub type $type = usize;
37        #[cfg(feature = "rustc")]
38        impl<$($($lts,)*)? S> SInto<S, $type> for $($mod)::+::$type$(<$($lts,)*>)? {
39            fn sinto(&self, _: &S) -> $type {
40                self.as_usize()
41            }
42        }
43    }
44}
45
46impl<S, LL, RR, L: SInto<S, LL>, R: SInto<S, RR>> SInto<S, (LL, RR)> for (L, R) {
47    fn sinto(&self, s: &S) -> (LL, RR) {
48        (self.0.sinto(s), self.1.sinto(s))
49    }
50}
51
52impl<S, D, T: SInto<S, D>> SInto<S, Option<D>> for Option<T> {
53    fn sinto(&self, s: &S) -> Option<D> {
54        self.as_ref().map(|x| x.sinto(s))
55    }
56}
57impl<S, D, T: SInto<S, D>> SInto<S, D> for Box<T> {
58    fn sinto(&self, s: &S) -> D {
59        (**self).sinto(s)
60    }
61}
62impl<S, D, T: SInto<S, D>> SInto<S, D> for &T {
63    fn sinto(&self, s: &S) -> D {
64        (**self).sinto(s)
65    }
66}
67impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for [T] {
68    fn sinto(&self, s: &S) -> Vec<D> {
69        self.iter().map(|x| x.sinto(s)).collect()
70    }
71}
72impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for Box<[T]> {
73    fn sinto(&self, s: &S) -> Vec<D> {
74        self.into_iter().map(|x| x.sinto(s)).collect()
75    }
76}
77
78impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for Vec<T> {
79    fn sinto(&self, s: &S) -> Vec<D> {
80        self.iter().map(|x| x.sinto(s)).collect()
81    }
82}
83
84macro_rules! sinto_clone {
85    ($t:ty) => {
86        impl<S> SInto<S, $t> for $t {
87            fn sinto(&self, _: &S) -> $t {
88                self.clone()
89            }
90        }
91    };
92    ($t:ty, $($rest:tt)*) => {
93        sinto_clone!($t);
94        sinto_clone!($($rest)+);
95    };
96    () => {};
97}
98
99sinto_clone!(bool, String, char);
100sinto_clone!(u8, u16, u32, u64, u128, usize);
101sinto_clone!(i8, i16, i32, i64, i128, isize);