Skip to content

Commit

Permalink
use as-any instead of downcast-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
fogti committed Sep 23, 2023
1 parent e1be14c commit 1824fc4
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion wayland-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ readme = "README.md"
build = "build.rs"

[dependencies]
as-any = "0.3"
wayland-sys = { version = "0.31.0", path = "../wayland-sys", features = [] }
log = { version = "0.4", optional = true }
scoped-tls = "1.0"
downcast-rs = "1.2"
raw-window-handle = { version = "0.5.0", optional = true }

[dependencies.smallvec]
Expand Down
6 changes: 3 additions & 3 deletions wayland-backend/src/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ use super::client_impl;

pub use crate::types::client::{InvalidId, NoWaylandLib, WaylandError};

use as_any::AsAny;

/// A trait representing your data associated to an object
///
/// You will only be given access to it as a `&` reference, so you
/// need to handle interior mutability by yourself.
///
/// The methods of this trait will be invoked internally every time a
/// new object is created to initialize its data.
pub trait ObjectData: downcast_rs::DowncastSync {
pub trait ObjectData: Send + Sync + AsAny {
/// Dispatch an event for the associated object
///
/// If the event has a `NewId` argument, the callback must return the object data
Expand Down Expand Up @@ -58,8 +60,6 @@ impl std::fmt::Debug for dyn ObjectData {
}
}

downcast_rs::impl_downcast!(sync ObjectData);

/// An ID representing a Wayland object
///
/// The backend internally tracks which IDs are still valid, invalidates them when the protocol object they
Expand Down
6 changes: 3 additions & 3 deletions wayland-backend/src/rs/server_impl/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use super::{
InnerClientId, InnerGlobalId, InnerObjectId, ObjectData, ObjectId,
};

use as_any::Downcast;

pub(crate) type PendingDestructor<D> = (Arc<dyn ObjectData<D>>, InnerClientId, InnerObjectId);

#[derive(Debug)]
Expand Down Expand Up @@ -269,7 +271,7 @@ impl InnerHandle {
}
}

pub(crate) trait ErasedState: downcast_rs::Downcast {
pub(crate) trait ErasedState: as_any::AsAny {
fn object_info(&self, id: InnerObjectId) -> Result<ObjectInfo, InvalidId>;
fn insert_client(
&mut self,
Expand Down Expand Up @@ -302,8 +304,6 @@ pub(crate) trait ErasedState: downcast_rs::Downcast {
fn flush(&mut self, client: Option<ClientId>) -> std::io::Result<()>;
}

downcast_rs::impl_downcast!(ErasedState);

impl<D> ErasedState for State<D> {
fn object_info(&self, id: InnerObjectId) -> Result<ObjectInfo, InvalidId> {
self.clients.get_client(id.client_id.clone())?.object_info(id)
Expand Down
27 changes: 18 additions & 9 deletions wayland-backend/src/server_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,29 @@ pub use crate::types::server::{Credentials, DisconnectReason, GlobalInfo, InitEr

use super::server_impl;

use as_any::AsAny;

/// Extends `AsAny` to support `Sync` traits that thus support `Arc` downcasting as well.
pub trait IntoAnyArc: Send + Sync + AsAny {
/// Convert `Arc<Trait>` (where `Trait: Downcast`) to `Arc<Any>`. `Arc<Any>` can then be
/// further `downcast` into `Arc<ConcreteType>` where `ConcreteType` implements `Trait`.
fn into_any_arc(self: Arc<Self>) -> Arc<dyn std::any::Any + Send + Sync>;
}

impl<T: Send + Sync + AsAny> IntoAnyArc for T {
fn into_any_arc(self: Arc<Self>) -> Arc<dyn std::any::Any + Send + Sync> {
self
}
}

/// A trait representing your data associated to an object
///
/// You will only be given access to it as a `&` reference, so you
/// need to handle interior mutability by yourself.
///
/// The methods of this trait will be invoked internally every time a
/// new object is created to initialize its data.
pub trait ObjectData<D>: downcast_rs::DowncastSync {
pub trait ObjectData<D>: IntoAnyArc {
/// Dispatch a request for the associated object
///
/// If the request has a `NewId` argument, the callback must return the object data
Expand Down Expand Up @@ -47,8 +62,6 @@ pub trait ObjectData<D>: downcast_rs::DowncastSync {
}
}

downcast_rs::impl_downcast!(sync ObjectData<D>);

impl<D: 'static> std::fmt::Debug for dyn ObjectData<D> {
#[cfg_attr(coverage, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -57,7 +70,7 @@ impl<D: 'static> std::fmt::Debug for dyn ObjectData<D> {
}

/// A trait representing the handling of new bound globals
pub trait GlobalHandler<D>: downcast_rs::DowncastSync {
pub trait GlobalHandler<D>: Send + Sync + AsAny {
/// Check if given client is allowed to interact with given global
///
/// If this function returns false, the client will not be notified of the existence
Expand Down Expand Up @@ -102,10 +115,8 @@ impl<D: 'static> std::fmt::Debug for dyn GlobalHandler<D> {
}
}

downcast_rs::impl_downcast!(sync GlobalHandler<D>);

/// A trait representing your data associated to a client
pub trait ClientData: downcast_rs::DowncastSync {
pub trait ClientData: Send + Sync + AsAny {
/// Notification that the client was initialized
fn initialized(&self, _client_id: ClientId) {}
/// Notification that the client is disconnected
Expand All @@ -128,8 +139,6 @@ impl std::fmt::Debug for dyn ClientData {

impl ClientData for () {}

downcast_rs::impl_downcast!(sync ClientData);

/// An ID representing a Wayland object
///
/// The backend internally tracks which IDs are still valid, invalidates them when the protocol object they
Expand Down
5 changes: 2 additions & 3 deletions wayland-backend/src/sys/server_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::protocol::{
check_for_signature, same_interface, AllowNull, Argument, ArgumentType, Interface, Message,
ObjectInfo, ANONYMOUS_INTERFACE,
};
use as_any::Downcast;
use scoped_tls::scoped_thread_local;
use smallvec::SmallVec;

Expand Down Expand Up @@ -822,7 +823,7 @@ impl InnerHandle {
}
}

pub(crate) trait ErasedState: downcast_rs::Downcast {
pub(crate) trait ErasedState: as_any::AsAny {
fn object_info(&self, id: InnerObjectId) -> Result<ObjectInfo, InvalidId>;
fn insert_client(
&self,
Expand Down Expand Up @@ -857,8 +858,6 @@ pub(crate) trait ErasedState: downcast_rs::Downcast {
fn display_ptr(&self) -> *mut wl_display;
}

downcast_rs::impl_downcast!(ErasedState);

impl<D: 'static> ErasedState for State<D> {
fn object_info(&self, id: InnerObjectId) -> Result<ObjectInfo, InvalidId> {
if !id.alive.load(Ordering::Acquire) {
Expand Down
8 changes: 4 additions & 4 deletions wayland-backend/tests/rs_sys_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ mod server {
fn test_impls() {
// ObjectData
assert_impl!(
dyn server::ObjectData<()>: std::fmt::Debug, downcast_rs::DowncastSync
dyn server::ObjectData<()>: std::fmt::Debug, Send, Sync, as_any::AsAny
);

// GlobalHandler
assert_impl!(
dyn server::GlobalHandler<()>: std::fmt::Debug, downcast_rs::DowncastSync
dyn server::GlobalHandler<()>: std::fmt::Debug, Send, Sync, as_any::AsAny
);

// ClientData
assert_impl!(
dyn server::ClientData: std::fmt::Debug, downcast_rs::DowncastSync
dyn server::ClientData: std::fmt::Debug, Send, Sync, as_any::AsAny
);

// ObjectId
Expand Down Expand Up @@ -150,7 +150,7 @@ mod client {
fn test_impls() {
// ObjectData
assert_impl!(
dyn client::ObjectData: std::fmt::Debug, downcast_rs::DowncastSync
dyn client::ObjectData: std::fmt::Debug, Send, Sync, as_any::AsAny
);

// ObjectId
Expand Down
2 changes: 1 addition & 1 deletion wayland-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ readme = "README.md"
[dependencies]
wayland-backend = { version = "0.3.0", path = "../wayland-backend" }
wayland-scanner = { version = "0.31.0", path = "../wayland-scanner" }
as-any = "0.3"
bitflags = "2"
log = { version = "0.4", optional = true }
nix = { version = "0.26.0", default-features = false }
downcast-rs = "1.2"
io-lifetimes = "2"

[package.metadata.docs.rs]
Expand Down
1 change: 1 addition & 0 deletions wayland-server/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl Client {
///
/// Returns [`None`] if the provided `Data` type parameter is not the correct one.
pub fn get_data<Data: ClientData + 'static>(&self) -> Option<&Data> {
use as_any::Downcast;
(*self.data).downcast_ref()
}

Expand Down

0 comments on commit 1824fc4

Please sign in to comment.