-
-
Notifications
You must be signed in to change notification settings - Fork 61
add To/FromValue trait to VariantType #340
Conversation
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.
Looks good but incomplete. Please add both directions for both types :)
src/variant_type.rs
Outdated
|
||
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) |
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.
Shouldn't this be set_boxed
? The rust part takes a reference, not ownership of the variant ty
src/variant_type.rs
Outdated
@@ -2,6 +2,9 @@ | |||
// 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}; |
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.
Unneeded {}
src/variant_type.rs
Outdated
|
||
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) |
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.
Is NULL a valid VariantTy?
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.
Probably not. I guess NULL would be "*"
?
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.
Needs to be checked in the GLib code, in the implementation of the boxed<->variantty code. Will check tomorrow if nobody else is faster :)
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.
It goes into g_variant_type_copy()
, which says that it must not be NULL
. So no SetValueOptional
for both and you can implement FromValue
for both
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.
So needed: StaticType
, FromValueOptional
, FromValue
and SetValue
for both types
src/variant_type.rs
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
assert for NULL here as it's not allowed
src/variant_type.rs
Outdated
|
||
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; |
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()
src/variant_type.rs
Outdated
|
||
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 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()
}
}
How come a VariantType can't be null? This test seems to tell otherwise? https://gitlab.gnome.org/GNOME/glib/blob/master/gio/tests/actions.c |
Inside That might be a bug on the GLib side though. Do you need it to be |
src/variant_type.rs
Outdated
Some(VariantTy::from_ptr(cvty)) | ||
} | ||
let cvty = gobject_ffi::g_value_get_boxed(value.to_glib_none().0) as *mut glib_ffi::GVariantType; | ||
assert!(cvty.is_null()); |
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.
This is missing a negation. You want to assert that it is not NULL
Not necessarily. Only for aping SimpleAction |
How does it work in |
The docs of SimpleAction state:
I've never used GAction before, so I'm not sure what that means. And my rust version of SimpleAction is still panicking somewhere without a clear trace... |
More from the docs
|
That hints at a bug in GLib then, that clearly says that |
From what I can tell, it is not so much GValueType that is not nullable, rather the type string itself? |
I'm not sure what you mean with this. |
Looking at the implementation of
There's no assertion here. It will just return another null pointer when you provide it a null pointer. |
This is an assertion (it will return |
Are you sure? According to the docs:
It will return So in this case, it will return |
It's only to be used as a guard against programming mistakes. It's an assertion, but not taking down your application unless yet set |
Ah, I though that was just a shorthand. So yeah, this is a bug in glib then. |
Lovely :) Not sure what we can do about that, need a workaround until a new GLib version arrives. But your code making it nullable is correct then, just GLib isn't correct. |
I would suggest to file a bug there and go ahead with your implementation. It will not cause unsafety, it only causes ugly warnings on the terminal and those are GLib's problem |
src/variant_type.rs
Outdated
} | ||
} | ||
|
||
// impl SetValueOptional for VariantType { |
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 can include this one now :)
Looks good to me, but please also link to the GLib bug report about this here for future reference |
I'll create the glib bug report soon. |
@GuillaumeGomez Let's get this in then? |
Yes! Thanks @vhdirk! |
This PR enables wrapping a
VariantTy(pe)
inValue