Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Structs

Place #[moxy(forward(fn ...))] on a field to generate methods on the parent struct that delegate to that field.

Basic Usage

use moxy::Forward;

#[derive(Forward)]
struct App {
    #[moxy(forward(
        fn len(&self) -> usize,
        fn is_empty(&self) -> bool,
        fn push(&mut self, item: String),
    ))]
    items: Vec<String>,
}

let mut app = App { items: vec![] };
app.push("hello".into());
assert_eq!(app.len(), 1);

Both &self and &mut self methods are supported. Arguments are passed through directly.

Multiple Fields

Different fields can forward different methods:

use moxy::Forward;

struct Config { name: String }
impl Config {
    fn name(&self) -> &str { &self.name }
}

struct Transport { log: Vec<String> }
impl Transport {
    fn send(&mut self, msg: &str) { self.log.push(msg.into()); }
}

#[derive(Forward)]
struct Service {
    #[moxy(forward(fn name(&self) -> &str))]
    config: Config,
    #[moxy(forward(fn send(&mut self, msg: &str)))]
    transport: Transport,
}

let mut svc = Service {
    config: Config { name: "api".into() },
    transport: Transport { log: vec![] },
};
assert_eq!(svc.name(), "api");
svc.send("hello");