macro_rules! delegate_dispatch {
    ($dispatch_from: ty: [$($interface: ty),*] => $dispatch_to: ty) => { ... };
}
Expand description

A helper macro which delegates a set of Dispatch implementations for a proxy to some other type which implements DelegateDispatch for each proxy.

This macro allows more easily delegating smaller parts of the protocol an application may wish to handle in a modular fashion.

Usage

For example, say you want to delegate events for WlRegistry to some other type.

For brevity, we will use the example in the documentation for DelegateDispatch, DelegateToMe.

use wayland_client::{delegate_dispatch, protocol::wl_registry};

// ExampleApp is the type events will be dispatched to.

/// The application state
struct ExampleApp {
    /// The delegate for handling wl_registry events.
    delegate: DelegateToMe,
}

// Use delegate_dispatch to implement Dispatch<wl_registry::WlRegistry> for ExampleApp.
delegate_dispatch!(ExampleApp: [wl_registry::WlRegistry] => DelegateToMe);

// But DelegateToMe requires that ExampleApp implements AsMut<DelegateToMe>, so we provide this impl
impl AsMut<DelegateToMe> for ExampleApp {
    fn as_mut(&mut self) -> &mut DelegateToMe {
        &mut self.delegate
    }
}

// To explain the macro above, you may read it as the following:
//
// For ExampleApp, delegate WlRegistry to DelegateToMe and use the closure to get an `&mut` reference to
// the delegate.

// Assert ExampleApp can Dispatch events for wl_registry
fn assert_is_registry_delegate<T>()
where
    T: Dispatch<wl_registry::WlRegistry, UserData = ()>,
{
}

assert_is_registry_delegate::<ExampleApp>();

You may also delegate multiple proxies to a single type. This is especially useful for handling multiple related protocols in the same modular component.

For example, a type which can dispatch both the wl_output and xdg_output protocols may be used as a delegate:

delegate_dispatch!(ExampleApp: [wl_output::WlOutput, xdg_output::XdgOutput] => OutputDelegate);