Skip to content

Commit

Permalink
Remove delegate macros
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Dec 14, 2024
1 parent 79e68da commit 6473d06
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 677 deletions.
1 change: 1 addition & 0 deletions wayland-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `QueueHandle::make_data` now accepts additional `DelegateTo` generic,
therefore allowing users to dispatch events to types different than main `State`
- `delegate_dispatch` Removed in favour of `DelegateTo` generic on `QueueHandle::make_data`

#### Additions

Expand Down
95 changes: 0 additions & 95 deletions wayland-client/src/event_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,101 +727,6 @@ impl<I: Proxy, U: std::fmt::Debug, State, DispatchTo> std::fmt::Debug
}
}

/*
* Dispatch delegation helpers
*/

/// 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`][crate::protocol::wl_registry::WlRegistry]
/// to the struct `DelegateToMe` for the [`Dispatch`] documentatione example.
///
/// ```
/// use wayland_client::{delegate_dispatch, protocol::wl_registry};
/// #
/// # use wayland_client::Dispatch;
/// #
/// # struct DelegateToMe;
/// # struct MyUserData;
/// #
/// # impl<State> Dispatch<wl_registry::WlRegistry, MyUserData, State> for DelegateToMe
/// # where
/// # State: Dispatch<wl_registry::WlRegistry, MyUserData> + AsMut<DelegateToMe>,
/// # {
/// # fn event(
/// # _state: &mut State,
/// # _proxy: &wl_registry::WlRegistry,
/// # _event: wl_registry::Event,
/// # _udata: &MyUserData,
/// # _conn: &wayland_client::Connection,
/// # _qhandle: &wayland_client::QueueHandle<State>,
/// # ) {
/// # }
/// # }
///
/// // 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>();
/// ```
#[macro_export]
macro_rules! delegate_dispatch {
($(@< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from:ty : [$interface: ty: $udata: ty] => $dispatch_to: ty) => {
impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::Dispatch<$interface, $udata> for $dispatch_from {
fn event(
state: &mut Self,
proxy: &$interface,
event: <$interface as $crate::Proxy>::Event,
data: &$udata,
conn: &$crate::Connection,
qhandle: &$crate::QueueHandle<Self>,
) {
<$dispatch_to as $crate::Dispatch<$interface, $udata, Self>>::event(state, proxy, event, data, conn, qhandle)
}

fn event_created_child(
opcode: u16,
qhandle: &$crate::QueueHandle<Self>
) -> ::std::sync::Arc<dyn $crate::backend::ObjectData> {
<$dispatch_to as $crate::Dispatch<$interface, $udata, Self>>::event_created_child(opcode, qhandle)
}
}
};
}

/// A helper macro which delegates a set of [`Dispatch`] implementations for proxies to a static handler.
///
/// # Usage
Expand Down
1 change: 1 addition & 0 deletions wayland-server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Breaking changes
- Added `Resource::delegated_data<_, DelegatedTo>` for accessing user data of delegated objects
- `delegate_dispatch` and `delegate_global_dispatch` Removed in favour of `DataInit::init_delegated` and `DisplayHandle::create_delegated_global`

#### Additions
- Added a way to create delegated objects, globals and resources (a way to dispatch events to types different than the main `State`)
Expand Down
77 changes: 0 additions & 77 deletions wayland-server/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,80 +412,3 @@ pub(crate) fn on_destroyed<DATA, I, U, D, DelegatedTo>(

<DelegatedTo as Dispatch<I, U, D>>::destroyed(data, client_id, &resource, udata)
}

/// A helper macro which delegates a set of [`Dispatch`] implementations for a resource to some other type which
/// provides a generic [`Dispatch`] implementation.
///
/// This macro allows more easily delegating smaller parts of the protocol a compositor may wish to handle
/// in a modular fashion.
///
/// # Usage
///
/// For example, say you want to delegate events for [`WlOutput`][crate::protocol::wl_output::WlOutput]
/// to the `DelegateToMe` type from the [`Dispatch`] documentation.
///
/// ```
/// use wayland_server::{delegate_dispatch, protocol::wl_output};
/// #
/// # use wayland_server::Dispatch;
/// #
/// # struct DelegateToMe;
/// #
/// # impl<D> Dispatch<wl_output::WlOutput, (), D> for DelegateToMe
/// # where
/// # D: Dispatch<wl_output::WlOutput, ()> + AsMut<DelegateToMe>,
/// # {
/// # fn request(
/// # _state: &mut D,
/// # _client: &wayland_server::Client,
/// # _resource: &wl_output::WlOutput,
/// # _request: wl_output::Request,
/// # _data: &(),
/// # _dhandle: &wayland_server::DisplayHandle,
/// # _data_init: &mut wayland_server::DataInit<'_, D>,
/// # ) {
/// # }
/// # }
/// #
/// # type MyUserData = ();
///
/// // 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_output::WlOutput, MyUserData> for ExampleApp.
/// delegate_dispatch!(ExampleApp: [wl_output::WlOutput: 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
/// }
/// }
/// ```
#[macro_export]
macro_rules! delegate_dispatch {
($(@< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from:ty : [$interface: ty: $udata: ty] => $dispatch_to: ty) => {
impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::Dispatch<$interface, $udata> for $dispatch_from {
fn request(
state: &mut Self,
client: &$crate::Client,
resource: &$interface,
request: <$interface as $crate::Resource>::Request,
data: &$udata,
dhandle: &$crate::DisplayHandle,
data_init: &mut $crate::DataInit<'_, Self>,
) {
<$dispatch_to as $crate::Dispatch<$interface, $udata, Self>>::request(state, client, resource, request, data, dhandle, data_init)
}

fn destroyed(state: &mut Self, client: $crate::backend::ClientId, resource: &$interface, data: &$udata) {
<$dispatch_to as $crate::Dispatch<$interface, $udata, Self>>::destroyed(state, client, resource, data)
}
}
};
}
147 changes: 0 additions & 147 deletions wayland-server/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,150 +124,3 @@ pub trait GlobalDispatch<I: Resource, GlobalData, State = Self>: Sized {
true
}
}

/*
* Dispatch delegation helpers
*/

/// A helper macro which delegates a set of [`GlobalDispatch`] implementations for a resource to some other type which
/// provdes a generic [`GlobalDispatch`] implementation.
///
/// Its usage is similar to the [`delegate_dispatch!()`] macro.
///
/// [`delegate_dispatch!()`]: crate::delegate_dispatch!()
#[macro_export]
macro_rules! delegate_global_dispatch {
($(@< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from:ty : [$interface: ty: $udata: ty] => $dispatch_to: ty) => {
impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::GlobalDispatch<$interface, $udata> for $dispatch_from {
fn bind(
state: &mut Self,
dhandle: &$crate::DisplayHandle,
client: &$crate::Client,
resource: $crate::New<$interface>,
global_data: &$udata,
data_init: &mut $crate::DataInit<'_, Self>,
) {
<$dispatch_to as $crate::GlobalDispatch<$interface, $udata, Self>>::bind(state, dhandle, client, resource, global_data, data_init)
}

fn can_view(client: $crate::Client, global_data: &$udata) -> bool {
<$dispatch_to as $crate::GlobalDispatch<$interface, $udata, Self>>::can_view(client, global_data)
}
}
};
}

#[cfg(test)]
mod tests {
#[test]
fn smoke_test_dispatch_global_dispatch() {
use crate::{
delegate_dispatch, protocol::wl_output, Client, DataInit, Dispatch, DisplayHandle,
GlobalDispatch, New,
};

struct DelegateToMe;

impl<D> Dispatch<wl_output::WlOutput, (), D> for DelegateToMe
where
D: Dispatch<wl_output::WlOutput, ()> + AsMut<DelegateToMe>,
{
fn request(
_state: &mut D,
_client: &Client,
_resource: &wl_output::WlOutput,
_request: wl_output::Request,
_data: &(),
_dhandle: &DisplayHandle,
_data_init: &mut DataInit<'_, D>,
) {
}
}
impl<D> GlobalDispatch<wl_output::WlOutput, (), D> for DelegateToMe
where
D: GlobalDispatch<wl_output::WlOutput, ()>,
D: Dispatch<wl_output::WlOutput, ()>,
D: AsMut<DelegateToMe>,
{
fn bind(
_state: &mut D,
_handle: &DisplayHandle,
_client: &Client,
_resource: New<wl_output::WlOutput>,
_global_data: &(),
_data_init: &mut DataInit<'_, D>,
) {
}
}

struct ExampleApp {
delegate: DelegateToMe,
}

delegate_dispatch!(ExampleApp: [wl_output::WlOutput: ()] => DelegateToMe);
delegate_global_dispatch!(ExampleApp: [wl_output::WlOutput: ()] => DelegateToMe);

impl AsMut<DelegateToMe> for ExampleApp {
fn as_mut(&mut self) -> &mut DelegateToMe {
&mut self.delegate
}
}
}

#[test]
fn smoke_test_dispatch_global_dispatch_generics() {
use crate::{
delegate_dispatch, protocol::wl_output, Client, DataInit, Dispatch, DisplayHandle,
GlobalDispatch, New,
};

struct DelegateToMe<A>(A);

impl<A, D> Dispatch<wl_output::WlOutput, (), D> for DelegateToMe<A>
where
A: 'static,
D: Dispatch<wl_output::WlOutput, ()> + AsMut<DelegateToMe<A>>,
{
fn request(
_state: &mut D,
_client: &Client,
_resource: &wl_output::WlOutput,
_request: wl_output::Request,
_data: &(),
_dhandle: &DisplayHandle,
_data_init: &mut DataInit<'_, D>,
) {
}
}
impl<A, D> GlobalDispatch<wl_output::WlOutput, (), D> for DelegateToMe<A>
where
A: 'static,
D: GlobalDispatch<wl_output::WlOutput, ()>,
D: Dispatch<wl_output::WlOutput, ()>,
D: AsMut<DelegateToMe<A>>,
{
fn bind(
_state: &mut D,
_handle: &DisplayHandle,
_client: &Client,
_resource: New<wl_output::WlOutput>,
_global_data: &(),
_data_init: &mut DataInit<'_, D>,
) {
}
}

struct ExampleApp<A> {
delegate: DelegateToMe<A>,
}

delegate_dispatch!(@<A: 'static> ExampleApp<A>: [wl_output::WlOutput: ()] => DelegateToMe<A>);
delegate_global_dispatch!(@<A: 'static> ExampleApp<A>: [wl_output::WlOutput: ()] => DelegateToMe<A>);

impl<A> AsMut<DelegateToMe<A>> for ExampleApp<A> {
fn as_mut(&mut self) -> &mut DelegateToMe<A> {
&mut self.delegate
}
}
}
}
Loading

0 comments on commit 6473d06

Please sign in to comment.