Skip to content

Commit

Permalink
feat: let delegate_noop can handle constGeneric
Browse files Browse the repository at this point in the history
the macro of delegate_noop cannot handle like
struct A<const X: unsize>, such kind of struct, so I want to fix it
  • Loading branch information
Decodetalkers committed Nov 29, 2023
1 parent fdd88c4 commit 2a378db
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
46 changes: 46 additions & 0 deletions wayland-client/src/event_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,21 @@ 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<const X: usize>;
/// delegate_noop!(@<const X: usize> ExampleAppGeneric<X>: ignore wl_data_offer::WlDataOffer);
/// delegate_noop!(@<const X: usize> ExampleAppGeneric<X>: wl_subcompositor::WlSubcompositor);
///
/// // @ is to start with generic, and for base generic type is also ok
/// trait E {
/// // add code here
/// }
/// struct ExampleAppGeneric2<T: E> {
/// a: T
/// }
/// delegate_noop!(@<T: E> Name2<T>: ignore wl_data_offer::WlDataOffer);
/// delegate_noop!(@<T: E> Name2<T>: ignore wl_subcompositor::WlSubcompositor);
/// ```
///
/// This last example will execute `unreachable!()` if the interface emits any events.
Expand All @@ -851,6 +866,22 @@ macro_rules! delegate_noop {
}
};

// delegate_noop!(@<const X: unsize> ExampleApp<X>: wl_subcompositor::WlSubcompositor);
($(@< $( const $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from: ty : $interface: ty) => {
impl$(< $(const $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::Dispatch<$interface, ()> for $dispatch_from {
fn event(
_: &mut Self,
_: &$interface,
_: <$interface as $crate::Proxy>::Event,
_: &(),
_: &$crate::Connection,
_: &$crate::QueueHandle<Self>,
) {
unreachable!();
}
}
};

($(@< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from: ty : ignore $interface: ty) => {
impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::Dispatch<$interface, ()> for $dispatch_from {
fn event(
Expand All @@ -864,4 +895,19 @@ macro_rules! delegate_noop {
}
}
};

// delegate_noop!(@<const X: unsize> ExampleApp<X>: ignore wl_subcompositor::WlSubcompositor);
($(@< $( const $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? $dispatch_from: ty : ignore $interface: ty) => {
impl$(< $( const $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::Dispatch<$interface, ()> for $dispatch_from {
fn event(
_: &mut Self,
_: &$interface,
_: <$interface as $crate::Proxy>::Event,
_: &(),
_: &$crate::Connection,
_: &$crate::QueueHandle<Self>,
) {
}
}
};
}
24 changes: 24 additions & 0 deletions wayland-server/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ impl<I: Resource + 'static, U: Send + Sync + 'static, D: Dispatch<I, U> + 'stati
/// }
/// }
/// ```
/// This macro_rules also support to be used with generic type, it will be similiar with client one
/// like
/// ```rust, no_run
/// delegate_dispatch!(@<const X : usize> ExampleApp<X>: [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) => {
Expand All @@ -363,6 +368,25 @@ macro_rules! delegate_dispatch {
<$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)
}
}
};
($(@< $( 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)
}
Expand Down

0 comments on commit 2a378db

Please sign in to comment.