Macro wayland_client::delegate_dispatch

source ·
macro_rules! delegate_dispatch {
    ($(@< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from:ty : [$interface: ty: $udata: 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>();