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, AA, BB, CC, A: SInto<S, AA>, B: SInto<S, BB>, C: SInto<S, CC>> SInto<S, (AA, BB, CC)>
53    for (A, B, C)
54{
55    fn sinto(&self, s: &S) -> (AA, BB, CC) {
56        (self.0.sinto(s), self.1.sinto(s), self.2.sinto(s))
57    }
58}
59
60impl<S, D, T: SInto<S, D>> SInto<S, Option<D>> for Option<T> {
61    fn sinto(&self, s: &S) -> Option<D> {
62        self.as_ref().map(|x| x.sinto(s))
63    }
64}
65impl<S, D, T: SInto<S, D>> SInto<S, D> for Box<T> {
66    fn sinto(&self, s: &S) -> D {
67        (**self).sinto(s)
68    }
69}
70impl<S, D, T: SInto<S, D>> SInto<S, D> for &T {
71    fn sinto(&self, s: &S) -> D {
72        (**self).sinto(s)
73    }
74}
75impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for [T] {
76    fn sinto(&self, s: &S) -> Vec<D> {
77        self.iter().map(|x| x.sinto(s)).collect()
78    }
79}
80impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for Box<[T]> {
81    fn sinto(&self, s: &S) -> Vec<D> {
82        self.into_iter().map(|x| x.sinto(s)).collect()
83    }
84}
85
86impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for Vec<T> {
87    fn sinto(&self, s: &S) -> Vec<D> {
88        self.iter().map(|x| x.sinto(s)).collect()
89    }
90}
91
92macro_rules! sinto_clone {
93    ($t:ty) => {
94        impl<S> SInto<S, $t> for $t {
95            fn sinto(&self, _: &S) -> $t {
96                self.clone()
97            }
98        }
99    };
100    ($t:ty, $($rest:tt)*) => {
101        sinto_clone!($t);
102        sinto_clone!($($rest)+);
103    };
104    () => {};
105}
106
107sinto_clone!(bool, String, char);
108sinto_clone!(u8, u16, u32, u64, u128, usize);
109sinto_clone!(i8, i16, i32, i64, i128, isize);