From 910a8f71e01100f1ddc86625344275105dd80462 Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Wed, 29 Nov 2023 17:04:42 +0800 Subject: [PATCH] feat: let delegate_noop can handle constGeneric the macro of delegate_noop cannot handle like struct A, such kind of struct, so I want to fix it --- wayland-client/CHANGELOG.md | 1 + wayland-client/src/event_queue.rs | 29 +++++++++++++++++++++++------ wayland-server/CHANGELOG.md | 1 + wayland-server/src/dispatch.rs | 25 +++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/wayland-client/CHANGELOG.md b/wayland-client/CHANGELOG.md index 75383746121..73a64f66cd3 100644 --- a/wayland-client/CHANGELOG.md +++ b/wayland-client/CHANGELOG.md @@ -11,6 +11,7 @@ - Implement `AsFd` for `Connection` and `EventQueue` so they can easily be used in a `calloop` source. +- Make `delegate_noop` usable for constGeneric type. ## 0.31.0 -- 2023-09-02 diff --git a/wayland-client/src/event_queue.rs b/wayland-client/src/event_queue.rs index 94674bd3747..5ac3f49d164 100644 --- a/wayland-client/src/event_queue.rs +++ b/wayland-client/src/event_queue.rs @@ -786,8 +786,8 @@ impl ObjectData for TemporaryData { /// ``` #[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 { + ($(@< $( $($lt:ident )+ $( : $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, @@ -831,13 +831,28 @@ macro_rules! delegate_dispatch { /// /// // This interface should not emit events: /// delegate_noop!(ExampleApp: wl_subcompositor::WlSubcompositor); +/// +/// // Also you can do it with generic type, for example +/// struct ExampleAppGeneric; +/// delegate_noop!(@ ExampleAppGeneric: ignore wl_data_offer::WlDataOffer); +/// delegate_noop!(@ ExampleAppGeneric: wl_subcompositor::WlSubcompositor); +/// +/// // @ is to start with generic, and for base generic type looks like below. +/// trait E { +/// // add code here +/// } +/// struct ExampleAppGeneric2 { +/// a: T +/// } +/// delegate_noop!(@ ExampleAppGeneric2: wl_data_offer::WlDataOffer); +/// delegate_noop!(@ ExampleAppGeneric2: ignore wl_subcompositor::WlSubcompositor); /// ``` /// /// This last example will execute `unreachable!()` if the interface emits any events. #[macro_export] macro_rules! delegate_noop { - ($(@< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from: ty : $interface: ty) => { - impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::Dispatch<$interface, ()> for $dispatch_from { + ($(@< $( $($lt:ident )+ $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from: ty : $interface: ty) => { + impl$(< $( $($lt )+ $( : $clt $(+ $dlt )* )? ),+ >)? $crate::Dispatch<$interface, ()> for $dispatch_from { fn event( _: &mut Self, _: &$interface, @@ -851,8 +866,9 @@ macro_rules! delegate_noop { } }; - ($(@< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from: ty : ignore $interface: ty) => { - impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::Dispatch<$interface, ()> for $dispatch_from { + + ($(@< $( $($lt:ident )+ $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from: ty : ignore $interface: ty) => { + impl$(< $( $($lt )+ $( : $clt $(+ $dlt )* )? ),+ >)? $crate::Dispatch<$interface, ()> for $dispatch_from { fn event( _: &mut Self, _: &$interface, @@ -864,4 +880,5 @@ macro_rules! delegate_noop { } } }; + } diff --git a/wayland-server/CHANGELOG.md b/wayland-server/CHANGELOG.md index 62399b83c32..8cff0c3d42c 100644 --- a/wayland-server/CHANGELOG.md +++ b/wayland-server/CHANGELOG.md @@ -10,6 +10,7 @@ - Updated wayland-backend to 0.3 - Use `BorrowedFd<'_>` arguments instead of `RawFd` - `Resource::destroyed` now passes the resource type instead of the `ObjectId`. +- Make `delegate_dispatch` usable for constGeneric type. #### Additions diff --git a/wayland-server/src/dispatch.rs b/wayland-server/src/dispatch.rs index f68fc5b84dd..8ed50b2ffb6 100644 --- a/wayland-server/src/dispatch.rs +++ b/wayland-server/src/dispatch.rs @@ -347,6 +347,11 @@ impl + 'stati /// } /// } /// ``` +/// This macro_rules also support to be used with generic type, it will be similiar with client one +/// like +/// ```ignore +/// delegate_dispatch!(@ ExampleApp: [wl_output::WlOutput: MyUserData] => DelegateToMe); +/// ``` #[macro_export] macro_rules! delegate_dispatch { ($(@< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from:ty : [$interface: ty: $udata: ty] => $dispatch_to: ty) => { @@ -368,4 +373,24 @@ macro_rules! delegate_dispatch { } } }; + + ($(@< $( const $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from:ty : [$interface: ty: $udata: ty] => $dispatch_to: ty) => { + impl$(< $( const $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) + } + } + }; }