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