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

Use checked maths #51

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- rust: nightly
- rust: beta
- rust: stable
- rust: 1.60.0
- name: macOS
rust: nightly
os: macos
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ license = "MIT OR Apache-2.0"
readme = "README.md"
description = "A tool to unzip an archive in parallel"
repository = "https://github.com/google/ripunzip"
rust-version = "1.60"

[features]
real_world_benchmark = []
Expand Down
30 changes: 20 additions & 10 deletions src/unzip/seekable_http_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,23 +564,33 @@ pub(crate) struct SeekableHttpReader {

impl Seek for SeekableHttpReader {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
// TODO used checked arithmetic when stabilized
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This TODO actually was in reference to a proposal to allow checked arithmetic between disparate types, e.g. u64 and i64. However using try_into is just as good.

self.pos = match pos {
SeekFrom::Start(pos) => pos,
SeekFrom::End(pos) => {
if -pos > self.engine.len() as i64 {
return Err(std::io::Error::new(
let positive_pos: u64 = (-pos).try_into().map_err(|_| {
std::io::Error::new(std::io::ErrorKind::Unsupported, "Seeked beyond end")
})?;
self.engine
.len()
.checked_sub(positive_pos)
.ok_or(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"Rewind too far",
));
}
self.engine.len() - ((-pos) as u64)
))?
}
SeekFrom::Current(offset_from_pos) => {
if offset_from_pos > 0 {
self.pos + (offset_from_pos as u64)
} else {
self.pos - ((-offset_from_pos) as u64)
let offset_from_pos_u64: Result<u64, _> = offset_from_pos.try_into();
match offset_from_pos_u64 {
Ok(positive_offset) => self.pos + positive_offset,
Err(_) => {
let negative_offset = -offset_from_pos as u64;
self.pos
.checked_sub(negative_offset)
.ok_or(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"Rewind too far",
))?
}
}
}
};
Expand Down
Loading