-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
[FEATURE REQUEST] TryFrom<u32> for glib::Enum #1662
Comments
@sdroege As discussed in Matrix. |
So, @fengalin Do you remember why |
Mh, while I can make a pull request to implement the change suggested in the description, I'm not sure I feel comfortable refactoring all of glib and probably Gir as well 😇 I can certainly try, but it'll take time and likely require quite a bit of hand holding 😬😅 |
Calling these conversions is We introduced From a Rust standpoint, dealing with an Would you take the risk of your program trusting a |
@fengalin I can literally just copy the I see your point about C enums crossing FFI boundaries, but this issue is about Rust enums with |
@fengalin What you write seems to apply to |
Imho, the introspected enums already have an unknown variant that holds any value that is > N. So what does unsafe serves here really? |
My point is about crossing FFI boundaries, this is why @slomo @bilelmoussaoui Sorry my English is poor. I was talking about ensuring the type conversion is appropriate regarding the API. In other words, the Quoting the doc:
|
Ah I see what you mean. But OTOH that's not very different from any other That our integers come via FFI does not really change anything here. It wouldn't be different if that integer comes via JSON from some web service. |
I don't remember all the details now (and I'm on my phone rn :)), but this trait interacts with other traits in |
How do we proceed? Replacing the FromGlib traits seems to require a bit of a discussion, so can I proceed to make a pull request to add a separate TryFrom as in the original description above, to at least address my immediate pain point? 😇 |
I would go with that for now, yes |
At least until someone has time to think the implications of the wider change through. |
OTOH you can as well just use |
No hurry, of course, but I think the change is pretty obvious, and it's cheap too, just a few lines in the derive macro. 🤷 But whatever you like, it's not like this is blocking anything. As you say, derive more exists 😊 |
Currently,
#[derive(glib::Enum)]
generates the following trait impls to convert the integer value of the enum back to the enum value itself:This is literally just a perfectly safe
TryFrom<u32>
except that it'sunsafe
becauseTryFromGlib
itself is unsafe, presumably because conversion from Glib is usually unsafe for non-trivial types because pointers and such.But since the conversion from
i32
to an enum value is perfectly safe, it'd be helpful ifglib::Enum
could first generate a safeTryFrom<i32>
and then createTryFromGlib
andFromGlib
on top of that, presumably for Gir compatibility. In other words, it'd be helpful ifglib::Enum
generated this instead:I'd be willing to make a pull request for this, if it's acceptable.
See original discussion in Matrix.
The text was updated successfully, but these errors were encountered: