Skip to content

Commit

Permalink
Replace more type casts with non-cast equivalents (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith authored May 31, 2024
1 parent 867392a commit f8899bd
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.56"
msrv = "1.57"
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion benches/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::mem::MaybeUninit;
fn bench_getrandom<const N: usize>() {
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
Expand Down
15 changes: 7 additions & 8 deletions src/hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -18,9 +13,13 @@ pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> 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))
}
}
15 changes: 9 additions & 6 deletions src/util_libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<libc::c_int>() == core::mem::size_of::<u32>());

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:
Expand Down

0 comments on commit f8899bd

Please sign in to comment.