winit/platform_impl/linux/x11/util/
memory.rs

1use std::ops::{Deref, DerefMut};
2
3use super::*;
4
5pub(crate) struct XSmartPointer<'a, T> {
6    xconn: &'a XConnection,
7    pub ptr: *mut T,
8}
9
10impl<'a, T> XSmartPointer<'a, T> {
11    // You're responsible for only passing things to this that should be XFree'd.
12    // Returns None if ptr is null.
13    pub fn new(xconn: &'a XConnection, ptr: *mut T) -> Option<Self> {
14        if !ptr.is_null() {
15            Some(XSmartPointer { xconn, ptr })
16        } else {
17            None
18        }
19    }
20}
21
22impl<T> Deref for XSmartPointer<'_, T> {
23    type Target = T;
24
25    fn deref(&self) -> &T {
26        unsafe { &*self.ptr }
27    }
28}
29
30impl<T> DerefMut for XSmartPointer<'_, T> {
31    fn deref_mut(&mut self) -> &mut T {
32        unsafe { &mut *self.ptr }
33    }
34}
35
36impl<T> Drop for XSmartPointer<'_, T> {
37    fn drop(&mut self) {
38        unsafe {
39            (self.xconn.xlib.XFree)(self.ptr as *mut _);
40        }
41    }
42}