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

More generic read/write api #14

Merged
merged 2 commits into from
Apr 17, 2020
Merged
Changes from all 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
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: &mut Box::new(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