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