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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9fd66f7
add To/FromValue trait to VariantType
dvhaeren 2c1876a
fix ownership of conversion & minor cleanup
dvhaeren 030d2d0
add FromValue conversion for VariantTy
dvhaeren 56fcf7e
fix memory issues
dvhaeren 925ac35
invert assertion & add simple test for VariantTy
dvhaeren 4735425
varianttype can actually be NULL
dvhaeren File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -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. | ||
/// | ||
|
@@ -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; | ||
if cvty.is_null() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be See also this comment in the code from 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 { | ||
|
@@ -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); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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()