Attribute Macro delegate
#[delegate]Expand description
Embed-and-delegate: forward the un-written View methods to an inner view.
The delegate field owns the view’s one ViewState, so the macro forwards
state/state_mut too. This compiles and forwards everything:
use tvision_rs::{Group, Rect, View, delegate};
struct Panel {
group: Group,
}
#[delegate(to = group)]
impl View for Panel {}
let p = Panel { group: Group::new(Rect::new(0, 0, 10, 5)) };
assert_eq!(p.state().get_bounds(), Rect::new(0, 0, 10, 5));A hand-written state/state_mut is accepted only when its body is the
forwarder the macro would generate anyway. It is redundant; leave it out:
use tvision_rs::{Group, View, ViewState, delegate};
struct Panel {
group: Group,
}
#[delegate(to = group)]
impl View for Panel {
fn state(&self) -> &ViewState {
self.group.state()
}
fn state_mut(&mut self) -> &mut ViewState {
self.group.state_mut()
}
}Parentheses around the receiver are accepted too:
use tvision_rs::{Group, View, ViewState, delegate};
struct Panel {
group: Group,
}
#[delegate(to = group)]
impl View for Panel {
fn state(&self) -> &ViewState {
(self.group).state()
}
}Any other state/state_mut body is a compile error: it would be a second
ViewState that every forwarded method ignores.
use tvision_rs::{Group, View, ViewState, delegate};
struct Panel {
group: Group,
st: ViewState,
}
#[delegate(to = group)]
impl View for Panel {
fn state(&self) -> &ViewState {
&self.st
}
fn state_mut(&mut self) -> &mut ViewState {
&mut self.st
}
}skip(state) / skip(state_mut) is rejected for the same reason. This
example documents the rule; rustc would reject it anyway (E0046, missing
required trait item), so the macro’s check only gives a clearer message:
use tvision_rs::{Group, View, delegate};
struct Panel {
group: Group,
}
#[delegate(to = group, skip(state))]
impl View for Panel {}#[delegate(to = <field>, skip(method1, method2, ...))]
Injects forwarders for every method of the trait that the impl block does
not already provide, each forwarding to self.<field>.<method>(<args>).
This is the boilerplate half of the embed-and-delegate pattern (the port’s
stand-in for C++ implementation inheritance). Only the View trait is
currently supported.
to = <field>(required): the field to forward un-provided methods to.skip(...)(optional): methods to leave at the trait’s own default rather than forwarding — use when a delegating type intentionally inherits the default instead of the inner field’s behavior.
§Pitfall: one ViewState
The delegate field owns the view’s ViewState. Every forwarded method
(set_state, change_bounds, calc_bounds, focus_to_edge, …) works on
self.<field>.state(). A state/state_mut override that returns a field
of the outer type gives the view a second ViewState that those methods
never touch: StateFlag::Focused lands on the inner state, so the outer
state().state.focused stays false while the view receives keys. The
macro therefore rejects a state/state_mut override unless its body is
exactly self.<field>.state() / self.<field>.state_mut() (the forwarder
it would generate anyway, so redundant but harmless). It also rejects
skip(state) / skip(state_mut). Keep the state in the delegate field, or
implement the trait by hand without #[delegate].
§Errors
Emits a compile error if to is missing or duplicated, if the macro is on a
plain impl (not impl Trait for Type), if the trait is not a known
delegatable trait, if a skip(...) name is not a method of that trait, or if
a View impl skips state/state_mut or provides one whose body is not
the plain forwarder self.<field>.state() / self.<field>.state_mut().