From 2be1f63a7c20228cff972730e41e4d08677db2ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 10 Mar 2022 09:56:51 +0200 Subject: [PATCH] glib: Allow borrowing a `ParamSpec` reference from a `Value` --- glib/src/param_spec.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/glib/src/param_spec.rs b/glib/src/param_spec.rs index 289edba12070..daabef009ba0 100644 --- a/glib/src/param_spec.rs +++ b/glib/src/param_spec.rs @@ -53,6 +53,23 @@ unsafe impl<'a> crate::value::FromValue<'a> for ParamSpec { } } +#[doc(hidden)] +unsafe impl<'a> crate::value::FromValue<'a> for &'a ParamSpec { + type Checker = crate::value::GenericValueTypeOrNoneChecker; + + unsafe fn from_value(value: &'a crate::Value) -> Self { + assert_eq!( + std::mem::size_of::(), + std::mem::size_of::() + ); + let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue); + let ptr = &value.data[0].v_pointer as *const crate::ffi::gpointer + as *const *const gobject_ffi::GParamSpec; + assert!(!(*ptr).is_null()); + &*(ptr as *const ParamSpec) + } +} + #[doc(hidden)] impl crate::value::ToValue for ParamSpec { fn to_value(&self) -> crate::Value { @@ -274,6 +291,19 @@ macro_rules! define_param_spec { } } + #[doc(hidden)] + unsafe impl<'a> crate::value::FromValue<'a> for &'a $rust_type { + type Checker = crate::value::GenericValueTypeOrNoneChecker; + + unsafe fn from_value(value: &'a crate::Value) -> Self { + assert_eq!(std::mem::size_of::(), std::mem::size_of::()); + let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue); + let ptr = &value.data[0].v_pointer as *const crate::ffi::gpointer as *const *const gobject_ffi::GParamSpec; + assert!(!(*ptr).is_null()); + &*(ptr as *const $rust_type) + } + } + #[doc(hidden)] impl crate::value::ToValue for $rust_type { fn to_value(&self) -> crate::Value {