cogl/
fence.rs

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