#inner #field #outer #methods #fields

delegatemethod

Delegate method calls to a field

5 releases

Uses old Rust 2015

0.2.2 Apr 12, 2017
0.2.1 Aug 1, 2016
0.2.0 Jul 24, 2016
0.1.1 Jul 24, 2016
0.1.0 Jul 24, 2016

#2472 in Rust patterns

MIT license

13KB
208 lines

delegatemethod-rs

Delegate method calls to a field.

What's new in 0.2.0

A total rearrange of code.

Now we support generic parameters of methods.

This comes at the price that now we generate the whole impl instead of impl items.

You have to put the whole impl inside the macro now.

Examples

Suppose you have a struct called Inner, with methods fn1 and fn2.

You have another struct called Outer with fields using Inner as type.

You can use delegate_method to forward some method implementations to the fields.

    #[derive(Copy, Clone)]
    struct Outer {
        inner1: Inner,
        inner2: Inner,
    }

    delegate_method! {
        impl Outer {
            // the <as FieldType> is optional below
            // it is only used for methods without self or &self or &mut self argument
            inner as Inner:
            pub fn fn1();
            
            // here comes another group, using another field.
            inner2 as Inner:
            pub fn fn2() -> usize;
        }
    }

Acknowledgements

No runtime deps