wl_client/proxy/low_level.rs
1//! Low-level proxy APIs.
2//!
3//! This module contains low-level APIs that are usually only used by `wl-client-builder` in
4//! generated protocol bindings.
5
6use crate::ffi::wl_proxy;
7use crate::proxy::BorrowedProxy;
8use crate::proxy::OwnedProxy;
9use crate::proxy::get_owned;
10pub use borrowed::UntypedBorrowedProxy;
11pub use borrowed::UntypedBorrowedProxyWrapper;
12pub use owned::EventHandler;
13pub(crate) use owned::OwnedProxyRegistry;
14pub use owned::UntypedOwnedProxy;
15pub use owned::UntypedOwnedProxyWrapper;
16pub(crate) use owned::destruction::ProxyDataDestruction;
17use std::mem;
18use std::mem::ManuallyDrop;
19use std::ptr::NonNull;
20
21pub(super) mod borrowed;
22pub(crate) mod owned;
23#[cfg(test)]
24mod tests;
25
26/// A type that can create an event handler.
27///
28/// This type is usually implemented by [`OwnedProxy::Api`] to turn an object implementing
29/// safe event callbacks into an event handler that can handle raw libwayland events.
30///
31/// This type is usually implemented by bindings that are automatically generated with the
32/// `wl-client-builder` crate.
33pub trait CreateEventHandler<T> {
34 type EventHandler: EventHandler;
35
36 /// Creates a new event handler.
37 fn create_event_handler(handler: T) -> Self::EventHandler;
38}
39
40#[inline]
41pub(crate) fn check_dispatching_proxy(proxy: Option<NonNull<wl_proxy>>) -> NonNull<wl_proxy> {
42 match proxy {
43 None => {
44 #[cold]
45 fn not_null() -> ! {
46 panic!("Proxy has already been destroyed");
47 }
48 not_null();
49 }
50 Some(p) => p,
51 }
52}
53
54#[inline]
55pub(crate) fn check_new_proxy(proxy: *mut wl_proxy) -> NonNull<wl_proxy> {
56 if let Some(proxy) = NonNull::new(proxy) {
57 proxy
58 } else {
59 #[cold]
60 fn not_null() -> ! {
61 panic!("new wl_proxy is null");
62 }
63 not_null();
64 }
65}
66
67/// Returns the borrowed version of an owned proxy.
68///
69/// This is a low-level API that you probably don't have to use unless you are writing
70/// protocol wrappers by hand.
71///
72/// `wl-client-builder` uses this function to implement the `Deref` trait.
73///
74/// # Example
75///
76/// ```
77/// # use wl_client::{proxy, Libwayland};
78/// # use wl_client::test_protocols::core::wl_display::{WlDisplay, WlDisplayRef};
79/// #
80/// let lib = Libwayland::open().unwrap();
81/// let con = lib.connect_to_default_display().unwrap();
82/// let queue = con.create_queue(c"queue name");
83///
84/// let display: WlDisplay = queue.display();
85/// let display_ref: &WlDisplayRef = proxy::low_level::deref(&display);
86/// ```
87pub fn deref<P>(proxy: &P) -> &P::Borrowed
88where
89 P: OwnedProxy,
90{
91 let borrowed: &UntypedBorrowedProxy = get_owned(proxy);
92 // SAFETY: borrowed has the interface P:WL_INTERFACE = P::Borrowed::Owned::WL_INTERFACE
93 unsafe { from_untyped_borrowed(borrowed) }
94}
95
96/// Creates a well-typed, borrowed proxy.
97///
98/// This is a low-level API that you probably don't have to use unless you are writing
99/// protocol wrappers by hand.
100///
101/// # Safety
102///
103/// - The proxy must have an interface compatible with `P::Owned::WL_INTERFACE`.
104#[inline]
105pub unsafe fn from_untyped_borrowed<P>(proxy: &UntypedBorrowedProxy) -> &P
106where
107 P: BorrowedProxy,
108{
109 // SAFETY: - Since the interface of the proxy is compatible with
110 // P::Owned::WL_INTERFACE, BorrowedProxy requires that this transmute is
111 // safe.
112 unsafe { mem::transmute::<&UntypedBorrowedProxy, &P>(proxy) }
113}
114
115/// Creates a well-typed, owned proxy.
116///
117/// This is a low-level API that you probably don't have to use unless you are writing
118/// protocol wrappers by hand.
119///
120/// # Safety
121///
122/// - The proxy must have an interface compatible with `P::WL_INTERFACE`.
123#[inline]
124pub unsafe fn from_untyped_owned<P>(proxy: UntypedOwnedProxy) -> P
125where
126 P: OwnedProxy,
127{
128 let proxy = ManuallyDrop::new(proxy);
129 // SAFETY: - Since the interface of the proxy is compatible with P::WL_INTERFACE,
130 // OwnedProxy requires that this transmute is safe.
131 unsafe { mem::transmute_copy::<UntypedOwnedProxy, P>(&*proxy) }
132}