Skip to content

Replace more type casts with non-cast equivalents#437

Merged
newpavlov merged 5 commits intorust-random:masterfrom
briansmith:b/casts-2
May 31, 2024
Merged

Replace more type casts with non-cast equivalents#437
newpavlov merged 5 commits intorust-random:masterfrom
briansmith:b/casts-2

Conversation

@briansmith
Copy link
Copy Markdown
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
Comment thread Cargo.toml Outdated
Comment thread src/hermit.rs
dest = &mut dest[res as usize..];
} else {
let err = match res {
MIN_RET_CODE..=-1 => NonZeroU32::new(-res as u32).unwrap().into(),
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread src/solid.rs
} 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Comment thread benches/buffer.rs Outdated
let mut buf = [0u8; N];
getrandom::getrandom(&mut buf).unwrap();
test::black_box(&buf as &[u8]);
test::black_box::<&[u8]>(&buf);
Copy link
Copy Markdown
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[..]).

Comment thread src/hermit.rs
dest = &mut dest[res as usize..];
} else {
let err = match res {
MIN_RET_CODE..=-1 => NonZeroU32::new(-res as u32).unwrap().into(),
Copy link
Copy Markdown
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.

Comment thread src/solid.rs
} 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
Copy Markdown
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.

Comment thread src/solid.rs Outdated
(-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
takumi-earth pushed a commit to earthlings-dev/getrandom that referenced this pull request Jan 27, 2026
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