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
118
119
use disjoint_impls::disjoint_impls;
pub trait Dispatch {
type Group;
}
trait Tr {
type A;
}
impl Tr for u8 {
type A = u16;
}
impl Tr for i8 {
type A = i16;
}
impl Dispatch for String {
type Group = (u32, u32);
}
impl<T> Dispatch for Vec<T> {
type Group = (u32, String);
}
impl Dispatch for i32 {
type Group = [(String, String); 1];
}
impl Dispatch for u32 {
type Group = [(String, u32); 1];
}
impl Dispatch for u8 {
type Group = (i8,);
}
impl Dispatch for i8 {
type Group = (u8,);
}
disjoint_impls! {
pub trait Kita<U, V> {
fn kita(_a: U, _b: V) -> String {
"Default Blanket".to_owned()
}
}
impl<T: Dispatch<Group = (U, V)>, U, V> Kita<(U,), V> for T {
fn kita(_a: (U,), _b: V) -> String {
"Generic Blanket A".to_owned()
}
}
impl<T: Dispatch<Group = [(U, V); 1]>, U: ToString, V> Kita<(V,), U> for T {
fn kita(_a: (V,), b: U) -> String {
b.to_string()
}
}
impl<T: Dispatch<Group = (U,)>, U: Tr<A = A>, A> Kita<u32, U> for T {}
}
/*
pub trait Kita<U, V> {
fn kita(_a: U, _b: V) -> String {
"Default Blanket".to_owned()
}
}
const _: () = {
pub trait Kita0<_TŠČ2: ?Sized, U, V> {
fn kita(_a: U, _b: V) -> String {
"Default Blanket".to_owned()
}
}
impl<T: Dispatch<Group = (U, V)>, U, V> Kita0<(U, V), (U,), V> for T {
fn kita(_a: (U,), _b: V) -> String {
"Generic Blanket A".to_owned()
}
}
impl<T: Dispatch<Group = [(U, V); 1]>, U: ToString, V> Kita0<[(U, V); 1], (V,), U>
for T {
fn kita(_a: (V,), b: U) -> String {
b.to_string()
}
}
impl<_TŠČ0, _TŠČ1, _TŠČ2> Kita<(_TŠČ0,), _TŠČ1> for _TŠČ2
where
_TŠČ2: Dispatch,
Self: Kita0<<_TŠČ2 as Dispatch>::Group, (_TŠČ0,), _TŠČ1>,
{
fn kita(_a: (_TŠČ0,), _b: _TŠČ1) -> String {
<Self as Kita0<
<_TŠČ2 as Dispatch>::Group,
(_TŠČ0,),
_TŠČ1,
>>::kita(_a, _b)
}
}
impl<T: Dispatch<Group = (U,)>, U: Tr<A = A>, A> Kita<u32, U> for T {}
};
*/
#[test]
fn dispatch_on_generic() {
let a_arg1 = 42;
let a_arg2 = 420;
let b_arg1 = "Generic Blanket B".to_owned();
let b_arg2 = "moja".to_owned();
let b_arg3 = "kita".to_owned();
assert_eq!("Generic Blanket A", String::kita((a_arg1,), a_arg2));
assert_eq!("Generic Blanket A", Vec::<u32>::kita((a_arg1,), b_arg2));
assert_eq!("Generic Blanket B", i32::kita((b_arg3,), b_arg1.clone()));
assert_eq!("Generic Blanket B", u32::kita((a_arg2,), b_arg1));
assert_eq!("Default Blanket", u8::kita(a_arg2, 8_i8));
assert_eq!("Default Blanket", i8::kita(a_arg2, 8_u8));
}