Skip to content

Commit c0e2ac9

Browse files
authored
Merge pull request #973 from sdroege/value-array-pspec
glib: Implement `ValueArray` `Value` traits manually because of the c…
2 parents d6571fc + 0f96733 commit c0e2ac9

File tree

1 file changed

+115
-2
lines changed

1 file changed

+115
-2
lines changed

glib/src/value_array.rs

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
use std::{cmp::Ordering, ops, slice};
44

5-
use crate::{translate::*, Value};
5+
use crate::{
6+
prelude::*,
7+
translate::*,
8+
value::{
9+
FromValue, GenericValueTypeOrNoneChecker, ToValueOptional, ValueType, ValueTypeOptional,
10+
},
11+
HasParamSpec, ParamSpecValueArray, ParamSpecValueArrayBuilder, Type, Value,
12+
};
613

714
wrapper! {
815
#[derive(Debug)]
@@ -12,7 +19,6 @@ wrapper! {
1219
match fn {
1320
copy => |ptr| gobject_ffi::g_value_array_copy(mut_override(ptr)),
1421
free => |ptr| gobject_ffi::g_value_array_free(ptr),
15-
type_ => || gobject_ffi::g_value_array_get_type(),
1622
}
1723
}
1824

@@ -136,3 +142,110 @@ impl ops::DerefMut for ValueArray {
136142
}
137143
}
138144
}
145+
146+
// Implementing `Value` traits manually because of a custom ParamSpec
147+
impl StaticType for ValueArray {
148+
#[inline]
149+
fn static_type() -> Type {
150+
unsafe { from_glib(gobject_ffi::g_value_array_get_type()) }
151+
}
152+
}
153+
154+
#[doc(hidden)]
155+
impl ValueType for ValueArray {
156+
type Type = Self;
157+
}
158+
159+
#[doc(hidden)]
160+
impl ValueTypeOptional for ValueArray {}
161+
162+
#[doc(hidden)]
163+
unsafe impl<'a> FromValue<'a> for ValueArray {
164+
type Checker = GenericValueTypeOrNoneChecker<Self>;
165+
166+
#[inline]
167+
unsafe fn from_value(value: &'a Value) -> Self {
168+
let ptr = gobject_ffi::g_value_dup_boxed(value.to_glib_none().0);
169+
debug_assert!(!ptr.is_null());
170+
from_glib_full(ptr as *mut gobject_ffi::GValueArray)
171+
}
172+
}
173+
174+
#[doc(hidden)]
175+
unsafe impl<'a> FromValue<'a> for &'a ValueArray {
176+
type Checker = GenericValueTypeOrNoneChecker<Self>;
177+
178+
#[inline]
179+
unsafe fn from_value(value: &'a Value) -> Self {
180+
debug_assert_eq!(
181+
std::mem::size_of::<Self>(),
182+
std::mem::size_of::<ffi::gpointer>()
183+
);
184+
let value = &*(value as *const Value as *const gobject_ffi::GValue);
185+
debug_assert!(!value.data[0].v_pointer.is_null());
186+
<ValueArray>::from_glib_ptr_borrow(
187+
&value.data[0].v_pointer as *const ffi::gpointer
188+
as *const *const gobject_ffi::GValueArray,
189+
)
190+
}
191+
}
192+
193+
#[doc(hidden)]
194+
impl ToValue for ValueArray {
195+
#[inline]
196+
fn to_value(&self) -> Value {
197+
unsafe {
198+
let mut value = Value::from_type_unchecked(<Self as StaticType>::static_type());
199+
gobject_ffi::g_value_take_boxed(
200+
value.to_glib_none_mut().0,
201+
ToGlibPtr::<*mut gobject_ffi::GValueArray>::to_glib_full(self) as *mut _,
202+
);
203+
value
204+
}
205+
}
206+
207+
#[inline]
208+
fn value_type(&self) -> Type {
209+
<Self as StaticType>::static_type()
210+
}
211+
}
212+
213+
impl std::convert::From<ValueArray> for Value {
214+
#[inline]
215+
fn from(o: ValueArray) -> Self {
216+
unsafe {
217+
let mut value = Value::from_type_unchecked(<ValueArray as StaticType>::static_type());
218+
gobject_ffi::g_value_take_boxed(
219+
value.to_glib_none_mut().0,
220+
IntoGlibPtr::<*mut gobject_ffi::GValueArray>::into_glib_ptr(o) as *mut _,
221+
);
222+
value
223+
}
224+
}
225+
}
226+
227+
#[doc(hidden)]
228+
impl ToValueOptional for ValueArray {
229+
#[inline]
230+
fn to_value_optional(s: Option<&Self>) -> Value {
231+
let mut value = Value::for_value_type::<Self>();
232+
unsafe {
233+
gobject_ffi::g_value_take_boxed(
234+
value.to_glib_none_mut().0,
235+
ToGlibPtr::<*mut gobject_ffi::GValueArray>::to_glib_full(&s) as *mut _,
236+
);
237+
}
238+
239+
value
240+
}
241+
}
242+
243+
impl HasParamSpec for ValueArray {
244+
type ParamSpec = ParamSpecValueArray;
245+
type SetValue = Self;
246+
type BuilderFn = fn(&str) -> ParamSpecValueArrayBuilder;
247+
248+
fn param_spec_builder() -> Self::BuilderFn {
249+
Self::ParamSpec::builder
250+
}
251+
}

0 commit comments

Comments
 (0)