-
-
Notifications
You must be signed in to change notification settings - Fork 62
translate: take advantage of Rust Option & Result types #718
Conversation
Looks like a nice improvement overall! |
Additional proposal: for signed to unsigned conversions, we could use |
Why do we have |
I guess that was implemented to handle the cases where the glib type was a signed so that negatives would indicate that the value is invalid (e.g. an error code in a return value) but the actual usable value was positive. |
I believe those can go away. I can't remember a single usage of that :) |
Good! :) |
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 to me apart from those comments, thanks!
I'm considering removing the (More context on the above: initially, I thought of For the
What do you think? |
Sounds good to me but @GuillaumeGomez will probably be unhappy if you add |
In that case, I can make a new |
I confirm: not happy! :< (but debatable depending on the amount of required code) |
Ok! I'll push an update shortly with a local |
No need for I'll update gir and take a look at the affected crates tomorrow: Pango & GTK showed up from a rapid check. |
This commit introduces OptionToGlib and TryFromGlib to help with translations from and to Glib types which encode a value with a sentinel to indicate a None value and/or for which some values are invalid. These traits allow auto-implementing Option<T>::to_glib, Option<T>::from_glib or Result::<Option<T>, _>::from_glib from dependent crates when appropriate despite the orphan rule.
Hey @fengalin ! We migrated the glib crate into gtk-rs/gtk-rs repository. Could you please reopen your PR there? :) |
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 to me otherwise
Transferred to gtk-rs/gtk3-rs#11 |
This commit introduces
OptionToGlib
andTryFromGlib
to help with translations from and toGlib
types which encode a value with a sentinel to indicate an undefined value and/or for which some values are invalid.These traits allow auto-implementing
Option<T>::to_glib
,Option<T>::from_glib
orResult::<Option<T>, _>::from_glib
from dependent crates when appropriate despite the orphan rule.Required for https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/issues/234.
Note: I removed the existing fallible implementations of
FromGlib
forOption<T>
(e.g.i32
tou32
). I believe we should now use the auto-implementedResult<T, InvalidError>
and update the caller where appropriate instead of silently returningNone
, which is not the same IMHO. See also the comment forimpl FromGlib<u32> for char
. If you agree, I'll update code in the affected crates in thegtk-rs
andgstreamer
organizations. Meanwhile, CI will fail.