pub struct Bspatch<'p> { /* private fields */ }Expand description
Fast and memory saving patcher compatible with bspatch.
Apply patch with a 4k copy buffer and a 1k-4k delta cache buffer:
use std::io;
use qbsdiff::Bspatch;
fn bspatch(source: &[u8], patch: &[u8]) -> io::Result<Vec<u8>> {
let mut target = Vec::new();
Bspatch::new(patch)?
.buffer_size(4096)
.delta_min(1024)
.apply(source, io::Cursor::new(&mut target))?;
Ok(target)
}Preallocate target file before applying patch:
use std::io;
use std::fs::File;
use std::path::Path;
use qbsdiff::Bspatch;
fn file_allocate(file: &mut File, size: u64) -> io::Result<()> {
unimplemented!()
}
fn bspatch<P: AsRef<Path>>(source: &[u8], target: P, patch: &[u8]) -> io::Result<u64> {
let patcher = Bspatch::new(patch)?;
let mut target_file = File::create(target)?;
file_allocate(&mut target_file, patcher.hint_target_size())?;
patcher.apply(source, &mut target_file)
}Implementations§
Source§impl<'p> Bspatch<'p>
impl<'p> Bspatch<'p>
Sourcepub fn new(patch: &'p [u8]) -> Result<Self>
pub fn new(patch: &'p [u8]) -> Result<Self>
Parse the patch file and create new patcher configuration.
Return error if failed to parse the patch header.
Sourcepub fn buffer_size(self, bs: usize) -> Self
pub fn buffer_size(self, bs: usize) -> Self
Set the main copy buffer size, (bs > 128, default is BUFFER_SIZE).
This is also the write buffer to target stream.
A relative big buffer (usually 128k) will speed up writing process
if the target stream is unbuffered (e.g. std::fs::File).
Sourcepub fn delta_min(self, dm: usize) -> Self
pub fn delta_min(self, dm: usize) -> Self
Sets the initial delta cache size, (dm > 128, default is DELTA_MIN).
The delta cache is dynamic and can grow up when needed (but keeps not greater than the size of main copy buffer).
This might be deprecated in later version.
Sourcepub fn hint_target_size(&self) -> u64
pub fn hint_target_size(&self) -> u64
Hint the final target file size.
Sourcepub fn apply<T: Write>(self, source: &[u8], target: T) -> Result<u64>
pub fn apply<T: Write>(self, source: &[u8], target: T) -> Result<u64>
Apply patch to the source data and output the stream of target.
Parameter source is designed to be a low-level &[u8] binary, rather than a Seek + Read random accessing data.
See #8, allowing random accessible source file might be misleading,
which could result in unwanted performance loss due to heavy syscall overheads of seek + read (by the way, pread/read_at is not cross-platform).
For those who in search of loading the source file lazily, simply memmap source file to memory is recommended.
The target data size would be returned if no error occurs.
Auto Trait Implementations§
impl<'p> Freeze for Bspatch<'p>
impl<'p> RefUnwindSafe for Bspatch<'p>
impl<'p> Send for Bspatch<'p>
impl<'p> Sync for Bspatch<'p>
impl<'p> Unpin for Bspatch<'p>
impl<'p> UnsafeUnpin for Bspatch<'p>
impl<'p> UnwindSafe for Bspatch<'p>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more