-
Notifications
You must be signed in to change notification settings - Fork 432
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
Revise rand_core::Error (alt #771) #800
Conversation
This is virtually no usage, thus it was decided to remove
This gives us an alternative to feature-specific behaviour
@@ -18,10 +18,11 @@ travis-ci = { repository = "rust-random/rand" } | |||
appveyor = { repository = "rust-random/rand" } | |||
|
|||
[features] | |||
std = ["alloc"] # use std library; should be default but for above bug | |||
std = ["alloc", "getrandom", "getrandom/std"] # use std library; should be default but for above bug |
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.
Why is this coupled to getrandom
?
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.
Do you mean we should support std
without getrandom
? We could try, but (1) getrandom
supports (pretty-much) any std
platform anyway and (2) I'm not sure Cargo allows us to imply getrandom/std
only when both std
and getrandom
are enabled.
At a brief glance this code looks broken. Enabling I wanted to draft an RFC for distributed slices first, which not only will neatly solve error code to error message conversion issue, but also will be really useful for other use cases as well. But I couldn't find time to work on it. Personally I like approach in #771 a bit more. Not only it results in a smaller footprint for |
rand_core/src/error.rs
Outdated
|
||
|
||
// A randomly-chosen 24-bit prefix for our codes. | ||
#[cfg(not(feature="std"))] | ||
pub(crate) const CODE_PREFIX: u32 = 0x517e8100; |
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.
Why do we need this?
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.
The current no_std
code uses a code, but one isn't always provided. This is in line with what happens in getrandom
, but may not be optimal.
Thanks @newpavlov for the response. I like the idea, but even with distributed slice support it wouldn't be perfect: it relies on all other error sources not forgetting to register their messages, and it only records the origin of the error, not where it manifests. (Though knowing something like Also, we cannot rely on a future RFC. We could of course just return numeric codes for now but this isn't ideal (also because distributed slices might never happen or never be portable enough). As mentioned, we can make the I disagree that the |
Personally I think double boxing is not too important, usually 2 words instead of 1 word is not a deal breaker for If |
I wasn't proposing dropping You are right,
So in my opinion there is still a lot to be said for a more compatible system using |
Alternative on the above that probably makes more sense if we keep It comes back to the discussion we had for |
It seems we have two options: Option A is @newpavlov's model: pub struct Error {
#[cfg(feature="std")]
inner: Box<std::error::Error + Send + Sync + 'static>,
#[cfg(not(feature="std"))]
code: NonZeroU32,
} On Option B is a cut-down version of my model: pub struct Error {
msg: &'static str,
#[cfg(feature="std")]
cause: Option<Box<std::error::Error + Send + Sync + 'static>>,
} Here, we can support a simple Why not other options: Opinions (or even better evidence) please? |
Small addition: even without distributed slices we can define a static array as part of |
This is a significant breaking change, discussed in rust-random#800. Also rand::rngs::adapter::read is no longer publically exported.
I adjusted this to use @newpavlov's model, which it seems is the best option. So long as CI passes, I think this is ready. Review please? What's missing: currently |
Good, the only CI failure is this. |
LGTM! Though I do not quite understand the Conversion of error code to messages can be done later. I think the easiest approach will be to define array of error codes/messages as part of |
Optional dependencies can be used as feature flags, but I don't think it's possible to make them depend on anything transitively. I want |
Ah, indeed. IIUC it should be fixed with rust-lang/cargo#5565 eventually. |
#[cfg(feature="std")] | ||
cause: Option<Box<std::error::Error + Send + Sync>>, | ||
inner: Box<dyn std::error::Error + Send + Sync + 'static>, | ||
#[cfg(not(feature="std"))] | ||
code: NonZeroU32, | ||
} |
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.
Doesn't this still have the issue that the error types are incompatible for different features, making the std
feature non-additive?
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.
No, with enabled std
feature NonZeroU32
code gets wrapped into ErrorCode
, so public API gets only extended with new methods and no_std
part of API does not change in any way.
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.
Functionally this is the case (and the code
method can no longer guarantee to have Some(NonZeroU32)
result), but API changes are only additive.
Great, sounds like we've finally solved this one (two steps towards 0.7)! 🎉 |
There are no concerns about the |
Cargo unifies features for each crate in the build. The only concern should be a dependency using an API incompatible with the API derived from the feature specification; a dependency may use API additions that come with a feature flag it uses, but if it does not use those additions (or require the feature), it should be compatible both with and without the feature (hence features may only make additive changes to the API). |
This is an alternative to #771.
This does not currently make any attempt to reduce the size of
Error
; we could use a secondBox
around internals forstd
(or evenalloc
) mode and omit themsg
inno_std
mode. If this does offer performance improvements, I expect these will only be in isolated cases, therefore it may be best to use a feature flag (e.g.perf_error_size
) to implement this (there are no breaking changes other thanmsg
andfmt
having different results).We can avoid adding a
std
feature torand_os
as in #771 by usingFrom<getrandom::Error>
. (We could use a special function including a message, but I don't think there's much point.)Additionally, this moves the
from_entropy
function as previously discussed and changes a few docs (though it appears several doc links are broken; this will need fixing later).Any comments @vks @newpavlov @burdges / everyone?