glib: add To/FromValue trait to VariantType
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
+use libc::{c_void};
+use types::Type;
+use types::StaticType;
use ffi as glib_ffi;
use translate::*;
use std::borrow::{Borrow, Cow, ToOwned};
@@ -10,6 +13,8 @@ use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::slice;
+use value::{Value, ToValue, SetValue, SetValueOptional, FromValueOptional};
+use gobject_ffi;
/// Describes `Variant` types.
///
@@ -208,6 +213,36 @@ impl ToOwned for VariantTy {
}
}
+impl StaticType for VariantTy {
+ fn static_type() -> Type {
+ unsafe { from_glib(glib_ffi::g_variant_type_get_gtype()) }
+ }
+}
+
+impl StaticType for VariantType {
+ fn static_type() -> Type {
+ unsafe { from_glib(glib_ffi::g_variant_type_get_gtype()) }
+ }
+}
+
+impl SetValue for VariantTy {
+ unsafe fn set_value(value: &mut Value, this: &Self) {
+ gobject_ffi::g_value_take_boxed(value.to_glib_none_mut().0, this.to_glib_none().0 as glib_ffi::gpointer)
+ }
+}
+
+impl SetValueOptional for VariantTy {
+ unsafe fn set_value_optional(value: &mut Value, this: Option<&Self>) {
+ gobject_ffi::g_value_take_boxed(value.to_glib_none_mut().0, this.to_glib_none().0 as glib_ffi::gpointer)
+ }
+}
+
+impl<'a> FromValueOptional<'a> for VariantType {
+ unsafe fn from_value_optional(value: &'a Value) -> Option<Self> {
+ Option::<VariantType>::from_glib_full(gobject_ffi::g_value_dup_boxed(value.to_glib_none().0) as *mut glib_ffi::GVariantType)
+ }
+}
+
impl PartialEq for VariantType {
#[inline]
fn eq(&self, other: &Self) -> bool {
@@ -346,4 +381,13 @@ mod tests {
assert!(equal(ty1.as_ptr(), ty2.as_ptr()));
}
}
+
+ #[test]
+ fn value() {
+ let ty1 = VariantType::new("*").unwrap();
+ let tyv = ty1.to_value();
+ let ty2 = tyv.get::<VariantType>().unwrap();
+ assert_eq!(ty1, ty2);
+ }
+
}
