Derive a trait by delegating to a struct member which implements that trait

Viewed 43

Consider the following struct:

struct SectoredStream<T> {
    sector_sz: usize,
    inner: T,
}

When T implements Read I can implement Read for SectoredStream<T> as follows:

impl<T: Read> Read for SectoredStream<T> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.read(buf)
    }
}

This is a simple example, but in general I may want to implement a complex trait by delegating all of the trait methods to one of the members of my struct which implements that trait. It seems clear that this type of boilerplate code could be generated by a macro, and I can see how to do it for a particular trait using a declarative macro. I believe the general case will require a procedural macro, but I doubt this is a unique problem. So, is there already a crate which implements this macro?

0 Answers
Related