-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
style(lib): Address cast-possible-truncation clippy findings. #4192
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
Open
Siech0
wants to merge
1
commit into
hyperium:master
Choose a base branch
from
Siech0:style/address-clippy-cast-possible-truncation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,13 +142,18 @@ impl Encoder { | |
| } | ||
| Kind::Length(remaining) => { | ||
| trace!("sized write, len = {}", len); | ||
| if len as u64 > *remaining { | ||
| let limit = *remaining as usize; | ||
| *remaining = 0; | ||
| BufKind::Limited(msg.take(limit)) | ||
| } else { | ||
| *remaining -= len as u64; | ||
| BufKind::Exact(msg) | ||
| match usize::try_from(*remaining) { | ||
| // Holding more than is owed, so write only what is left. | ||
| Ok(limit) if limit < len => { | ||
| *remaining = 0; | ||
| BufKind::Limited(msg.take(limit)) | ||
| } | ||
| // Ok(_) => Owed at least what we hold, write all of it. | ||
| // Err(_) => Owed more than `usize` can represent, write all of it. | ||
| Ok(_) | Err(_) => { | ||
| *remaining -= len as u64; | ||
| BufKind::Exact(msg) | ||
| } | ||
| } | ||
| } | ||
| #[cfg(feature = "server")] | ||
|
|
@@ -241,6 +246,7 @@ impl Encoder { | |
| dst.buffer(msg); | ||
| !self.is_last | ||
| } | ||
| #[allow(clippy::cast_possible_truncation, reason="usize::MAX > len > remaining, cast truncation is impossible")] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did an analysis on the assembly using try_from would produce here, and it would be a regression in performance on 32-bit systems. the as cast here does allow the compiler to enforce a more tight invariant and it is completely sound so I allowed it explicitly. |
||
| Ordering::Greater => { | ||
| dst.buffer(msg.take(remaining as usize)); | ||
| !self.is_last | ||
|
|
@@ -328,11 +334,7 @@ where | |
| } | ||
| } | ||
|
|
||
| #[cfg(target_pointer_width = "32")] | ||
| const USIZE_BYTES: usize = 4; | ||
|
|
||
| #[cfg(target_pointer_width = "64")] | ||
| const USIZE_BYTES: usize = 8; | ||
| const USIZE_BYTES: usize = std::mem::size_of::<usize>(); | ||
|
|
||
| // each byte will become 2 hex | ||
| const CHUNK_SIZE_MAX_BYTES: usize = USIZE_BYTES * 2; | ||
|
|
@@ -369,6 +371,7 @@ impl Buf for ChunkSize { | |
| } | ||
|
|
||
| #[inline] | ||
| #[allow(clippy::cast_possible_truncation)] | ||
| fn advance(&mut self, cnt: usize) { | ||
| assert!(cnt <= self.remaining()); | ||
| self.pos += cnt as u8; // just asserted cnt fits in u8 | ||
|
|
@@ -385,12 +388,14 @@ impl fmt::Debug for ChunkSize { | |
| } | ||
|
|
||
| impl fmt::Write for ChunkSize { | ||
| #[allow(clippy::cast_possible_truncation, reason="bytes is structurally always less than u8::MAX")] | ||
| fn write_str(&mut self, num: &str) -> fmt::Result { | ||
| use std::io::Write; | ||
| (&mut self.bytes[self.len.into()..]) | ||
| .write_all(num.as_bytes()) | ||
| .expect("&mut [u8].write() cannot error"); | ||
| self.len += num.len() as u8; // safe because bytes is never bigger than 256 | ||
| debug_assert!(u8::try_from(num.len()).is_ok()); | ||
| self.len += num.len() as u8; // safe because bytes is never bigger than 255 | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this can just be folded to -> _, but I felt like keeping the separation makes the clarity of why that is the case more obvious.