Skip to content

Commit

Permalink
More generic read/write api
Browse files Browse the repository at this point in the history
Require just `Read` and `Write` for input arguments and drop the `'static`
requirement for `Read`.

Signed-off-by: Jonathas-Conceicao <jonathas.conceicao@ossystems.com.br>
  • Loading branch information
Jonathas-Conceicao authored and otavio committed Apr 17, 2020
1 parent 7a39063 commit 1b6222a
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,23 @@ enum Mode {
/// # Ok(())
/// # }
/// ```
pub fn uncompress_file<R, W>(source: &mut R, target: &mut W) -> Result<()>
///
/// Slices can be used if you know the exact length of the uncompressed data.
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use compress_tools::*;
/// use std::fs::File;
///
/// let mut source = File::open("file.txt.gz")?;
/// let mut target = [0 as u8;313];
///
/// uncompress_file(&mut source, &mut target as &mut [u8])?;
/// # Ok(())
/// # }
/// ```
pub fn uncompress_file<R, W>(source: R, target: W) -> Result<()>
where
R: Read + 'static,
R: Read,
W: Write,
{
run_with_archive(
Expand Down Expand Up @@ -116,9 +130,9 @@ where
/// # Ok(())
/// # }
/// ```
pub fn uncompress_archive<R>(source: &mut R, dest: &Path, ownership: Ownership) -> Result<()>
pub fn uncompress_archive<R>(source: R, dest: &Path, ownership: Ownership) -> Result<()>
where
R: Read + 'static,
R: Read,
{
run_with_archive(
Mode::WriteDisk { ownership },
Expand Down Expand Up @@ -180,9 +194,9 @@ where
/// # Ok(())
/// # }
/// ```
pub fn uncompress_archive_file<R, W>(source: &mut R, target: &mut W, path: &str) -> Result<()>
pub fn uncompress_archive_file<R, W>(source: R, target: W, path: &str) -> Result<()>
where
R: Read + 'static,
R: Read,
W: Write,
{
run_with_archive(
Expand All @@ -207,10 +221,10 @@ where
)
}

fn run_with_archive<F, R>(mode: Mode, reader: &mut R, f: F) -> Result<()>
fn run_with_archive<F, R>(mode: Mode, mut reader: R, f: F) -> Result<()>
where
F: FnOnce(*mut ffi::archive, *mut ffi::archive, *mut ffi::archive_entry) -> Result<()>,
R: Read + 'static,
R: Read,
{
let archive_reader: *mut ffi::archive;
let archive_writer: *mut ffi::archive;
Expand Down Expand Up @@ -271,7 +285,7 @@ where
}

let mut pipe = Pipe {
reader,
reader: &mut reader,
buffer: &mut [0; READER_BUFFER_SIZE],
};

Expand Down Expand Up @@ -324,7 +338,7 @@ fn libarchive_copy_data(

unsafe fn libarchive_write_data_block<W>(
archive_reader: *mut ffi::archive,
target: &mut W,
mut target: W,
) -> Result<()>
where
W: Write,
Expand Down

0 comments on commit 1b6222a

Please sign in to comment.