Skip to content

Commit

Permalink
Merge pull request #597 from sdroege/paramspec-from-value-reference
Browse files Browse the repository at this point in the history
glib: Allow borrowing a `ParamSpec` reference from a `Value`
  • Loading branch information
sdroege authored Mar 10, 2022
2 parents 706f213 + 2be1f63 commit 16a07a4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions glib/src/param_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>;

unsafe fn from_value(value: &'a crate::Value) -> Self {
assert_eq!(
std::mem::size_of::<Self>(),
std::mem::size_of::<crate::ffi::gpointer>()
);
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 {
Expand Down Expand Up @@ -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<Self>;

unsafe fn from_value(value: &'a crate::Value) -> Self {
assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<crate::ffi::gpointer>());
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 {
Expand Down

0 comments on commit 16a07a4

Please sign in to comment.