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 a045b09 commit d3b6104
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 735 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`

## 0.31.7 -- 2024-10-23

Expand Down
154 changes: 1 addition & 153 deletions wayland-client/src/event_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,156 +727,4 @@ impl<I: Proxy, U: std::fmt::Debug, State, DispatchTo> std::fmt::Debug
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("QueueProxyData").field("udata", &self.udata).finish()
}
}

/*
* 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
///
/// This macro is useful to implement [`Dispatch`] for interfaces where events are unimportant to
/// the current application and can be ignored.
///
/// # Example
///
/// ```
/// use wayland_client::{delegate_noop, protocol::{wl_data_offer, wl_subcompositor}};
///
/// /// The application state
/// struct ExampleApp {
/// // ...
/// }
///
/// // Ignore all events for this interface:
/// delegate_noop!(ExampleApp: ignore wl_data_offer::WlDataOffer);
///
/// // This interface should not emit events:
/// delegate_noop!(ExampleApp: 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 {
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(
_: &mut Self,
_: &$interface,
_: <$interface as $crate::Proxy>::Event,
_: &(),
_: &$crate::Connection,
_: &$crate::QueueHandle<Self>,
) {
}
}
};
}
}
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 @@ -415,80 +415,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)
}
}
};
}
Loading

0 comments on commit d3b6104

Please sign in to comment.