-
Notifications
You must be signed in to change notification settings - Fork 4
/
match_.rs
132 lines (107 loc) · 3.14 KB
/
match_.rs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::common::*;
use typenum::consts::*;
mod animal_test {
use super::*;
pub trait Animal {}
pub struct Dog;
impl Animal for Dog {}
pub struct Cat;
impl Animal for Cat {}
pub struct Mouse;
impl Animal for Mouse {}
typ! {
fn MatchTest1<A>(A: Animal) -> Unsigned
{
match A {
Dog => 1u,
Cat => 2u,
Mouse => 3u,
}
}
}
#[test]
fn test() {
let _: AssertSameOp<MatchTest1Op<Dog>, U1> = ();
let _: AssertSameOp<MatchTest1Op<Cat>, U2> = ();
let _: AssertSameOp<MatchTest1Op<Mouse>, U3> = ();
}
}
mod recursive_append_list_test {
use super::*;
// traits and types
pub trait List {}
pub struct Cons<Head, Tail>
where
Tail: List,
{
_head: Head,
_tail: Tail,
}
impl<Head, Tail> List for Cons<Head, Tail> where Tail: List {}
pub struct Nil;
impl List for Nil {}
// append type operator
typ! {
fn Append<input, value>(input: List, value: _) -> List {
match input {
#[generics(head, tail: List)]
Cons::<head, tail> => {
let new_tail = Append(tail, value);
Cons::<head, new_tail>
}
Nil => {
Cons::<value, Nil>
}
}
}
}
#[test]
fn test() {
type List0 = Nil;
type List1 = Cons<U0, Nil>;
type List2 = Cons<U0, Cons<U1, Nil>>;
type List3 = Cons<U0, Cons<U1, Cons<U2, Nil>>>;
type List4 = Cons<U0, Cons<U1, Cons<U2, Cons<U3, Nil>>>>;
let _: AssertSameOp<AppendOp<List0, U0>, List1> = ();
let _: AssertSameOp<AppendOp<List1, U1>, List2> = ();
let _: AssertSameOp<AppendOp<List2, U2>, List3> = ();
let _: AssertSameOp<AppendOp<List3, U3>, List4> = ();
}
}
mod attributes_test {
use super::*;
struct Alice<X>(X);
struct Bob<X>(X);
typ! {
fn Compare<lhs, rhs>(lhs: _, rhs: _) {
let lval = match lhs {
#[generics(val)]
Alice::<val> => val,
#[generics(val)]
Bob::<val> => val,
};
match rhs {
#[capture(lval)]
Alice::<lval> => (),
#[capture(lval)]
Bob::<lval> => (),
}
}
}
#[test]
fn test() {
let _: AssertSameOp<CompareOp<Alice<B0>, Alice<B0>>, ()> = ();
let _: AssertSameOp<CompareOp<Alice<B0>, Bob<B0>>, ()> = ();
let _: AssertSameOp<CompareOp<Bob<B0>, Alice<B0>>, ()> = ();
let _: AssertSameOp<CompareOp<Bob<B0>, Bob<B0>>, ()> = ();
let _: AssertSameOp<CompareOp<Alice<B1>, Alice<B1>>, ()> = ();
let _: AssertSameOp<CompareOp<Alice<B1>, Bob<B1>>, ()> = ();
let _: AssertSameOp<CompareOp<Bob<B1>, Alice<B1>>, ()> = ();
let _: AssertSameOp<CompareOp<Bob<B1>, Bob<B1>>, ()> = ();
}
}
#[test]
fn compile_fail_test() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/macro/fail_match_attribute.rs");
}