Skip to content
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

Replace more type casts with non-cast equivalents #437

Merged
merged 5 commits into from
May 31, 2024

Conversation

briansmith
Copy link
Contributor

No description provided.

@briansmith briansmith force-pushed the b/casts-2 branch 4 times, most recently from 58f3c01 to c02f532 Compare May 29, 2024 20:20
Cargo.toml Outdated Show resolved Hide resolved
@@ -19,7 +19,7 @@ pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
dest = &mut dest[res as usize..];
} else {
let err = match res {
MIN_RET_CODE..=-1 => NonZeroU32::new(-res as u32).unwrap().into(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if res == -i32::MIN then -res overflows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MIN_RET_CODE is -i32::MAX, so -res can not overflow. The current one results in a slightly better codegen and prevents accidental creation of errors in the custom range (e.g. if we got a corrupted return code for some reason), but your code is a bit easier to read and does not contain any unwraps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prevents accidental creation of errors in the custom range

Good point. I will point out a couple of other places where this is an issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only relevant for Hermit, since it uses isize for return codes, while other targets use i32.

@@ -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())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap here gets eliminated, but it still may be worth to use:

NonZeroU32::new(res.unsigned_abs()).unwrap_or(Error::UNEXPECTED)

to quickly prevent any concerns about potential panics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I was actually meaning to remove these unwrap()s in a separate PR, but since I did it in the hermit code, I also change it to do the same thing here.

@briansmith briansmith force-pushed the b/casts-2 branch 3 times, most recently from 8f3322b to 1d82297 Compare May 29, 2024 23:40
@@ -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::<&[u8]>(&buf);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to write it as test::black_box(&buf[..]).

@@ -19,7 +19,7 @@ pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
dest = &mut dest[res as usize..];
} else {
let err = match res {
MIN_RET_CODE..=-1 => NonZeroU32::new(-res as u32).unwrap().into(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MIN_RET_CODE is -i32::MAX, so -res can not overflow. The current one results in a slightly better codegen and prevents accidental creation of errors in the custom range (e.g. if we got a corrupted return code for some reason), but your code is a bit easier to read and does not contain any unwraps.

@@ -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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap here gets eliminated, but it still may be worth to use:

NonZeroU32::new(res.unsigned_abs()).unwrap_or(Error::UNEXPECTED)

to quickly prevent any concerns about potential panics.

src/solid.rs Outdated Show resolved Hide resolved
(-x) overflows when x is the minimum value, where `x.unsigned_abs()`
does the right thing.

Avoid unreachable `unwrap()` in these conversions.
@newpavlov newpavlov merged commit f8899bd into rust-random:master May 31, 2024
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants