cogl/
handle.rs

1use glib::translate::*;
2use std::mem;
3use std::ptr;
4
5#[repr(C)]
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7pub struct Handle(ffi::CoglHandle);
8
9impl GlibPtrDefault for Handle {
10    type GlibType = ffi::CoglHandle;
11}
12
13#[doc(hidden)]
14impl Uninitialized for Handle {
15    #[inline]
16    unsafe fn uninitialized() -> Self {
17        mem::zeroed()
18    }
19}
20
21impl<'a> ToGlibPtr<'a, ffi::CoglHandle> for Handle {
22    type Storage = ();
23
24    #[inline]
25    fn to_glib_none(&self) -> Stash<'a, ffi::CoglHandle, Handle> {
26        Stash(self.0, ())
27    }
28}
29
30impl<'a> ToGlibPtrMut<'a, *mut ffi::CoglHandle> for Handle {
31    type Storage = ();
32
33    #[inline]
34    fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::CoglHandle, Handle> {
35        StashMut(&mut self.0, ())
36    }
37}
38
39impl<'a> ToGlibContainerFromSlice<'a, *mut ffi::CoglHandle> for &'a Handle {
40    type Storage = (
41        Vec<Stash<'a, ffi::CoglHandle, &'a Handle>>,
42        Option<Vec<ffi::CoglHandle>>,
43    );
44
45    fn to_glib_none_from_slice(t: &'a [&'a Handle]) -> (*mut ffi::CoglHandle, Self::Storage) {
46        let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect();
47        let mut v_ptr: Vec<_> = v.iter().map(|s| s.0).collect();
48        v_ptr.push(ptr::null_mut());
49
50        (v_ptr.as_ptr() as *mut ffi::CoglHandle, (v, Some(v_ptr)))
51    }
52
53    fn to_glib_container_from_slice(t: &'a [&'a Handle]) -> (*mut ffi::CoglHandle, Self::Storage) {
54        let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect();
55
56        let v_ptr = unsafe {
57            let v_ptr = glib_sys::g_malloc0(mem::size_of::<ffi::CoglHandle>() * (t.len() + 1))
58                as *mut ffi::CoglHandle;
59
60            for (i, s) in v.iter().enumerate() {
61                ptr::write(v_ptr.add(i), s.0);
62            }
63
64            v_ptr
65        };
66
67        (v_ptr, (v, None))
68    }
69
70    fn to_glib_full_from_slice(_: &[&'a Handle]) -> *mut ffi::CoglHandle {
71        unimplemented!()
72    }
73}
74
75impl<'a> ToGlibContainerFromSlice<'a, *const ffi::CoglHandle> for &'a Handle {
76    type Storage = (
77        Vec<Stash<'a, ffi::CoglHandle, &'a Handle>>,
78        Option<Vec<ffi::CoglHandle>>,
79    );
80
81    fn to_glib_none_from_slice(t: &'a [&'a Handle]) -> (*const ffi::CoglHandle, Self::Storage) {
82        let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect();
83        let mut v_ptr: Vec<_> = v.iter().map(|s| s.0).collect();
84        v_ptr.push(ptr::null_mut());
85
86        (v_ptr.as_ptr() as *const ffi::CoglHandle, (v, Some(v_ptr)))
87    }
88
89    fn to_glib_container_from_slice(
90        t: &'a [&'a Handle],
91    ) -> (*const ffi::CoglHandle, Self::Storage) {
92        let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect();
93
94        let v_ptr = unsafe {
95            let v_ptr = glib_sys::g_malloc0(mem::size_of::<ffi::CoglHandle>() * (t.len() + 1))
96                as *mut ffi::CoglHandle;
97
98            for (i, s) in v.iter().enumerate() {
99                ptr::write(v_ptr.add(i), s.0);
100            }
101
102            v_ptr as *const ffi::CoglHandle
103        };
104
105        (v_ptr, (v, None))
106    }
107
108    fn to_glib_full_from_slice(_: &[&'a Handle]) -> *const ffi::CoglHandle {
109        unimplemented!()
110    }
111}
112
113impl FromGlibPtrNone<ffi::CoglHandle> for Handle {
114    #[inline]
115    unsafe fn from_glib_none(ptr: ffi::CoglHandle) -> Handle {
116        Handle(ptr)
117    }
118}
119
120#[doc(hidden)]
121impl FromGlibPtrNone<*const ffi::CoglHandle> for Handle {
122    unsafe fn from_glib_none(ptr: *const ffi::CoglHandle) -> Self {
123        *(ptr as *const Handle)
124    }
125}
126
127#[doc(hidden)]
128impl FromGlibPtrNone<*mut ffi::CoglHandle> for Handle {
129    unsafe fn from_glib_none(ptr: *mut ffi::CoglHandle) -> Self {
130        *(ptr as *mut Handle)
131    }
132}
133
134impl FromGlibPtrBorrow<ffi::CoglHandle> for Handle {
135    #[inline]
136    unsafe fn from_glib_borrow(ptr: ffi::CoglHandle) -> glib::translate::Borrowed<Handle> {
137        glib::translate::Borrowed::new(Handle(ptr))
138    }
139}
140
141#[doc(hidden)]
142impl FromGlibPtrBorrow<*mut ffi::CoglHandle> for Handle {
143    unsafe fn from_glib_borrow(ptr: *mut ffi::CoglHandle) -> glib::translate::Borrowed<Self> {
144        glib::translate::Borrowed::new(*(ptr as *mut Handle))
145    }
146}
147
148#[doc(hidden)]
149impl FromGlibPtrBorrow<*const ffi::CoglHandle> for Handle {
150    unsafe fn from_glib_borrow(ptr: *const ffi::CoglHandle) -> glib::translate::Borrowed<Self> {
151        glib::translate::Borrowed::new(*(ptr as *const Handle))
152    }
153}
154
155impl FromGlibPtrFull<ffi::CoglHandle> for Handle {
156    #[inline]
157    unsafe fn from_glib_full(_: ffi::CoglHandle) -> Handle {
158        unimplemented!()
159    }
160}
161
162impl FromGlibContainerAsVec<ffi::CoglHandle, *mut ffi::CoglHandle> for Handle {
163    unsafe fn from_glib_none_num_as_vec(ptr: *mut ffi::CoglHandle, num: usize) -> Vec<Self> {
164        if num == 0 || ptr.is_null() {
165            return Vec::new();
166        }
167
168        let mut res = Vec::with_capacity(num);
169        for i in 0..num {
170            res.push(from_glib_none(ptr::read(ptr.add(i))));
171        }
172        res
173    }
174
175    unsafe fn from_glib_container_num_as_vec(ptr: *mut ffi::CoglHandle, num: usize) -> Vec<Self> {
176        let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
177        glib_sys::g_free(ptr as *mut _);
178        res
179    }
180
181    unsafe fn from_glib_full_num_as_vec(ptr: *mut ffi::CoglHandle, num: usize) -> Vec<Self> {
182        if num == 0 || ptr.is_null() {
183            return Vec::new();
184        }
185
186        let mut res = Vec::with_capacity(num);
187        for i in 0..num {
188            res.push(from_glib_full(ptr::read(ptr.add(i))));
189        }
190        glib_sys::g_free(ptr as *mut _);
191        res
192    }
193}
194
195impl FromGlibPtrArrayContainerAsVec<ffi::CoglHandle, *mut ffi::CoglHandle> for Handle {
196    unsafe fn from_glib_none_as_vec(ptr: *mut ffi::CoglHandle) -> Vec<Self> {
197        FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, c_ptr_array_len(ptr))
198    }
199
200    unsafe fn from_glib_container_as_vec(ptr: *mut ffi::CoglHandle) -> Vec<Self> {
201        FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, c_ptr_array_len(ptr))
202    }
203
204    unsafe fn from_glib_full_as_vec(ptr: *mut ffi::CoglHandle) -> Vec<Self> {
205        FromGlibContainerAsVec::from_glib_full_num_as_vec(ptr, c_ptr_array_len(ptr))
206    }
207}