diff --git a/.clippy.toml b/.clippy.toml index 62ca7423..5cccb362 100644 --- a/.clippy.toml +++ b/.clippy.toml @@ -1 +1 @@ -msrv = "1.56" +msrv = "1.57" diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 53ebd05e..f3831d5c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,7 +44,7 @@ jobs: strategy: matrix: os: [ubuntu-22.04, windows-2022] - toolchain: [nightly, beta, stable, 1.56] + toolchain: [nightly, beta, stable, 1.57] # Only Test macOS on stable to reduce macOS CI jobs include: # x86_64-apple-darwin. diff --git a/Cargo.toml b/Cargo.toml index d7e65bfa..b9357bce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "getrandom" version = "0.2.15" # Also update html_root_url in lib.rs when bumping this -edition = "2018" -rust-version = "1.56" +edition = "2021" +rust-version = "1.57" # Sync .clippy.toml, tests.yml, and README.md. authors = ["The Rand Project Developers"] license = "MIT OR Apache-2.0" description = "A small cross-platform library for retrieving random data from system source" diff --git a/README.md b/README.md index 2ca14a66..56af89dd 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ crate features, WASM support and Custom RNGs see the ## Minimum Supported Rust Version -This crate requires Rust 1.56.0 or later. +This crate requires Rust 1.57.0 or later. ## Platform Support diff --git a/benches/buffer.rs b/benches/buffer.rs index b32be433..303bc3e4 100644 --- a/benches/buffer.rs +++ b/benches/buffer.rs @@ -8,7 +8,7 @@ use std::mem::MaybeUninit; fn bench_getrandom() { let mut buf = [0u8; N]; getrandom::getrandom(&mut buf).unwrap(); - test::black_box(&buf as &[u8]); + test::black_box(&buf[..]); } // Call getrandom_uninit on an uninitialized stack buffer diff --git a/src/hermit.rs b/src/hermit.rs index d797f74a..056e1b14 100644 --- a/src/hermit.rs +++ b/src/hermit.rs @@ -2,11 +2,6 @@ use crate::Error; use core::{mem::MaybeUninit, num::NonZeroU32}; -/// Minimum return value which we should get from syscalls in practice, -/// because Hermit uses positive `i32`s for error codes: -/// https://github.com/hermitcore/libhermit-rs/blob/main/src/errno.rs -const MIN_RET_CODE: isize = -(i32::MAX as isize); - extern "C" { fn sys_read_entropy(buffer: *mut u8, length: usize, flags: u32) -> isize; } @@ -18,9 +13,13 @@ pub fn getrandom_inner(mut dest: &mut [MaybeUninit]) -> Result<(), Error> { if res > 0 && (res as usize) <= dest.len() { dest = &mut dest[res as usize..]; } else { - let err = match res { - MIN_RET_CODE..=-1 => NonZeroU32::new(-res as u32).unwrap().into(), - _ => Error::UNEXPECTED, + let err = if res < 0 { + u32::try_from(res.unsigned_abs()) + .ok() + .and_then(NonZeroU32::new) + .map_or(Error::UNEXPECTED, Error::from) + } else { + Error::UNEXPECTED }; return Err(err); } diff --git a/src/lazy.rs b/src/lazy.rs index 693bb7ff..ca65bb7f 100644 --- a/src/lazy.rs +++ b/src/lazy.rs @@ -55,7 +55,7 @@ impl LazyBool { } pub fn unsync_init(&self, init: impl FnOnce() -> bool) -> bool { - self.0.unsync_init(|| init() as usize) != 0 + self.0.unsync_init(|| usize::from(init())) != 0 } } diff --git a/src/solid.rs b/src/solid.rs index 56a47eeb..f3181313 100644 --- a/src/solid.rs +++ b/src/solid.rs @@ -13,6 +13,6 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { } else { // ITRON error numbers are always negative, so we negate it so that it // falls in the dedicated OS error range (1..INTERNAL_START). - Err(NonZeroU32::new((-ret) as u32).unwrap().into()) + Err(NonZeroU32::new(ret.unsigned_abs()).map_or(Error::UNEXPECTED, Error::from)) } } diff --git a/src/util_libc.rs b/src/util_libc.rs index add966de..2ec7ff35 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -35,12 +35,15 @@ cfg_if! { } pub fn last_os_error() -> Error { - let errno = unsafe { get_errno() }; - if errno > 0 { - Error::from(NonZeroU32::new(errno as u32).unwrap()) - } else { - Error::ERRNO_NOT_POSITIVE - } + let errno: libc::c_int = unsafe { get_errno() }; + + // c_int-to-u32 conversion is lossless for nonnegative values if they are the same size. + const _: () = assert!(core::mem::size_of::() == core::mem::size_of::()); + + u32::try_from(errno) + .ok() + .and_then(NonZeroU32::new) + .map_or(Error::ERRNO_NOT_POSITIVE, Error::from) } // Fill a buffer by repeatedly invoking a system call. The `sys_fill` function: