wl_client/test_protocols/core/
wl_string.rs

1use {super::super::all_types::*, crate::builder::prelude::*};
2
3static INTERFACE: wl_interface = wl_interface {
4    name: c"wl_string".as_ptr(),
5    version: 1,
6    method_count: 0,
7    methods: ptr::null(),
8    event_count: 1,
9    events: {
10        static MESSAGES: [wl_message; 1] = [wl_message {
11            name: c"string".as_ptr(),
12            signature: c"s".as_ptr(),
13            types: {
14                static TYPES: [Option<&'static wl_interface>; 1] = [None];
15                TYPES.as_ptr().cast()
16            },
17        }];
18        MESSAGES.as_ptr()
19    },
20};
21
22/// An owned wl_string proxy.
23///
24/// See the documentation of [the module][self] for the interface description.
25#[derive(Clone, Eq, PartialEq)]
26#[repr(transparent)]
27pub struct WlString {
28    /// This proxy has the interface INTERFACE.
29    proxy: UntypedOwnedProxy,
30}
31
32/// A borrowed wl_string proxy.
33///
34/// See the documentation of [the module][self] for the interface description.
35#[derive(Eq, PartialEq)]
36#[repr(transparent)]
37pub struct WlStringRef {
38    /// This proxy has the interface INTERFACE.
39    proxy: UntypedBorrowedProxy,
40}
41
42// SAFETY: WlString is a transparent wrapper around UntypedOwnedProxy
43unsafe impl UntypedOwnedProxyWrapper for WlString {}
44
45// SAFETY: - INTERFACE is a valid wl_interface
46//         - The only invariant is that self.proxy has a compatible interface
47unsafe impl OwnedProxy for WlString {
48    const INTERFACE: &'static str = "wl_string";
49    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
50    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
51        private::EventHandler(private::NoOpEventHandler);
52    const MAX_VERSION: u32 = 1;
53
54    type Borrowed = WlStringRef;
55    type Api = private::ProxyApi;
56    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
57}
58
59// SAFETY: WlStringRef is a transparent wrapper around UntypedBorrowedProxy
60unsafe impl UntypedBorrowedProxyWrapper for WlStringRef {}
61
62// SAFETY: - The only invariant is that self.proxy has a compatible interface
63unsafe impl BorrowedProxy for WlStringRef {
64    type Owned = WlString;
65}
66
67impl Deref for WlString {
68    type Target = WlStringRef;
69
70    fn deref(&self) -> &Self::Target {
71        proxy::low_level::deref(self)
72    }
73}
74
75mod private {
76    pub struct ProxyApi;
77
78    #[allow(dead_code)]
79    pub struct EventHandler<H>(pub(super) H);
80
81    #[allow(dead_code)]
82    pub struct NoOpEventHandler;
83}
84
85impl Debug for WlString {
86    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
87        write!(f, "wl_string#{}", self.proxy.id())
88    }
89}
90
91impl Debug for WlStringRef {
92    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
93        write!(f, "wl_string#{}", self.proxy.id())
94    }
95}
96
97impl PartialEq<WlStringRef> for WlString {
98    fn eq(&self, other: &WlStringRef) -> bool {
99        self.proxy == other.proxy
100    }
101}
102
103impl PartialEq<WlString> for WlStringRef {
104    fn eq(&self, other: &WlString) -> bool {
105        self.proxy == other.proxy
106    }
107}
108
109impl WlString {
110    /// Since when the string event is available.
111    #[allow(dead_code)]
112    pub const EVT__STRING__SINCE: u32 = 1;
113}
114
115/// An event handler for [WlString] proxies.
116#[allow(dead_code)]
117pub trait WlStringEventHandler {
118    /// # Arguments
119    ///
120    /// - `string`:
121    #[inline]
122    fn string(&self, _slf: &WlStringRef, string: &str) {
123        let _ = string;
124    }
125}
126
127impl WlStringEventHandler for private::NoOpEventHandler {}
128
129// SAFETY: INTERFACE is a valid wl_interface
130unsafe impl<H> EventHandler for private::EventHandler<H>
131where
132    H: WlStringEventHandler,
133{
134    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
135
136    #[allow(unused_variables)]
137    unsafe fn handle_event(
138        &self,
139        queue: &Queue,
140        slf: &UntypedBorrowedProxy,
141        opcode: u32,
142        args: *mut wl_argument,
143    ) {
144        // SAFETY: This function required that slf has the interface INTERFACE
145        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlStringRef>(slf) };
146        match opcode {
147            0 => {
148                // SAFETY: INTERFACE requires that there are 1 arguments
149                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
150                // SAFETY: - INTERFACE requires that args[0] contains a string
151                //         - if the pointer is not null, then it is a c string
152                let arg0 = unsafe { convert_string_arg("wl_string", "string", args[0].s) };
153                self.0.string(slf, arg0);
154            }
155            _ => {
156                invalid_opcode("wl_string", opcode);
157            }
158        }
159    }
160}
161
162impl<H> CreateEventHandler<H> for private::ProxyApi
163where
164    H: WlStringEventHandler,
165{
166    type EventHandler = private::EventHandler<H>;
167
168    #[inline]
169    fn create_event_handler(handler: H) -> Self::EventHandler {
170        private::EventHandler(handler)
171    }
172}
173
174/// Functional event handlers.
175pub mod event_handlers {
176    use super::*;
177
178    /// Event handler for string events.
179    pub struct String<F>(F);
180    impl<F> WlStringEventHandler for String<F>
181    where
182        F: Fn(&WlStringRef, &str),
183    {
184        #[inline]
185        fn string(&self, _slf: &WlStringRef, string: &str) {
186            self.0(_slf, string)
187        }
188    }
189
190    impl WlString {
191        /// Creates an event handler for string events.
192        ///
193        /// The event handler ignores all other events.
194        #[allow(dead_code)]
195        pub fn on_string<F>(f: F) -> String<F>
196        where
197            F: Fn(&WlStringRef, &str),
198        {
199            String(f)
200        }
201    }
202}