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

add To/FromValue trait to VariantType #340

Merged
merged 6 commits into from Jun 22, 2018
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/variant_type.rs
Expand Up @@ -2,6 +2,8 @@
// 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 types::Type;
use types::StaticType;
use ffi as glib_ffi;
use translate::*;
use std::borrow::{Borrow, Cow, ToOwned};
Expand All @@ -10,6 +12,8 @@ use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::slice;
use value::{Value, ToValue, SetValue, FromValueOptional};
use gobject_ffi;

/// Describes `Variant` types.
///
Expand Down Expand Up @@ -208,6 +212,47 @@ impl ToOwned for VariantTy {
}
}

impl StaticType for VariantTy {
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_set_boxed(value.to_glib_none_mut().0, this.to_glib_none().0 as glib_ffi::gpointer)
}
}

impl<'a> FromValueOptional<'a> for &'a VariantTy {
unsafe fn from_value_optional(value: &'a Value) -> Option<Self> {
let cvty = gobject_ffi::g_value_dup_boxed(value.to_glib_none().0) as *mut glib_ffi::GVariantType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're leaking the memory here, this has to be get_boxed()

if cvty.is_null() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert for NULL here as it's not allowed

None
} else {
Some(VariantTy::from_ptr(cvty))
}
}
}

impl StaticType for VariantType {
fn static_type() -> Type {
unsafe { from_glib(glib_ffi::g_variant_type_get_gtype()) }
}
}

impl SetValue for VariantType {
unsafe fn set_value(value: &mut Value, this: &Self) {
gobject_ffi::g_value_set_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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be from_glib_none() and get_boxed().

See also this comment in the code from from_glib_full:

impl FromGlibPtrFull<*const glib_ffi::GVariantType> for VariantType {
    unsafe fn from_glib_full(ptr: *const glib_ffi::GVariantType) -> VariantType {
        // Don't assume ownership of a const pointer.
        // A transfer: full annotation on a `const GVariantType*` is likely a bug.
        VariantTy::from_ptr(ptr).to_owned()
    }
}

}
}

impl PartialEq for VariantType {
#[inline]
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -346,4 +391,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);
}

}