Skip to content

Commit 2be1f63

Browse files
committed
glib: Allow borrowing a ParamSpec reference from a Value
1 parent 95a43a2 commit 2be1f63

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

glib/src/param_spec.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ unsafe impl<'a> crate::value::FromValue<'a> for ParamSpec {
5353
}
5454
}
5555

56+
#[doc(hidden)]
57+
unsafe impl<'a> crate::value::FromValue<'a> for &'a ParamSpec {
58+
type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;
59+
60+
unsafe fn from_value(value: &'a crate::Value) -> Self {
61+
assert_eq!(
62+
std::mem::size_of::<Self>(),
63+
std::mem::size_of::<crate::ffi::gpointer>()
64+
);
65+
let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue);
66+
let ptr = &value.data[0].v_pointer as *const crate::ffi::gpointer
67+
as *const *const gobject_ffi::GParamSpec;
68+
assert!(!(*ptr).is_null());
69+
&*(ptr as *const ParamSpec)
70+
}
71+
}
72+
5673
#[doc(hidden)]
5774
impl crate::value::ToValue for ParamSpec {
5875
fn to_value(&self) -> crate::Value {
@@ -274,6 +291,19 @@ macro_rules! define_param_spec {
274291
}
275292
}
276293

294+
#[doc(hidden)]
295+
unsafe impl<'a> crate::value::FromValue<'a> for &'a $rust_type {
296+
type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;
297+
298+
unsafe fn from_value(value: &'a crate::Value) -> Self {
299+
assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<crate::ffi::gpointer>());
300+
let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue);
301+
let ptr = &value.data[0].v_pointer as *const crate::ffi::gpointer as *const *const gobject_ffi::GParamSpec;
302+
assert!(!(*ptr).is_null());
303+
&*(ptr as *const $rust_type)
304+
}
305+
}
306+
277307
#[doc(hidden)]
278308
impl crate::value::ToValue for $rust_type {
279309
fn to_value(&self) -> crate::Value {

0 commit comments

Comments
 (0)