Skip to content

Commit

Permalink
Make OutBuffer::dst private
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Jul 20, 2023
1 parent 79592c8 commit 3cb6088
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/stream/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ impl Operation for NoOp {
) -> io::Result<usize> {
// 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:
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 18 additions & 19 deletions zstd-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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> {
Expand Down

0 comments on commit 3cb6088

Please sign in to comment.