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<'a, T> Deref for XSmartPointer<'a, T> {
23 type Target = T;
24
25 fn deref(&self) -> &T {
26 unsafe { &*self.ptr }
27 }
28}
29
30impl<'a, T> DerefMut for XSmartPointer<'a, T> {
31 fn deref_mut(&mut self) -> &mut T {
32 unsafe { &mut *self.ptr }
33 }
34}
35
36impl<'a, T> Drop for XSmartPointer<'a, T> {
37 fn drop(&mut self) {
38 unsafe {
39 (self.xconn.xlib.XFree)(self.ptr as *mut _);
40 }
41 }
42}
43