-
Notifications
You must be signed in to change notification settings - Fork 195
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
Use NonZeroU32::new_unchecked
to convert wasi error
#233
Conversation
Thanks for opening this. I think it's fine to leave this as a call to It might be worth adding/chaining a comment explaining why it is OK to call |
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.
At the time I thought that having unwrap
in the error conversion path would be fine since compiler should be able to remove the panic completely, but according to godbolt surprisingly it's not the case today. Ideally we would simply use the Into
impl, but it would require bumping MSRV to 1.41. Can you please add a respective TODO comment?
Also I think it could be worth to join the unsafe
s into one:
unsafe {
random_get(dest.as_mut_ptr(), dest.len())
.map_err(|e| NonZeroU32::new_unchecked(e.raw_error() as u32).into())
}
@newpavlov do you thing the unsafe code is worth the "cost" of a not-taken jump instruction? It's always going to be predicted correctly regardless. There's also not a guarantee in the I think the better solution is for |
Considering that the conversion happens only for errors, which are already extremely rare, I am not concerned about runtime efficiency in this case at all. But I am not fond of having clearly redundant and easily removable panic paths in compiled results.
IIRC it's guaranteed by WASI docs, i.e. it specifies that zero return code always represents "success". I agree that ideally we would use a method returning |
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.
IIRC it's guaranteed by WASI docs, i.e. it specifies that zero return code always represents "success". I agree that ideally we would use a method returning
NonZeroU16
, but unfortunately it does not exist currently. We could use the rawrandom_get
directly, but I don't think it's worth the trouble and it may result in additional headache when hypotheticalwasi_snapshot_preview2
is released.
Sounds reasonable, I agree that using the underlying raw call is not worth it. Approved modulo @newpavlov's suggested changes.
Should I open a PR for Edit: specifically, is there any reason not to? |
I think it would be a good addition to |
// will never return 0 | ||
unsafe { NonZeroU32::new_unchecked(e.raw_error() as u32) }.into() | ||
}) | ||
unsafe { |
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 had no idea unsafe
blocks propagated into closures
I'll go make a (*since it's auto-generated, it's not just as simple as fork->implement->PR, so I'll open an issue for it instead) |
The check in
NonZeroU32::new
isn't needed, aswasi::Error
isNonZeroU16
internally, sowasi::Error::raw_error
will never return zero