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
112
113
114
115
116
117
use crate::prelude::{derive_group, JsonSchema};

pub trait SInto<S, To> {
    fn sinto(&self, s: &S) -> To;
}

#[macro_export]
macro_rules! sinto_todo {
    ($($mod:ident)::+, $type:ident$(<$($lts:lifetime),*$(,)?>)? as $renamed:ident) => {
        #[derive_group(Serializers)]
        #[derive(Clone, Debug, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord)]
        pub enum $renamed {
            $type {
                todo: String
            },
        }
        #[cfg(feature = "rustc")]
        impl<$($($lts,)*)? S> SInto<S, $renamed> for $($mod)::+::$type$(<$($lts,)*>)? {
            fn sinto(&self, _: &S) -> $renamed {
                $renamed::$type{todo: format!("{:?}", self)}
            }
        }
    };
    ($($mod:ident)::+, $type:ident$(<$($lts:lifetime),*$(,)?>)?) => {
        sinto_todo!($($mod)::+, $type$(<$($lts),*>)? as $type);
    }
}

#[macro_export]
macro_rules! sinto_as_usize {
    ($($mod:ident)::+, $type:ident$(<$($lts:lifetime),*$(,)?>)?) => {
        pub type $type = usize;
        #[cfg(feature = "rustc")]
        impl<$($($lts,)*)? S> SInto<S, $type> for $($mod)::+::$type$(<$($lts,)*>)? {
            fn sinto(&self, _: &S) -> $type {
                self.as_usize()
            }
        }
    }
}

#[allow(dead_code)]
mod test {
    #[derive(Debug)]
    pub struct Foo(usize);
    impl Foo {
        pub fn as_usize(&self) -> usize {
            self.0
        }
    }
}

sinto_todo!(test, Foo);
// sinto_as_usize!(test, Foo);

impl<S, LL, RR, L: SInto<S, LL>, R: SInto<S, RR>> SInto<S, (LL, RR)> for (L, R) {
    fn sinto(&self, s: &S) -> (LL, RR) {
        (self.0.sinto(s), self.1.sinto(s))
    }
}

impl<S, D, T: SInto<S, D>> SInto<S, Option<D>> for Option<T> {
    fn sinto(&self, s: &S) -> Option<D> {
        self.as_ref().map(|x| x.sinto(s))
    }
}
impl<S, D, T: SInto<S, D>> SInto<S, D> for Box<T> {
    fn sinto(&self, s: &S) -> D {
        (**self).sinto(s)
    }
}
impl<S, D, T: SInto<S, D>> SInto<S, D> for &T {
    fn sinto(&self, s: &S) -> D {
        (**self).sinto(s)
    }
}
impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for [T] {
    fn sinto(&self, s: &S) -> Vec<D> {
        self.iter().map(|x| x.sinto(s)).collect()
    }
}
impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for Box<[T]> {
    fn sinto(&self, s: &S) -> Vec<D> {
        self.into_iter().map(|x| x.sinto(s)).collect()
    }
}

impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for Vec<T> {
    fn sinto(&self, s: &S) -> Vec<D> {
        self.iter().map(|x| x.sinto(s)).collect()
    }
}
#[cfg(feature = "rustc")]
impl<S> SInto<S, Vec<u8>> for rustc_data_structures::sync::Lrc<[u8]> {
    fn sinto(&self, _s: &S) -> Vec<u8> {
        (**self).to_vec()
    }
}

macro_rules! sinto_clone {
    ($t:ty) => {
        impl<S> SInto<S, $t> for $t {
            fn sinto(&self, _: &S) -> $t {
                self.clone()
            }
        }
    };
    ($t:ty, $($rest:tt)*) => {
        sinto_clone!($t);
        sinto_clone!($($rest)+);
    };
    () => {};
}

sinto_clone!(bool, String, char);
sinto_clone!(u8, u16, u32, u64, u128, usize);
sinto_clone!(i8, i16, i32, i64, i128, isize);