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
#![no_std]
#[macro_export]
macro_rules! fake_inheritance {
($sname:ident, $fname:ident, fields = [$($ffname:ident:$return_val:ident;)*]) => {
impl $sname {
$(
pub fn $ffname(&self) -> $return_val {
self.$fname.$ffname
}
)*
}
};
($sname:ident, $fname:ident, fields = [$(f($ffname:ident):$return_val:ident;)*]) => {
impl $sname {
$(
pub fn $ffname(&self) -> $return_val {
self.$fname.$ffname()
}
)*
}
};
}
#[cfg(test)]
mod tests {
#[test]
fn test_the_macro_1() {
struct FakeInheritance {
a: i32,
b: i32,
}
struct A {
fi: FakeInheritance,
}
fake_inheritance!(A, fi, fields = [a: i32; b: i32;]);
let a = A { fi: FakeInheritance { a: 42, b: 42 }};
assert_eq!(a.a(), 42);
assert_eq!(a.b(), 42);
}
#[test]
fn test_the_macro_2() {
struct FakeInheritance {
a: i32,
b: i32,
}
impl FakeInheritance {
fn a(&self) -> i32 {
self.a
}
fn b(&self) -> i32 {
self.b
}
}
struct A {
fi: FakeInheritance,
}
fake_inheritance!(A, fi, fields = [f(a): i32; f(b): i32;]);
let a = A { fi: FakeInheritance { a: 42, b: 42 }};
assert_eq!(a.a(), 42);
assert_eq!(a.b(), 42);
}
}