diff --git a/src/stream/raw.rs b/src/stream/raw.rs index 65a6d06c..2819455f 100644 --- a/src/stream/raw.rs +++ b/src/stream/raw.rs @@ -95,12 +95,12 @@ impl Operation for NoOp { ) -> io::Result { // Skip the prelude let src = &input.src[input.pos..]; - // Safe because `output.pos() <= output.dst.capacity()`. + // Safe because `output.pos() <= output.capacity()`. let output_pos = output.pos(); - let dst = unsafe { output.dst.as_mut_ptr().add(output_pos) }; + let dst = unsafe { output.as_mut_ptr().add(output_pos) }; // Ignore anything past the end - let len = usize::min(src.len(), output.dst.capacity() - output_pos); + let len = usize::min(src.len(), output.capacity() - output_pos); let src = &src[..len]; // Safe because: @@ -193,7 +193,7 @@ impl Operation for Decoder<'_> { self.run(&mut InBuffer::around(&[]), output)?; // We don't _know_ how much (decompressed data) there is still in buffer. - if output.pos() < output.dst.capacity() { + if output.pos() < output.capacity() { // We only know when there's none (the output buffer is not full). Ok(0) } else { diff --git a/zstd-safe/src/lib.rs b/zstd-safe/src/lib.rs index 1c555674..d21cf971 100644 --- a/zstd-safe/src/lib.rs +++ b/zstd-safe/src/lib.rs @@ -19,6 +19,8 @@ //! `experimental` feature. #![cfg_attr(feature = "doc-cfg", feature(doc_cfg))] +// TODO: Use alloc feature instead to implement stuff for Vec +// TODO: What about Cursor? #[cfg(feature = "std")] extern crate std; @@ -33,11 +35,6 @@ pub use zstd_sys::ZSTD_strategy as Strategy; /// Reset directive. // pub use zstd_sys::ZSTD_ResetDirective as ResetDirective; - -#[cfg(feature = "std")] -use std::os::raw::{c_char, c_int, c_ulonglong, c_void}; - -#[cfg(not(feature = "std"))] use core::ffi::{c_char, c_int, c_ulonglong, c_void}; use core::marker::PhantomData; @@ -817,19 +814,9 @@ unsafe impl<'a> Send for CCtx<'a> {} // CCtx can't be shared across threads, so it does not implement Sync. unsafe fn c_char_to_str(text: *const c_char) -> &'static str { - #[cfg(not(feature = "std"))] - { - core::ffi::CStr::from_ptr(text) - .to_str() - .expect("bad error message from zstd") - } - - #[cfg(feature = "std")] - { - std::ffi::CStr::from_ptr(text) - .to_str() - .expect("bad error message from zstd") - } + core::ffi::CStr::from_ptr(text) + .to_str() + .expect("bad error message from zstd") } /// Returns the error string associated with an error code. @@ -1662,7 +1649,7 @@ unsafe impl<'a> WriteBuf for OutBuffer<'a, [u8]> { /// /// `pos <= dst.capacity()` pub struct OutBuffer<'a, C: WriteBuf + ?Sized> { - pub dst: &'a mut C, + dst: &'a mut C, pos: usize, } @@ -1717,11 +1704,18 @@ impl<'a, C: WriteBuf + ?Sized> OutBuffer<'a, C> { } /// Returns the current cursor position. + /// + /// Guaranteed to be <= self.capacity() pub fn pos(&self) -> usize { assert!(self.pos <= self.dst.capacity()); self.pos } + /// Returns the capacity of the underlying buffer. + pub fn capacity(&self) -> usize { + self.dst.capacity() + } + /// Sets the new cursor position. /// /// # Panics @@ -1760,6 +1754,11 @@ impl<'a, C: WriteBuf + ?Sized> OutBuffer<'a, C> { let pos = self.pos; &self.dst.as_slice()[..pos] } + + /// Returns a pointer to the start of this buffer. + pub fn as_mut_ptr(&mut self) -> *mut u8 { + self.dst.as_mut_ptr() + } } impl<'a, 'b, C: WriteBuf + ?Sized> Drop for OutBufferWrapper<'a, 'b, C> {