Skip to content

Commit

Permalink
make it compile
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesvollmer committed Sep 22, 2023
1 parent 2f76858 commit 1a76683
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/io/track_progress.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use tiff::ColorType;
use crate::Progress;

//! Tracks read and write progress on the level of byte writers and readers.
//! The progress is reported based on the size of the uncompressed image.
Expand All @@ -17,13 +12,17 @@ use crate::Progress;
//! Any buffering, like `BufRead`, should be in the inner reader.
//! Otherwise, the tracker will only be able to see occasional large byte chunks.
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use crate::{ColorType, Progress};

/// A byte reader, like a file or a network stream.
/// Tracks how many bytes are taken to the inner reader.
/// Calls the progress callback occasionally.
/// Does not support seeking currently.
/// The progress callback will never overshoot.
pub struct TrackProgressReader<R, F> {
pub struct TrackProgressReader<R, F: FnMut(Progress)> {
inner: R,
on_progress: F,
complete_on_drop: bool,
Expand All @@ -36,30 +35,31 @@ pub struct TrackProgressReader<R, F> {
/// Calls the progress callback occasionally.
/// Does not support seeking currently.
/// The progress callback will never overshoot.
pub struct TrackProgressWriter<R, F> {
pub struct TrackProgressWriter<R, F: FnMut(Progress)> {
inner: R,
on_progress: F,
complete_on_drop: bool,
written_bytes: u64,
approximate_total_bytes: u64,
}

impl<F> TrackProgressReader<File, F> {
impl<F: FnMut(Progress)> TrackProgressReader<File, F> {
/// Usually called before anything is decoded, but only available for files. Most precise option.
pub fn open_file(path: impl AsRef<Path>, on_progress: F) -> Self {
pub fn open_file(path: impl AsRef<Path>, on_progress: F) -> std::io::Result<Self> {
let file = File::open(path)?;

Self {
Ok(Self {
on_progress,
inner: file,
approximate_total_bytes: file.metadata()?.len(),
complete_on_drop: true,
loaded_bytes: 0,
}

inner: file,
})
}
}

impl<R: Read, F> TrackProgressReader<R, F> {
impl<R: Read, F: FnMut(Progress)> TrackProgressReader<R, F> {
/// Usually called after meta data has been extracted. May undershoot when file contents are compressed.
pub fn new(inner: R, on_progress: F, file_size: u64) -> Self {
Self {
Expand All @@ -82,7 +82,7 @@ impl<R: Read, F> TrackProgressReader<R, F> {
}
}

impl<W: Write, F> TrackProgressWriter<W, F> {
impl<W: Write, F: FnMut(Progress)> TrackProgressWriter<W, F> {
/// Usually called after meta data has been extracted. May undershoot when file contents are compressed.
pub fn new(inner: W, on_progress: F, estimated_compressed_file_size: u64) -> Self {
Self {
Expand All @@ -106,10 +106,10 @@ impl<R,F> Read for TrackProgressReader<R, F>
fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
let result = self.inner.read(buffer);

if let Some(&count) = &result {
if let Ok(count) = result {
// report progress of the bytes that have definitely been processed
self.on_progress(new_progress(self.loaded_bytes, self.approximate_total_bytes));
self.loaded_bytes += count;
(self.on_progress)(new_progress(self.loaded_bytes, self.approximate_total_bytes));
self.loaded_bytes += count as u64;
}

result
Expand All @@ -123,11 +123,11 @@ impl<W,F> Write for TrackProgressWriter<W, F>
fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> {
let result = self.inner.write(buffer);

if let Some(&count) = &result {
self.written_bytes += count;
if let Ok(count) = result {
self.written_bytes += count as u64;

// report progress of written state
self.on_progress(new_progress(self.written_bytes, self.approximate_total_bytes));
(self.on_progress)(new_progress(self.written_bytes, self.approximate_total_bytes));
}

result
Expand All @@ -141,15 +141,15 @@ impl<W,F> Write for TrackProgressWriter<W, F>
impl<R, F: FnMut(Progress)> Drop for TrackProgressReader<R, F> {
fn drop(&mut self) {
if self.complete_on_drop {
self.on_progress(complete_progress(self.approximate_total_bytes))
(self.on_progress)(complete_progress(self.approximate_total_bytes))
}
}
}

impl<W, F: FnMut(Progress)> Drop for TrackProgressWriter<W, F> {
fn drop(&mut self) {
if self.complete_on_drop {
self.on_progress(complete_progress(self.approximate_total_bytes))
(self.on_progress)(complete_progress(self.approximate_total_bytes))
}
}
}
Expand Down

0 comments on commit 1a76683

Please sign in to comment.