Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions wayland-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,31 @@ pub trait Proxy: Clone + std::fmt::Debug + Sized {
}
}

/// Trait for instances of [`Proxy`] that have destructors
pub trait ProxyDestroy: Proxy {
/// Sends the destructor request to the proxy, which is usually called `.destroy()`
fn call_proxy_destructor(&self);
}

/// Wrap a [`ProxyDestroy`] instance to automatically call the destructor on
/// drop.
#[derive(Debug, Eq, PartialEq, Hash)]
pub struct DestroyOnDrop<T: ProxyDestroy>(T);

impl<T: ProxyDestroy> Drop for DestroyOnDrop<T> {
fn drop(&mut self) {
self.0.call_proxy_destructor();
}
}

impl<T: ProxyDestroy> std::ops::Deref for DestroyOnDrop<T> {
type Target = T;

fn deref(&self) -> &T {
&self.0
}
}

/// Wayland dispatching error
#[derive(Debug)]
pub enum DispatchError {
Expand Down
22 changes: 22 additions & 0 deletions wayland-scanner/src/client_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ fn generate_objects_for(interface: &Interface) -> TokenStream {
};
let doc_attr = to_doc_attr(&docs);

let destructor_trait_impl = if let Some(destructor) =
interface.requests.iter().find(|msg| msg.typ == Some(Type::Destructor))
{
// TODO split off shared code into function
let method_name = format_ident!(
"{}{}",
if is_keyword(&destructor.name) { "_" } else { "" },
destructor.name
);
quote! {
impl super::wayland_client::ProxyDestroy for #iface_name {
fn call_proxy_destructor(&self) {
self.#method_name();
}
}
}
} else {
quote! {}
};

quote! {
#mod_doc
pub mod #mod_name {
Expand Down Expand Up @@ -173,6 +193,8 @@ fn generate_objects_for(interface: &Interface) -> TokenStream {
}
}

#destructor_trait_impl

impl #iface_name {
#methods
}
Expand Down
Loading