rust-random / getrandom

A small cross-platform library for retrieving random data from (operating) system source
Apache License 2.0
264 stars 166 forks source link

Deprecate and remove `impl From<NonZeroU32> for Error`. #455

Open briansmith opened 1 month ago

briansmith commented 1 month ago

The From<NonZeroU32> for Error implementation doesn't do any checking of its inputs; for example, it doesn't verify that an "internal" error code isn't being used, nor does it verify that any internal error code is actually a valid/known one.

I propose:

This way, internal errors will eventually only be able to be constructed from within the crate.

josephlr commented 2 weeks ago

I like this idea, having something like:

impl Error {
  pub const fn from_os_error(i32) -> Self;
  pub const fn raw_os_error(self) -> Option<i32>

  pub const fn new_custom(u16) -> Self;
  const fn new_internal(u16) -> Self;
}

seems reasonable to me. We may want to use i32 consistently for OS errors as that's what libstd uses (and it's the return type of errno).

EDIT: Should we also remove the Error::code method?