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

chore: modern rust #231

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/spooled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl SpooledTempFile {
pub fn roll(&mut self) -> io::Result<()> {
if !self.is_rolled() {
let mut file = tempfile()?;
if let SpooledData::InMemory(ref mut cursor) = self.inner {
if let SpooledData::InMemory(cursor) = &mut self.inner {
file.write_all(cursor.get_ref())?;
file.seek(SeekFrom::Start(cursor.position()))?;
}
Expand All @@ -99,12 +99,12 @@ impl SpooledTempFile {
if size as usize > self.max_size {
self.roll()?; // does nothing if already rolled over
}
match self.inner {
SpooledData::InMemory(ref mut cursor) => {
match &mut self.inner {
SpooledData::InMemory(cursor) => {
cursor.get_mut().resize(size as usize, 0);
Ok(())
}
SpooledData::OnDisk(ref mut file) => file.set_len(size),
SpooledData::OnDisk(file) => file.set_len(size),
}
}

Expand All @@ -117,9 +117,9 @@ impl SpooledTempFile {

impl Read for SpooledTempFile {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.inner {
SpooledData::InMemory(ref mut cursor) => cursor.read(buf),
SpooledData::OnDisk(ref mut file) => file.read(buf),
match &mut self.inner {
SpooledData::InMemory(cursor) => cursor.read(buf),
SpooledData::OnDisk(file) => file.read(buf),
}
}
}
Expand All @@ -128,34 +128,34 @@ impl Write for SpooledTempFile {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
// roll over to file if necessary
let mut rolling = false;
if let SpooledData::InMemory(ref mut cursor) = self.inner {
if let SpooledData::InMemory(cursor) = &mut self.inner {
rolling = cursor.position() as usize + buf.len() > self.max_size;
}
if rolling {
self.roll()?;
}

// write the bytes
match self.inner {
SpooledData::InMemory(ref mut cursor) => cursor.write(buf),
SpooledData::OnDisk(ref mut file) => file.write(buf),
match &mut self.inner {
SpooledData::InMemory(cursor) => cursor.write(buf),
SpooledData::OnDisk(file) => file.write(buf),
}
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
match self.inner {
SpooledData::InMemory(ref mut cursor) => cursor.flush(),
SpooledData::OnDisk(ref mut file) => file.flush(),
match &mut self.inner {
SpooledData::InMemory(cursor) => cursor.flush(),
SpooledData::OnDisk(file) => file.flush(),
}
}
}

impl Seek for SpooledTempFile {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match self.inner {
SpooledData::InMemory(ref mut cursor) => cursor.seek(pos),
SpooledData::OnDisk(ref mut file) => file.seek(pos),
match &mut self.inner {
SpooledData::InMemory(cursor) => cursor.seek(pos),
SpooledData::OnDisk(file) => file.seek(pos),
}
}
}
9 changes: 3 additions & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ fn tmpname(prefix: &OsStr, suffix: &OsStr, rand_len: usize) -> OsString {
buf
}

pub fn create_helper<F, R>(
pub fn create_helper<R>(
base: &Path,
prefix: &OsStr,
suffix: &OsStr,
random_len: usize,
mut f: F,
) -> io::Result<R>
where
F: FnMut(PathBuf) -> io::Result<R>,
{
mut f: impl FnMut(PathBuf) -> io::Result<R>,
) -> io::Result<R> {
let num_retries = if random_len != 0 {
crate::NUM_RETRIES
} else {
Expand Down