-
-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamic type function variants for Variants and Values #519
Conversation
CC @cgwalters @lucab in case you have opinions |
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.
Generally speaking, these new methods looks useful to have 👍.
glib/src/variant.rs
Outdated
pub fn as_maybe(&self) -> Option<Variant> { | ||
assert!(self.type_().is_maybe()); | ||
|
||
self.try_child_value(0) |
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.
I may have misunderstood the logic, but I'm not exactly sure that this matches what the docstring says.
Let's maybe cover this method with a test to avoid any doubts?
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.
A maybe variant is a container with either 1 or 0 values, thus roughly the logic for this function is if self.n_children() == 0 { None } else { Some(self.child_value(0)) }
- Variant::try_child_value
already exists as a rough equivalent to rust's slice.get(0)
for this purpose, so it didn't seem worth duplicating the logic here.
However, I really should just use ffi::g_variant_get_maybe
instead, since it's clearer - it implements the exact same logic regardless.
Good point on adding a test for this method though, will do!
Explicit dynamic type variants of `Variant::array_from_iter`, `Variant::from_maybe`, `Variant::is`, `Value::is`, and `Value::transform`
Addresses the main part of #507 by providing explicit dynamic type variants of:
Variant::array_from_iter
->Variant::array_from_iter_with_type
(a mouthful but consistent with other existing functions like
from_data_with_type_trusted
)Variant::from_maybe
becomesVariant::from_none
andVariant::from_some
Variant::as_maybe
(panicking, should there be a
try_as_maybe
? should it return anOption<Option<Variant>>
to be more in line withVariant::as_variant
?)Variant::is
->Variant::is_type
(consider
is_of_type
instead?)Value::is
->Value::is_type
(ditto)Value::transform
->Value::transform_with_type
(consider
transform_into
/transform_to_type
instead?)