-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhello_world.rs
57 lines (47 loc) · 1.15 KB
/
hello_world.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
use metastruct::metastruct;
#[metastruct(mappings(
map_numeric_fields_of_obj(exclude(y)),
map_mut_numeric_fields_of_obj(exclude(y), mutable)
))]
pub struct Obj {
pub x: u64,
pub y: String,
pub z: u8,
}
// FIXME(sproul): generate this
pub trait NumFields<Selector> {
const NUM_FIELDS: usize;
}
pub struct AllFields;
pub struct AllNumericFields;
impl NumFields<AllFields> for Obj {
const NUM_FIELDS: usize = 3;
}
impl NumFields<AllNumericFields> for Obj {
const NUM_FIELDS: usize = 2;
}
fn sum(obj: &Obj) -> usize {
let mut total = 0usize;
map_numeric_fields_of_obj!(obj, |_, x| total += *x as usize);
total
}
fn increment_all(obj: &mut Obj) {
map_mut_numeric_fields_of_obj!(obj, |_, x| {
*x += 1;
});
}
fn main() {
let mut obj = Obj {
x: 10,
y: "Hello world".to_string(),
z: 5,
};
println!("initial sum: {}", sum(&obj));
increment_all(&mut obj);
println!("after increment all: {}", sum(&obj));
println!(
"num fields? {}/{} are numeric",
<Obj as NumFields<AllNumericFields>>::NUM_FIELDS,
<Obj as NumFields<AllFields>>::NUM_FIELDS
);
}