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

A helper macro which delegates a set of Dispatch implementations for proxies to some other type which provides a generic Dispatch implementation.

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 the struct DelegateToMe for the Dispatch documentatione example.

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, MyUserData> for ExampleApp
delegate_dispatch!(ExampleApp: [wl_registry::WlRegistry: MyUserData] => DelegateToMe);

// DelegateToMe requires that ExampleApp implements AsMut<DelegateToMe>, so we provide the
// trait implementation.
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.

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

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: OutputData,
    xdg_output::XdgOutput: XdgOutputData,
] => OutputDelegate);