1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! `IMPL` BoxedInline wrapper implementation.
5
6// rustdoc-stripper-ignore-next
7/// Wrapper implementations for BoxedInline types. See `wrapper!`.
8#[macro_export]
9macro_rules! glib_boxed_inline_wrapper {
10 ([$($attr:meta)*] $visibility:vis $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty
11 $(, @type_ $get_type_expr:expr)?) => {
12 $(#[$attr])*
13 #[repr(transparent)]
14 $visibility struct $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? {
15 pub(crate) inner: $ffi_name,
16 $(pub(crate) phantom: std::marker::PhantomData<$($generic),+>,)?
17 }
18
19 #[allow(clippy::incorrect_clone_impl_on_copy_type)]
20 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? std::clone::Clone for $name $(<$($generic),+>)? {
21 #[inline]
22 fn clone(&self) -> Self {
23 Self {
24 inner: std::clone::Clone::clone(&self.inner),
25 $(phantom: std::marker::PhantomData::<$($generic),+>)?
26 }
27 }
28 }
29
30 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? std::marker::Copy for $name $(<$($generic),+>)? {}
31
32 $crate::glib_boxed_inline_wrapper!(
33 @generic_impl $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)?, $ffi_name,
34 @copy ptr unsafe { let copy = $crate::ffi::g_malloc(std::mem::size_of::<$ffi_name>()) as *mut $ffi_name; std::ptr::copy_nonoverlapping(ptr, copy, 1); copy },
35 @free ptr unsafe { $crate::ffi::g_free(ptr as *mut _); },
36 @init _ptr (), @copy_into dest src std::ptr::copy_nonoverlapping(src, dest, 1), @clear _ptr ()
37 );
38
39 $crate::glib_boxed_inline_wrapper!(@value_impl $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)?, $ffi_name $(, @type_ $get_type_expr)?);
40 };
41
42 ([$($attr:meta)*] $visibility:vis $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty,
43 @copy $copy_arg:ident $copy_expr:expr, @free $free_arg:ident $free_expr:expr
44 $(, @type_ $get_type_expr:expr)?) => {
45 $(#[$attr])*
46 #[repr(transparent)]
47 $visibility struct $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? {
48 pub(crate) inner: $ffi_name,
49 $(pub(crate) phantom: std::marker::PhantomData<$($generic),+>,)?
50 }
51
52 #[allow(clippy::incorrect_clone_impl_on_copy_type)]
53 #[allow(clippy::non_canonical_clone_impl)]
54 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? std::clone::Clone for $name $(<$($generic),+>)? {
55 #[inline]
56 fn clone(&self) -> Self {
57 Self {
58 inner: std::clone::Clone::clone(&self.inner),
59 $(phantom: std::marker::PhantomData::<$($generic),+>)?
60 }
61 }
62 }
63
64 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? std::marker::Copy for $name $(<$($generic),+>)? {}
65
66 $crate::glib_boxed_inline_wrapper!(
67 @generic_impl $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)?, $ffi_name,
68 @copy $copy_arg $copy_expr, @free $free_arg $free_expr,
69 @init _ptr (), @copy_into dest src std::ptr::copy_nonoverlapping(src, dest, 1), @clear _ptr ()
70 );
71
72 $crate::glib_boxed_inline_wrapper!(@value_impl $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)?, $ffi_name $(, @type_ $get_type_expr)?);
73 };
74
75 ([$($attr:meta)*] $visibility:vis $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty,
76 @init $init_arg:ident $init_expr:expr, @copy_into $copy_into_arg_dest:ident $copy_into_arg_src:ident $copy_into_expr:expr, @clear $clear_arg:ident $clear_expr:expr
77 $(, @type_ $get_type_expr:expr)?) => {
78 $(#[$attr])*
79 #[repr(transparent)]
80 $visibility struct $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? {
81 pub(crate) inner: $ffi_name,
82 $(pub(crate) phantom: std::marker::PhantomData<$($generic),+>,)?
83 }
84
85 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? std::clone::Clone for $name $(<$($generic),+>)? {
86 #[inline]
87 fn clone(&self) -> Self {
88 unsafe {
89 $crate::translate::from_glib_none(&self.inner as *const $ffi_name)
90 }
91 }
92 }
93
94 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? Drop for $name $(<$($generic),+>)? {
95 #[inline]
96 fn drop(&mut self) {
97 unsafe {
98 let clear = |$clear_arg: *mut $ffi_name| $clear_expr;
99 clear(&mut self.inner as *mut $ffi_name);
100 }
101 }
102 }
103
104 $crate::glib_boxed_inline_wrapper!(
105 @generic_impl $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)?, $ffi_name,
106 @copy ptr unsafe { let copy = $crate::ffi::g_malloc(std::mem::size_of::<$ffi_name>()) as *mut $ffi_name; let c = |$copy_into_arg_dest, $copy_into_arg_src| $copy_into_expr; c(copy, ptr); copy },
107 @free ptr unsafe { let c = |$clear_arg| $clear_expr; c(ptr); $crate::ffi::g_free(ptr as *mut _); },
108 @init $init_arg $init_expr, @copy_into $copy_into_arg_dest $copy_into_arg_src $copy_into_expr, @clear $clear_arg $clear_expr
109 );
110
111 $crate::glib_boxed_inline_wrapper!(@value_impl $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)?, $ffi_name $(, @type_ $get_type_expr)?);
112 };
113
114
115 ([$($attr:meta)*] $visibility:vis $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty,
116 @copy $copy_arg:ident $copy_expr:expr, @free $free_arg:ident $free_expr:expr,
117 @init $init_arg:ident $init_expr:expr, @copy_into $copy_into_arg_dest:ident $copy_into_arg_src:ident $copy_into_expr:expr, @clear $clear_arg:ident $clear_expr:expr
118 $(, @type_ $get_type_expr:expr)?) => {
119 $(#[$attr])*
120 #[repr(transparent)]
121 $visibility struct $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? {
122 pub(crate) inner: $ffi_name,
123 $(pub(crate) phantom: std::marker::PhantomData<$($generic),+>,)?
124 }
125
126 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? std::clone::Clone for $name $(<$($generic),+>)? {
127 #[inline]
128 fn clone(&self) -> Self {
129 unsafe {
130 $crate::translate::from_glib_none(&self.inner as *const $ffi_name)
131 }
132 }
133 }
134
135 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? Drop for $name $(<$($generic),+>)? {
136 #[inline]
137 fn drop(&mut self) {
138 unsafe {
139 let clear = |$clear_arg: *mut $ffi_name| $clear_expr;
140 clear(&mut self.inner as *mut $ffi_name);
141 }
142 }
143 }
144
145 $crate::glib_boxed_inline_wrapper!(
146 @generic_impl $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)?, $ffi_name,
147 @copy $copy_arg $copy_expr, @free $free_arg $free_expr,
148 @init $init_arg $init_expr, @copy_into $copy_into_arg_dest $copy_into_arg_src $copy_into_expr, @clear $clear_arg $clear_expr
149 );
150
151 $crate::glib_boxed_inline_wrapper!(@value_impl $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)?, $ffi_name $(, @type_ $get_type_expr)?);
152 };
153
154 (@generic_impl $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty,
155 @copy $copy_arg:ident $copy_expr:expr, @free $free_arg:ident $free_expr:expr,
156 @init $init_arg:ident $init_expr:expr, @copy_into $copy_into_arg_dest:ident $copy_into_arg_src:ident $copy_into_expr:expr, @clear $clear_arg:ident $clear_expr:expr) => {
157
158 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $name $(<$($generic),+>)? {
159 #[inline]
160 pub fn as_ptr(&self) -> *mut $ffi_name {
161 &self.inner as *const $ffi_name as *mut _
162 }
163
164 #[doc = "Borrows the underlying C value."]
165 #[inline]
166 pub unsafe fn from_glib_ptr_borrow<'a>(ptr: *const $ffi_name) -> &'a Self {
167 &*(ptr as *const Self)
168 }
169
170 #[doc = "Borrows the underlying C value mutably."]
171 #[inline]
172 pub unsafe fn from_glib_ptr_borrow_mut<'a>(ptr: *mut $ffi_name) -> &'a mut Self {
173 &mut *(ptr as *mut Self)
174 }
175 }
176
177 #[doc(hidden)]
178 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::GlibPtrDefault for $name $(<$($generic),+>)? {
179 type GlibType = *mut $ffi_name;
180 }
181
182 #[doc(hidden)]
183 unsafe impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::TransparentType for $name $(<$($generic),+>)? {
184 type GlibType = $ffi_name;
185 }
186
187 #[doc(hidden)]
188 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::Uninitialized for $name $(<$($generic),+>)? {
189 #[inline]
190 unsafe fn uninitialized() -> Self {
191 let mut v = std::mem::MaybeUninit::zeroed();
192 let init = |$init_arg: *mut $ffi_name| $init_expr;
193 init(v.as_mut_ptr());
194 Self {
195 inner: v.assume_init(),
196 $(phantom: std::marker::PhantomData::<$($generic),+>)?
197 }
198 }
199 }
200
201 #[doc(hidden)]
202 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::UnsafeFrom<$ffi_name> for $name $(<$($generic),+>)? {
203 #[inline]
204 unsafe fn unsafe_from(t: $ffi_name) -> Self {
205 Self {
206 inner: t,
207 $(phantom: std::marker::PhantomData::<$($generic),+>)?
208 }
209 }
210 }
211
212 #[doc(hidden)]
213 impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtr<'a, *const $ffi_name> for $name $(<$($generic),+>)? {
214 type Storage = std::marker::PhantomData<&'a Self>;
215
216 #[inline]
217 fn to_glib_none(&'a self) -> $crate::translate::Stash<'a, *const $ffi_name, Self> {
218 $crate::translate::Stash(&self.inner as *const $ffi_name, std::marker::PhantomData)
219 }
220
221 #[inline]
222 fn to_glib_full(&self) -> *const $ffi_name {
223 unsafe {
224 let copy = |$copy_arg: *const $ffi_name| $copy_expr;
225 copy(&self.inner as *const $ffi_name)
226 }
227 }
228 }
229
230 #[doc(hidden)]
231 impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtrMut<'a, *mut $ffi_name> for $name $(<$($generic),+>)? {
232 type Storage = std::marker::PhantomData<&'a mut Self>;
233
234 #[inline]
235 fn to_glib_none_mut(&'a mut self) -> $crate::translate::StashMut<'a, *mut $ffi_name, Self> {
236 let ptr = &mut self.inner as *mut $ffi_name;
237 $crate::translate::StashMut(ptr, std::marker::PhantomData)
238 }
239 }
240
241 #[doc(hidden)]
242 impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *mut *const $ffi_name> for $name $(<$($generic),+>)? {
243 type Storage = (std::marker::PhantomData<&'a [Self]>, Option<Vec<*const $ffi_name>>);
244
245 fn to_glib_none_from_slice(t: &'a [Self]) -> (*mut *const $ffi_name, Self::Storage) {
246 let mut v: Vec<_> = t.iter().map(|s| &s.inner as *const $ffi_name).collect();
247 v.push(std::ptr::null_mut() as *const $ffi_name);
248
249 (v.as_mut_ptr(), (std::marker::PhantomData, Some(v)))
250 }
251
252 fn to_glib_container_from_slice(t: &'a [Self]) -> (*mut *const $ffi_name, Self::Storage) {
253 let v_ptr = unsafe {
254 let v_ptr = $crate::ffi::g_malloc(std::mem::size_of::<*const $ffi_name>() * (t.len() + 1)) as *mut *const $ffi_name;
255
256 for (i, s) in t.iter().enumerate() {
257 std::ptr::write(v_ptr.add(i), &s.inner as *const $ffi_name);
258 }
259 std::ptr::write(v_ptr.add(t.len()), std::ptr::null_mut());
260
261 v_ptr
262 };
263
264 (v_ptr, (std::marker::PhantomData, None))
265 }
266
267 fn to_glib_full_from_slice(t: &[Self]) -> *mut *const $ffi_name {
268 unsafe {
269 let v_ptr = $crate::ffi::g_malloc(std::mem::size_of::<*const $ffi_name>() * (t.len() + 1)) as *mut *const $ffi_name;
270
271 for (i, s) in t.iter().enumerate() {
272 std::ptr::write(v_ptr.add(i), $crate::translate::ToGlibPtr::to_glib_full(s));
273 }
274 std::ptr::write(v_ptr.add(t.len()), std::ptr::null_mut());
275
276 v_ptr
277 }
278 }
279 }
280
281 #[doc(hidden)]
282 impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *const *const $ffi_name> for $name $(<$($generic),+>)? {
283 type Storage = (std::marker::PhantomData<&'a [Self]>, Option<Vec<*const $ffi_name>>);
284
285 #[inline]
286 fn to_glib_none_from_slice(t: &'a [Self]) -> (*const *const $ffi_name, Self::Storage) {
287 let (ptr, stash) = $crate::translate::ToGlibContainerFromSlice::<'a, *mut *const $ffi_name>::to_glib_none_from_slice(t);
288 (ptr as *const *const $ffi_name, stash)
289 }
290
291 fn to_glib_container_from_slice(_: &'a [Self]) -> (*const *const $ffi_name, Self::Storage) {
292 // Can't have consumer free a *const pointer
293 unimplemented!()
294 }
295
296 fn to_glib_full_from_slice(_: &[Self]) -> *const *const $ffi_name {
297 // Can't have consumer free a *const pointer
298 unimplemented!()
299 }
300 }
301
302 #[doc(hidden)]
303 impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *mut $ffi_name> for $name $(<$($generic),+>)? {
304 type Storage = std::marker::PhantomData<&'a [Self]>;
305
306 #[inline]
307 fn to_glib_none_from_slice(t: &'a [Self]) -> (*mut $ffi_name, Self::Storage) {
308 (t.as_ptr() as *mut $ffi_name, std::marker::PhantomData)
309 }
310
311 fn to_glib_container_from_slice(t: &'a [Self]) -> (*mut $ffi_name, Self::Storage) {
312 (
313 $crate::translate::ToGlibContainerFromSlice::<'a, *mut $ffi_name>::to_glib_full_from_slice(t),
314 std::marker::PhantomData,
315 )
316 }
317
318 fn to_glib_full_from_slice(t: &[Self]) -> *mut $ffi_name {
319 let v_ptr = unsafe {
320 let v_ptr = $crate::ffi::g_malloc(std::mem::size_of::<$ffi_name>()) as *mut $ffi_name;
321
322 for (i, s) in t.iter().enumerate() {
323 let copy_into = |$copy_into_arg_dest: *mut $ffi_name, $copy_into_arg_src: *const $ffi_name| $copy_into_expr;
324 copy_into(v_ptr.add(i), &s.inner as *const $ffi_name);
325 }
326
327 v_ptr
328 };
329
330 v_ptr
331 }
332 }
333
334 #[doc(hidden)]
335 impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *const $ffi_name> for $name $(<$($generic),+>)? {
336 type Storage = std::marker::PhantomData<&'a [Self]>;
337
338 #[inline]
339 fn to_glib_none_from_slice(t: &'a [Self]) -> (*const $ffi_name, Self::Storage) {
340 let (ptr, stash) = $crate::translate::ToGlibContainerFromSlice::<'a, *mut $ffi_name>::to_glib_none_from_slice(t);
341 (ptr as *const $ffi_name, stash)
342 }
343
344 fn to_glib_container_from_slice(_: &'a [Self]) -> (*const $ffi_name, Self::Storage) {
345 // Can't have consumer free a *const pointer
346 unimplemented!()
347 }
348
349 fn to_glib_full_from_slice(_: &[Self]) -> *const $ffi_name {
350 // Can't have consumer free a *const pointer
351 unimplemented!()
352 }
353 }
354
355 #[doc(hidden)]
356 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrNone<*mut $ffi_name> for $name $(<$($generic),+>)? {
357 #[inline]
358 unsafe fn from_glib_none(ptr: *mut $ffi_name) -> Self {
359 debug_assert!(!ptr.is_null());
360
361 let mut v = <Self as $crate::translate::Uninitialized>::uninitialized();
362 let copy_into = |$copy_into_arg_dest: *mut $ffi_name, $copy_into_arg_src: *const $ffi_name| $copy_into_expr;
363 copy_into(&mut v.inner as *mut $ffi_name, ptr as *const $ffi_name);
364
365 v
366 }
367 }
368
369 #[doc(hidden)]
370 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrNone<*const $ffi_name> for $name $(<$($generic),+>)? {
371 #[inline]
372 unsafe fn from_glib_none(ptr: *const $ffi_name) -> Self {
373 $crate::translate::from_glib_none::<_, Self>(ptr as *mut $ffi_name)
374 }
375 }
376
377 #[doc(hidden)]
378 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrFull<*mut $ffi_name> for $name $(<$($generic),+>)? {
379 #[inline]
380 unsafe fn from_glib_full(ptr: *mut $ffi_name) -> Self {
381 debug_assert!(!ptr.is_null());
382
383 let mut v = <Self as $crate::translate::Uninitialized>::uninitialized();
384 let copy_into = |$copy_into_arg_dest: *mut $ffi_name, $copy_into_arg_src: *const $ffi_name| $copy_into_expr;
385 copy_into(&mut v.inner as *mut $ffi_name, ptr as *const $ffi_name);
386
387 let free = |$free_arg: *mut $ffi_name| $free_expr;
388 free(ptr);
389
390 v
391 }
392 }
393
394 #[doc(hidden)]
395 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrFull<*const $ffi_name> for $name $(<$($generic),+>)? {
396 #[inline]
397 unsafe fn from_glib_full(ptr: *const $ffi_name) -> Self {
398 $crate::translate::from_glib_full::<_, Self>(ptr as *mut $ffi_name)
399 }
400 }
401
402 #[doc(hidden)]
403 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow<*mut $ffi_name> for $name $(<$($generic),+>)? {
404 #[inline]
405 unsafe fn from_glib_borrow(ptr: *mut $ffi_name) -> $crate::translate::Borrowed<Self> {
406 debug_assert!(!ptr.is_null());
407
408 $crate::translate::Borrowed::new(Self {
409 inner: std::ptr::read(ptr),
410 $(phantom: std::marker::PhantomData::<$($generic),+>)?
411 })
412 }
413 }
414
415 #[doc(hidden)]
416 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow<*const $ffi_name> for $name $(<$($generic),+>)? {
417 #[inline]
418 unsafe fn from_glib_borrow(ptr: *const $ffi_name) -> $crate::translate::Borrowed<Self> {
419 $crate::translate::from_glib_borrow::<_, Self>(ptr as *mut $ffi_name)
420 }
421 }
422
423 #[doc(hidden)]
424 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibContainerAsVec<*mut $ffi_name, *mut $ffi_name> for $name $(<$($generic),+>)? {
425 unsafe fn from_glib_none_num_as_vec(ptr: *mut $ffi_name, num: usize) -> Vec<Self> {
426 $crate::translate::FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr as *const _, num)
427 }
428
429 unsafe fn from_glib_container_num_as_vec(ptr: *mut $ffi_name, num: usize) -> Vec<Self> {
430 $crate::translate::FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr as *const _, num)
431 }
432
433 unsafe fn from_glib_full_num_as_vec(ptr: *mut $ffi_name, num: usize) -> Vec<Self> {
434 $crate::translate::FromGlibContainerAsVec::from_glib_full_num_as_vec(ptr as *const _, num)
435 }
436 }
437
438 #[doc(hidden)]
439 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibContainerAsVec<*mut $ffi_name, *const $ffi_name> for $name $(<$($generic),+>)? {
440 unsafe fn from_glib_none_num_as_vec(ptr: *const $ffi_name, num: usize) -> Vec<Self> {
441 if num == 0 || ptr.is_null() {
442 return Vec::new();
443 }
444
445 let mut res = Vec::<Self>::with_capacity(num);
446 let res_ptr = res.as_mut_ptr();
447 for i in 0..num {
448 ::std::ptr::write(res_ptr.add(i), $crate::translate::from_glib_none(ptr.add(i)));
449 }
450 res.set_len(num);
451 res
452 }
453
454 unsafe fn from_glib_container_num_as_vec(ptr: *const $ffi_name, num: usize) -> Vec<Self> {
455 let res = $crate::translate::FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
456 $crate::ffi::g_free(ptr as *mut _);
457 res
458 }
459
460 unsafe fn from_glib_full_num_as_vec(ptr: *const $ffi_name, num: usize) -> Vec<Self> {
461 if num == 0 || ptr.is_null() {
462 $crate::ffi::g_free(ptr as *mut _);
463 return Vec::new();
464 }
465
466 let mut res = Vec::with_capacity(num);
467 let res_ptr = res.as_mut_ptr();
468 ::std::ptr::copy_nonoverlapping(ptr as *mut Self, res_ptr, num);
469 res.set_len(num);
470 $crate::ffi::g_free(ptr as *mut _);
471 res
472 }
473 }
474
475 #[doc(hidden)]
476 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibContainerAsVec<*mut $ffi_name, *mut *mut $ffi_name> for $name $(<$($generic),+>)? {
477 unsafe fn from_glib_none_num_as_vec(ptr: *mut *mut $ffi_name, num: usize) -> Vec<Self> {
478 if num == 0 || ptr.is_null() {
479 return Vec::new();
480 }
481
482 let mut res = Vec::<Self>::with_capacity(num);
483 let res_ptr = res.as_mut_ptr();
484 for i in 0..num {
485 ::std::ptr::write(res_ptr.add(i), $crate::translate::from_glib_none(::std::ptr::read(ptr.add(i))));
486 }
487 res.set_len(num);
488 res
489 }
490
491 unsafe fn from_glib_container_num_as_vec(ptr: *mut *mut $ffi_name, num: usize) -> Vec<Self> {
492 let res = $crate::translate::FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
493 $crate::ffi::g_free(ptr as *mut _);
494 res
495 }
496
497 unsafe fn from_glib_full_num_as_vec(ptr: *mut *mut $ffi_name, num: usize) -> Vec<Self> {
498 if num == 0 || ptr.is_null() {
499 $crate::ffi::g_free(ptr as *mut _);
500 return Vec::new();
501 }
502
503 let mut res = Vec::<Self>::with_capacity(num);
504 let res_ptr = res.as_mut_ptr();
505 for i in 0..num {
506 ::std::ptr::write(res_ptr.add(i), $crate::translate::from_glib_full(::std::ptr::read(ptr.add(i))));
507 }
508 res.set_len(num);
509 $crate::ffi::g_free(ptr as *mut _);
510 res
511 }
512 }
513
514 #[doc(hidden)]
515 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrArrayContainerAsVec<*mut $ffi_name, *mut *mut $ffi_name> for $name $(<$($generic),+>)? {
516 unsafe fn from_glib_none_as_vec(ptr: *mut *mut $ffi_name) -> Vec<Self> {
517 $crate::translate::FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, $crate::translate::c_ptr_array_len(ptr))
518 }
519
520 unsafe fn from_glib_container_as_vec(ptr: *mut *mut $ffi_name) -> Vec<Self> {
521 $crate::translate::FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, $crate::translate::c_ptr_array_len(ptr))
522 }
523
524 unsafe fn from_glib_full_as_vec(ptr: *mut *mut $ffi_name) -> Vec<Self> {
525 $crate::translate::FromGlibContainerAsVec::from_glib_full_num_as_vec(ptr, $crate::translate::c_ptr_array_len(ptr))
526 }
527 }
528 #[doc(hidden)]
529 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*mut $ffi_name> for $name $(<$($generic),+>)? {
530 #[inline]
531 unsafe fn into_glib_ptr(self) -> *mut $ffi_name {
532 $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_full(&self) as *mut _
533 }
534 }
535
536 #[doc(hidden)]
537 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*const $ffi_name> for $name $(<$($generic),+>)? {
538 #[inline]
539 unsafe fn into_glib_ptr(self) -> *const $ffi_name {
540 $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_full(&self)
541 }
542 }
543
544 };
545
546 (@value_impl $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty) => { };
547
548 (@value_impl $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty, @type_ $get_type_expr:expr) => {
549 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::types::StaticType for $name $(<$($generic),+>)? {
550 #[inline]
551 fn static_type() -> $crate::types::Type {
552 #[allow(unused_unsafe)]
553 unsafe { $crate::translate::from_glib($get_type_expr) }
554 }
555 }
556
557 #[doc(hidden)]
558 impl$(<$($generic: 'static + $($bound $(+ $bound2)*)?),+>)? $crate::value::ValueType for $name $(<$($generic),+>)? {
559 type Type = Self;
560 }
561
562 #[doc(hidden)]
563 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::value::ValueTypeOptional for $name $(<$($generic),+>)? { }
564
565 #[doc(hidden)]
566 unsafe impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::value::FromValue<'_> for $name $(<$($generic),+>)? {
567 type Checker = $crate::value::GenericValueTypeOrNoneChecker<Self>;
568
569 #[inline]
570 unsafe fn from_value(value: &'_ $crate::Value) -> Self {
571 let ptr = $crate::gobject_ffi::g_value_get_boxed($crate::translate::ToGlibPtr::to_glib_none(value).0);
572 debug_assert!(!ptr.is_null());
573 <Self as $crate::translate::FromGlibPtrNone<*const $ffi_name>>::from_glib_none(ptr as *const $ffi_name)
574 }
575 }
576
577 #[doc(hidden)]
578 unsafe impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::value::FromValue<'_> for &'_ $name $(<$($generic),+>)? {
579 type Checker = $crate::value::GenericValueTypeOrNoneChecker<Self>;
580
581 #[inline]
582 unsafe fn from_value(value: &'_ $crate::Value) -> Self {
583 let ptr = $crate::gobject_ffi::g_value_get_boxed($crate::translate::ToGlibPtr::to_glib_none(value).0);
584 debug_assert!(!ptr.is_null());
585 &*(ptr as *const $ffi_name as *const $name $(<$($generic),+>)?)
586 }
587 }
588
589 #[doc(hidden)]
590 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::value::ToValue for $name $(<$($generic),+>)? {
591 #[inline]
592 fn to_value(&self) -> $crate::Value {
593 unsafe {
594 let mut value = $crate::Value::from_type_unchecked(<Self as $crate::StaticType>::static_type());
595 $crate::gobject_ffi::g_value_set_boxed(
596 $crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
597 $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(self).0 as *mut _,
598 );
599 value
600 }
601 }
602
603 #[inline]
604 fn value_type(&self) -> $crate::Type {
605 <Self as $crate::StaticType>::static_type()
606 }
607 }
608
609 #[doc(hidden)]
610 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? ::std::convert::From<$name $(<$($generic),+>)?> for $crate::Value {
611 #[inline]
612 fn from(v: $name $(<$($generic),+>)?) -> Self {
613 $crate::value::ToValue::to_value(&v)
614 }
615 }
616
617 #[doc(hidden)]
618 impl $(<$($generic: 'static + $($bound $(+ $bound2)*)?),+>)? $crate::value::ToValueOptional for $name $(<$($generic),+>)? {
619 #[inline]
620 fn to_value_optional(s: Option<&Self>) -> $crate::Value {
621 let mut value = $crate::Value::for_value_type::<Self>();
622 unsafe {
623 $crate::gobject_ffi::g_value_set_boxed(
624 $crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
625 $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&s).0 as *mut _,
626 );
627 }
628
629 value
630 }
631 }
632
633 impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::HasParamSpec for $name $(<$($generic),+>)? {
634 type ParamSpec = $crate::ParamSpecBoxed;
635 type SetValue = Self;
636 type BuilderFn = fn(&str) -> $crate::ParamSpecBoxedBuilder<Self>;
637
638 fn param_spec_builder() -> Self::BuilderFn {
639 |name| Self::ParamSpec::builder(name)
640 }
641 }
642 };
643}
644