Skip to content

Commit dd7b1c4

Browse files
committed
Address API reviews
1 parent 118a6dc commit dd7b1c4

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
@@ -179,23 +179,17 @@ impl<'a> Decoder<'a> {
179179
// TODO: remove self?
180180
/// Read a skippable frame.
181181
pub fn read_skippable_frame(&self, dest: &mut Vec<u8>, input: &[u8]) -> io::Result<(usize, MagicVariant)> {
182-
use zstd_safe::DCtx;
183-
184182
let mut magic_variant = 0;
185-
DCtx::read_skippable_frame(&mut OutBuffer::around(dest), &mut magic_variant, input)
183+
zstd_safe::read_skippable_frame(dest, &mut magic_variant, input)
186184
.map(|written| (written, MagicVariant(magic_variant as u8)))
187185
.map_err(map_error_code)
188186
}
189187

190188
#[cfg(feature = "experimental")]
191189
// TODO: remove self?
192190
/// Check if a frame is skippable.
193-
pub fn is_skippable_frame(&self, input: &[u8]) -> io::Result<bool> {
194-
use zstd_safe::DCtx;
195-
196-
DCtx::is_skippable_frame(input)
197-
.map(|is_skippable| is_skippable != 0)
198-
.map_err(map_error_code)
191+
pub fn is_skippable_frame(&self, input: &[u8]) -> bool {
192+
zstd_safe::is_skippable_frame(input)
199193
}
200194
}
201195

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
@@ -444,23 +444,6 @@ impl<'a> CCtx<'a> {
444444
})
445445
}
446446

447-
#[cfg(feature = "experimental")]
448-
// TODO: use InBuffer?
449-
pub fn write_skippable_frame<C: WriteBuf + ?Sized>(output: &mut OutBuffer<'_, C>, input: &[u8], magic_variant: u32) -> SafeResult {
450-
let input_len = input.len();
451-
unsafe {
452-
output.dst.write_from(|buffer, capacity| {
453-
parse_code(zstd_sys::ZSTD_writeSkippableFrame(
454-
buffer,
455-
capacity,
456-
input.as_ptr() as *mut _,
457-
input_len,
458-
magic_variant,
459-
))
460-
})
461-
}
462-
}
463-
464447
/// Performs a step of a streaming compression operation.
465448
///
466449
/// This will read some data from `input` and/or write some data to `output`.
@@ -821,6 +804,45 @@ impl Default for DCtx<'_> {
821804
}
822805
}
823806

807+
#[cfg(feature = "experimental")]
808+
pub fn read_skippable_frame<C: WriteBuf + ?Sized>(dst: &mut C, magic_variant: &mut u32, input: &[u8]) -> SafeResult {
809+
let input_len = input.len();
810+
unsafe {
811+
dst.write_from(|buffer, capacity| {
812+
parse_code(zstd_sys::ZSTD_readSkippableFrame(
813+
buffer,
814+
capacity,
815+
magic_variant,
816+
ptr_void(input),
817+
input_len,
818+
))
819+
})
820+
}
821+
}
822+
823+
#[cfg(feature = "experimental")]
824+
pub fn write_skippable_frame<C: WriteBuf + ?Sized>(dst: &mut C, input: &[u8], magic_variant: u32) -> SafeResult {
825+
let input_len = input.len();
826+
unsafe {
827+
dst.write_from(|buffer, capacity| {
828+
parse_code(zstd_sys::ZSTD_writeSkippableFrame(
829+
buffer,
830+
capacity,
831+
input.as_ptr() as *mut _,
832+
input_len,
833+
magic_variant,
834+
))
835+
})
836+
}
837+
}
838+
839+
#[cfg(feature = "experimental")]
840+
pub fn is_skippable_frame(input: &[u8]) -> bool {
841+
unsafe {
842+
zstd_sys::ZSTD_isSkippableFrame(ptr_void(input), input.len()) > 0
843+
}
844+
}
845+
824846
impl<'a> DCtx<'a> {
825847
/// Try to create a new decompression context.
826848
///
@@ -920,29 +942,6 @@ impl<'a> DCtx<'a> {
920942
}
921943
}
922944

923-
#[cfg(feature = "experimental")]
924-
pub fn read_skippable_frame<C: WriteBuf + ?Sized>(output: &mut OutBuffer<'_, C>, magic_variant: &mut u32, input: &[u8]) -> SafeResult {
925-
let input_len = input.len();
926-
unsafe {
927-
output.dst.write_from(|buffer, capacity| {
928-
parse_code(zstd_sys::ZSTD_readSkippableFrame(
929-
buffer,
930-
capacity,
931-
magic_variant,
932-
input.as_ptr() as *mut _,
933-
input_len,
934-
))
935-
})
936-
}
937-
}
938-
939-
#[cfg(feature = "experimental")]
940-
pub fn is_skippable_frame(input: &[u8]) -> SafeResult {
941-
unsafe {
942-
parse_code(zstd_sys::ZSTD_isSkippableFrame(input.as_ptr() as *mut _, input.len()) as usize)
943-
}
944-
}
945-
946945
/// Wraps the `ZSTD_initCStream()` function.
947946
///
948947
/// Initializes an existing `DStream` for decompression.

0 commit comments

Comments
 (0)