Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit 0f529b8

Browse files
Merge pull request #114 from gkoz/value
Adjust ToValue implementation to support both Option<T> and Option<&T>
2 parents 2ae5469 + 3850a8d commit 0f529b8

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/value.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,12 @@ pub trait ToValue {
362362
fn to_value_type(&self) -> Type;
363363
}
364364

365-
impl<'a, T: ?Sized + SetValueOptional> ToValue for Option<&'a T> {
365+
impl<T: SetValueOptional> ToValue for Option<T> {
366366
fn to_value(&self) -> Value {
367367
unsafe {
368368
let mut ret = Value::uninitialized();
369369
gobject_ffi::g_value_init(ret.to_glib_none_mut().0, T::static_type().to_glib());
370-
T::set_value_optional(&mut ret, self.clone());
370+
T::set_value_optional(&mut ret, self.as_ref());
371371
ret
372372
}
373373
}
@@ -448,15 +448,15 @@ impl SetValueOptional for str {
448448
}
449449
}
450450

451-
impl<'a> SetValue for &'a str {
451+
impl<'a, T: ?Sized + SetValue> SetValue for &'a T {
452452
unsafe fn set_value(value: &mut Value, this: &Self) {
453-
gobject_ffi::g_value_take_string(value.to_glib_none_mut().0, this.to_glib_full())
453+
SetValue::set_value(value, *this)
454454
}
455455
}
456456

457-
impl<'a> SetValueOptional for &'a str {
457+
impl<'a, T: ?Sized + SetValueOptional> SetValueOptional for &'a T {
458458
unsafe fn set_value_optional(value: &mut Value, this: Option<&Self>) {
459-
gobject_ffi::g_value_take_string(value.to_glib_none_mut().0, this.to_glib_full())
459+
SetValueOptional::set_value_optional(value, this.map(|v| *v))
460460
}
461461
}
462462

@@ -491,12 +491,6 @@ impl<T: IsA<Object>> SetValueOptional for T {
491491
}
492492
}
493493

494-
impl<'a, T: IsA<Object>> SetValue for &'a T {
495-
unsafe fn set_value(value: &mut Value, this: &Self) {
496-
gobject_ffi::g_value_set_object(value.to_glib_none_mut().0, this.to_glib_none().0)
497-
}
498-
}
499-
500494
impl FromValueOptional for bool {
501495
unsafe fn from_value_optional(value: &Value) -> Option<Self> {
502496
Some(from_glib(gobject_ffi::g_value_get_boolean(value.to_glib_none().0)))

0 commit comments

Comments
 (0)