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
//! A little utility macro for a "faked" inheritance.
//! It generates shortcuts from the inner struct's fields to the parent struct,
//! to make the access of these fields easier to use. But with minimal effort.
#![no_std]

/// Input of this macro follows: (parent_struct_name, field_name_for_inner_struct, fields = [
/// field_name: field_type; ...]) (The ... indicates that the pattern can be used infinitely).
/// For the macro to use the inner struct's accesor functions (e. g `inner.a()`); use
/// "fields = [f(field_name): field_type; ...]" 
///
/// # Examples
/// ```rust,ignore
/// #[macro_use]
/// extern crate fake_inheritance;
/// 
/// struct Inner {
///    a: i32, b: i32
/// }
///
/// struct Parent { inner: Inner }
///
/// fake_inheritance!(Parent, inner, fields = [a: i32; b: i32;]);
///
/// let parent = Parent { inner: Inner { a: 1, b: 2 }}; 
/// assert_eq!(parent.a(), 1);
/// assert_eq!(parent.b(), 2);
/// ```
#[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);
    }
}