Skip to content

Commit e3e0431

Browse files
committed
Address API reviews
1 parent 71f3a83 commit e3e0431

File tree

4 files changed

+44
-53
lines changed

4 files changed

+44
-53
lines changed

src/stream/raw.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,17 @@ impl<'a> Decoder<'a> {
182182
// TODO: remove self?
183183
/// Read a skippable frame.
184184
pub fn read_skippable_frame(&self, dest: &mut Vec<u8>, input: &[u8]) -> io::Result<(usize, MagicVariant)> {
185-
use zstd_safe::DCtx;
186-
187185
let mut magic_variant = 0;
188-
DCtx::read_skippable_frame(&mut OutBuffer::around(dest), &mut magic_variant, input)
186+
zstd_safe::read_skippable_frame(dest, &mut magic_variant, input)
189187
.map(|written| (written, MagicVariant(magic_variant as u8)))
190188
.map_err(map_error_code)
191189
}
192190

193191
#[cfg(feature = "experimental")]
194192
// TODO: remove self?
195193
/// Check if a frame is skippable.
196-
pub fn is_skippable_frame(&self, input: &[u8]) -> io::Result<bool> {
197-
use zstd_safe::DCtx;
198-
199-
DCtx::is_skippable_frame(input)
200-
.map(|is_skippable| is_skippable != 0)
201-
.map_err(map_error_code)
194+
pub fn is_skippable_frame(&self, input: &[u8]) -> bool {
195+
zstd_safe::is_skippable_frame(input)
202196
}
203197
}
204198

src/stream/read/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'a, R: Read + Seek> Decoder<'a, BufReader<R>> {
154154
let op = self.reader.operation();
155155
// FIXME: I feel like we should do that check right after reading the magic number, but
156156
// ZSTD does it after reading the content size.
157-
if !op.is_skippable_frame(&magic_buffer)? {
157+
if !op.is_skippable_frame(&magic_buffer) {
158158
bytes_to_seek = U32_SIZE * 2;
159159
return Err(io::Error::new(io::ErrorKind::Other, "Unsupported frame parameter"));
160160
}

src/stream/zio/writer.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,13 @@ where
134134
/// Write a skippable frame after finishing the previous frame if needed.
135135
#[cfg(feature = "experimental")]
136136
pub fn write_skippable_frame(&mut self, buf: &[u8], magic_variant: u32) -> io::Result<()> {
137-
use zstd_safe::CCtx;
138-
139137
use crate::map_error_code;
140138

141139
if self.writing_frame {
142140
self.finish()?; // Since we're about to overwrite the buffer, flush its content to the destination.
143141
}
144142

145-
CCtx::write_skippable_frame(&mut OutBuffer::around(&mut self.buffer), buf, magic_variant)
143+
zstd_safe::write_skippable_frame(&mut self.buffer, buf, magic_variant)
146144
.map_err(map_error_code)?;
147145
self.offset = 0;
148146
self.write_from_offset()?;

zstd-safe/src/lib.rs

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -470,23 +470,6 @@ impl<'a> CCtx<'a> {
470470
})
471471
}
472472

473-
#[cfg(feature = "experimental")]
474-
// TODO: use InBuffer?
475-
pub fn write_skippable_frame<C: WriteBuf + ?Sized>(output: &mut OutBuffer<'_, C>, input: &[u8], magic_variant: u32) -> SafeResult {
476-
let input_len = input.len();
477-
unsafe {
478-
output.dst.write_from(|buffer, capacity| {
479-
parse_code(zstd_sys::ZSTD_writeSkippableFrame(
480-
buffer,
481-
capacity,
482-
input.as_ptr() as *mut _,
483-
input_len,
484-
magic_variant,
485-
))
486-
})
487-
}
488-
}
489-
490473
/// Performs a step of a streaming compression operation.
491474
///
492475
/// This will read some data from `input` and/or write some data to `output`.
@@ -867,6 +850,45 @@ impl Default for DCtx<'_> {
867850
}
868851
}
869852

853+
#[cfg(feature = "experimental")]
854+
pub fn read_skippable_frame<C: WriteBuf + ?Sized>(dst: &mut C, magic_variant: &mut u32, input: &[u8]) -> SafeResult {
855+
let input_len = input.len();
856+
unsafe {
857+
dst.write_from(|buffer, capacity| {
858+
parse_code(zstd_sys::ZSTD_readSkippableFrame(
859+
buffer,
860+
capacity,
861+
magic_variant,
862+
ptr_void(input),
863+
input_len,
864+
))
865+
})
866+
}
867+
}
868+
869+
#[cfg(feature = "experimental")]
870+
pub fn write_skippable_frame<C: WriteBuf + ?Sized>(dst: &mut C, input: &[u8], magic_variant: u32) -> SafeResult {
871+
let input_len = input.len();
872+
unsafe {
873+
dst.write_from(|buffer, capacity| {
874+
parse_code(zstd_sys::ZSTD_writeSkippableFrame(
875+
buffer,
876+
capacity,
877+
input.as_ptr() as *mut _,
878+
input_len,
879+
magic_variant,
880+
))
881+
})
882+
}
883+
}
884+
885+
#[cfg(feature = "experimental")]
886+
pub fn is_skippable_frame(input: &[u8]) -> bool {
887+
unsafe {
888+
zstd_sys::ZSTD_isSkippableFrame(ptr_void(input), input.len()) > 0
889+
}
890+
}
891+
870892
impl<'a> DCtx<'a> {
871893
/// Try to create a new decompression context.
872894
///
@@ -966,29 +988,6 @@ impl<'a> DCtx<'a> {
966988
}
967989
}
968990

969-
#[cfg(feature = "experimental")]
970-
pub fn read_skippable_frame<C: WriteBuf + ?Sized>(output: &mut OutBuffer<'_, C>, magic_variant: &mut u32, input: &[u8]) -> SafeResult {
971-
let input_len = input.len();
972-
unsafe {
973-
output.dst.write_from(|buffer, capacity| {
974-
parse_code(zstd_sys::ZSTD_readSkippableFrame(
975-
buffer,
976-
capacity,
977-
magic_variant,
978-
input.as_ptr() as *mut _,
979-
input_len,
980-
))
981-
})
982-
}
983-
}
984-
985-
#[cfg(feature = "experimental")]
986-
pub fn is_skippable_frame(input: &[u8]) -> SafeResult {
987-
unsafe {
988-
parse_code(zstd_sys::ZSTD_isSkippableFrame(input.as_ptr() as *mut _, input.len()) as usize)
989-
}
990-
}
991-
992991
/// Wraps the `ZSTD_initCStream()` function.
993992
///
994993
/// Initializes an existing `DStream` for decompression.

0 commit comments

Comments
 (0)