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
|
#![feature(decl_macro)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Field {
RootCtxt,
MacroCtxt,
}
#[rustfmt::skip]
macro x(
$macro_name:ident,
$macro2_name:ident,
$type_name:ident,
$field_name:ident,
$const_name:ident
) {
#[derive(Copy, Clone)]
pub struct $type_name {
pub field: Field,
pub $field_name: Field,
}
pub const $const_name: $type_name =
$type_name { field: Field::MacroCtxt, $field_name: Field::RootCtxt };
#[macro_export]
macro_rules! $macro_name {
(check_fields_of $e:expr) => {{
let e = $e;
assert_eq!(e.field, Field::MacroCtxt);
assert_eq!(e.$field_name, Field::RootCtxt);
}};
(check_fields) => {{
assert_eq!($const_name.field, Field::MacroCtxt);
assert_eq!($const_name.$field_name, Field::RootCtxt);
}};
(construct) => {
$type_name { field: Field::MacroCtxt, $field_name: Field::RootCtxt }
};
}
pub macro $macro2_name {
(check_fields_of $e:expr) => {{
let e = $e;
assert_eq!(e.field, Field::MacroCtxt);
assert_eq!(e.$field_name, Field::RootCtxt);
}},
(check_fields) => {{
assert_eq!($const_name.field, Field::MacroCtxt);
assert_eq!($const_name.$field_name, Field::RootCtxt);
}},
(construct) => {
$type_name { field: Field::MacroCtxt, $field_name: Field::RootCtxt }
}
}
}
x!(test_fields, test_fields2, MyStruct, field, MY_CONST);
pub fn check_fields(s: MyStruct) {
test_fields!(check_fields_of s);
}
pub fn check_fields_local() {
test_fields!(check_fields);
test_fields2!(check_fields);
let s1 = test_fields!(construct);
test_fields!(check_fields_of s1);
let s2 = test_fields2!(construct);
test_fields2!(check_fields_of s2);
}
|