Skip to content

Commit

Permalink
Make ref_prefix available (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
WizKid committed May 24, 2024
1 parent fb6b490 commit 2b63859
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/stream/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ impl<'a> Decoder<'a> {
Ok(Decoder { context })
}

/// Creates a new decoder, using a ref prefix
pub fn with_ref_prefix<'b>(
ref_prefix: &'b [u8],
) -> io::Result<Self>
where
'b: 'a,
{
let mut context = zstd_safe::DCtx::create();
context
.ref_prefix(ref_prefix)
.map_err(map_error_code)?;
Ok(Decoder { context })
}

/// Sets a decompression parameter for this decoder.
pub fn set_parameter(&mut self, parameter: DParameter) -> io::Result<()> {
self.context
Expand Down Expand Up @@ -269,6 +283,27 @@ impl<'a> Encoder<'a> {
Ok(Encoder { context })
}

/// Creates a new encoder initialized with the given ref prefix.
pub fn with_ref_prefix<'b>(
level: i32,
ref_prefix: &'b [u8]
) -> io::Result<Self>
where
'b: 'a,
{
let mut context = zstd_safe::CCtx::create();

context
.set_parameter(CParameter::CompressionLevel(level))
.map_err(map_error_code)?;

context
.ref_prefix(ref_prefix)
.map_err(map_error_code)?;

Ok(Encoder { context })
}

/// Sets a compression parameter for this encoder.
pub fn set_parameter(&mut self, parameter: CParameter) -> io::Result<()> {
self.context
Expand Down
16 changes: 16 additions & 0 deletions src/stream/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ impl<'a, R: BufRead> Decoder<'a, R> {
Ok(Decoder { reader })
}

/// Creates a new decoder, using a ref prefix.
///
/// The prefix must be the same as the one used during compression.
pub fn with_ref_prefix<'b>(
reader: R,
ref_prefix: &'b [u8]
) -> io::Result<Self>
where
'b: 'a,
{
let decoder = raw::Decoder::with_ref_prefix(ref_prefix)?;
let reader = zio::Reader::new(reader, decoder);

Ok(Decoder { reader })
}

/// Recommendation for the size of the output buffer.
pub fn recommended_output_size() -> usize {
zstd_safe::DCtx::out_size()
Expand Down
14 changes: 14 additions & 0 deletions src/stream/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ impl<'a, W: Write> Encoder<'a, W> {
Ok(Encoder { writer })
}

/// Creates a new encoder, using a ref prefix
pub fn with_ref_prefix<'b>(
writer: W,
level: i32,
ref_prefix: &'b [u8],
) -> io::Result<Self>
where
'b: 'a,
{
let encoder = raw::Encoder::with_ref_prefix(level, ref_prefix)?;
let writer = zio::Writer::new(writer, encoder);
Ok(Encoder { writer })
}

/// Returns a wrapper around `self` that will finish the stream on drop.
pub fn auto_finish(self) -> AutoFinishEncoder<'a, W> {
AutoFinishEncoder {
Expand Down

0 comments on commit 2b63859

Please sign in to comment.