Skip to content

Commit f8899bd

Browse files
authored
Replace more type casts with non-cast equivalents (#437)
1 parent 867392a commit f8899bd

File tree

9 files changed

+24
-22
lines changed

9 files changed

+24
-22
lines changed

.clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
msrv = "1.56"
1+
msrv = "1.57"

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
strategy:
4545
matrix:
4646
os: [ubuntu-22.04, windows-2022]
47-
toolchain: [nightly, beta, stable, 1.56]
47+
toolchain: [nightly, beta, stable, 1.57]
4848
# Only Test macOS on stable to reduce macOS CI jobs
4949
include:
5050
# x86_64-apple-darwin.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
22
name = "getrandom"
33
version = "0.2.15" # Also update html_root_url in lib.rs when bumping this
4-
edition = "2018"
5-
rust-version = "1.56"
4+
edition = "2021"
5+
rust-version = "1.57" # Sync .clippy.toml, tests.yml, and README.md.
66
authors = ["The Rand Project Developers"]
77
license = "MIT OR Apache-2.0"
88
description = "A small cross-platform library for retrieving random data from system source"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ crate features, WASM support and Custom RNGs see the
5252

5353
## Minimum Supported Rust Version
5454

55-
This crate requires Rust 1.56.0 or later.
55+
This crate requires Rust 1.57.0 or later.
5656

5757
## Platform Support
5858

benches/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::mem::MaybeUninit;
88
fn bench_getrandom<const N: usize>() {
99
let mut buf = [0u8; N];
1010
getrandom::getrandom(&mut buf).unwrap();
11-
test::black_box(&buf as &[u8]);
11+
test::black_box(&buf[..]);
1212
}
1313

1414
// Call getrandom_uninit on an uninitialized stack buffer

src/hermit.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
use crate::Error;
33
use core::{mem::MaybeUninit, num::NonZeroU32};
44

5-
/// Minimum return value which we should get from syscalls in practice,
6-
/// because Hermit uses positive `i32`s for error codes:
7-
/// https://github.com/hermitcore/libhermit-rs/blob/main/src/errno.rs
8-
const MIN_RET_CODE: isize = -(i32::MAX as isize);
9-
105
extern "C" {
116
fn sys_read_entropy(buffer: *mut u8, length: usize, flags: u32) -> isize;
127
}
@@ -18,9 +13,13 @@ pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1813
if res > 0 && (res as usize) <= dest.len() {
1914
dest = &mut dest[res as usize..];
2015
} else {
21-
let err = match res {
22-
MIN_RET_CODE..=-1 => NonZeroU32::new(-res as u32).unwrap().into(),
23-
_ => Error::UNEXPECTED,
16+
let err = if res < 0 {
17+
u32::try_from(res.unsigned_abs())
18+
.ok()
19+
.and_then(NonZeroU32::new)
20+
.map_or(Error::UNEXPECTED, Error::from)
21+
} else {
22+
Error::UNEXPECTED
2423
};
2524
return Err(err);
2625
}

src/lazy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl LazyBool {
5555
}
5656

5757
pub fn unsync_init(&self, init: impl FnOnce() -> bool) -> bool {
58-
self.0.unsync_init(|| init() as usize) != 0
58+
self.0.unsync_init(|| usize::from(init())) != 0
5959
}
6060
}
6161

src/solid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1313
} else {
1414
// ITRON error numbers are always negative, so we negate it so that it
1515
// falls in the dedicated OS error range (1..INTERNAL_START).
16-
Err(NonZeroU32::new((-ret) as u32).unwrap().into())
16+
Err(NonZeroU32::new(ret.unsigned_abs()).map_or(Error::UNEXPECTED, Error::from))
1717
}
1818
}

src/util_libc.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ cfg_if! {
3535
}
3636

3737
pub fn last_os_error() -> Error {
38-
let errno = unsafe { get_errno() };
39-
if errno > 0 {
40-
Error::from(NonZeroU32::new(errno as u32).unwrap())
41-
} else {
42-
Error::ERRNO_NOT_POSITIVE
43-
}
38+
let errno: libc::c_int = unsafe { get_errno() };
39+
40+
// c_int-to-u32 conversion is lossless for nonnegative values if they are the same size.
41+
const _: () = assert!(core::mem::size_of::<libc::c_int>() == core::mem::size_of::<u32>());
42+
43+
u32::try_from(errno)
44+
.ok()
45+
.and_then(NonZeroU32::new)
46+
.map_or(Error::ERRNO_NOT_POSITIVE, Error::from)
4447
}
4548

4649
// Fill a buffer by repeatedly invoking a system call. The `sys_fill` function:

0 commit comments

Comments
 (0)