Skip to content

Commit

Permalink
Rename Buf/BufMut, methods to chunk/chunk_mut (#450)
Browse files Browse the repository at this point in the history
The `bytes()` / `bytes_mut()` name implies the method returns the full
set of bytes represented by `Buf`/`BufMut`. To rectify this, the methods
are renamed to `chunk()` and `chunk_mut()` to reflect the partial nature
of the returned byte slice.

`bytes_vectored()` is renamed `chunks_vectored()`.

Closes #447
  • Loading branch information
carllerche committed Dec 18, 2020
1 parent 54f5ced commit 06907f3
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 95 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -11,6 +11,7 @@ on:
env:
RUSTFLAGS: -Dwarnings
RUST_BACKTRACE: 1
nightly: nightly-2020-12-17

defaults:
run:
Expand Down Expand Up @@ -120,7 +121,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Install Rust
run: rustup update nightly && rustup default nightly
run: rustup update $nightly && rustup default $nightly
- name: Install rust-src
run: rustup component add rust-src
- name: ASAN / TSAN
Expand Down
6 changes: 3 additions & 3 deletions benches/buf.rs
Expand Up @@ -53,7 +53,7 @@ impl Buf for TestBuf {
assert!(self.pos <= self.buf.len());
self.next_readlen();
}
fn bytes(&self) -> &[u8] {
fn chunk(&self) -> &[u8] {
if self.readlen == 0 {
Default::default()
} else {
Expand Down Expand Up @@ -87,8 +87,8 @@ impl Buf for TestBufC {
self.inner.advance(cnt)
}
#[inline(never)]
fn bytes(&self) -> &[u8] {
self.inner.bytes()
fn chunk(&self) -> &[u8] {
self.inner.chunk()
}
}

Expand Down
48 changes: 24 additions & 24 deletions src/buf/buf_impl.rs
Expand Up @@ -16,7 +16,7 @@ macro_rules! buf_get_impl {
// this Option<ret> trick is to avoid keeping a borrow on self
// when advance() is called (mut borrow) and to call bytes() only once
let ret = $this
.bytes()
.chunk()
.get(..SIZE)
.map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) });

Expand Down Expand Up @@ -78,7 +78,7 @@ pub trait Buf {
/// the buffer.
///
/// This value is greater than or equal to the length of the slice returned
/// by `bytes`.
/// by `chunk()`.
///
/// # Examples
///
Expand Down Expand Up @@ -115,31 +115,31 @@ pub trait Buf {
///
/// let mut buf = &b"hello world"[..];
///
/// assert_eq!(buf.bytes(), &b"hello world"[..]);
/// assert_eq!(buf.chunk(), &b"hello world"[..]);
///
/// buf.advance(6);
///
/// assert_eq!(buf.bytes(), &b"world"[..]);
/// assert_eq!(buf.chunk(), &b"world"[..]);
/// ```
///
/// # Implementer notes
///
/// This function should never panic. Once the end of the buffer is reached,
/// i.e., `Buf::remaining` returns 0, calls to `bytes` should return an
/// i.e., `Buf::remaining` returns 0, calls to `chunk()` should return an
/// empty slice.
fn bytes(&self) -> &[u8];
fn chunk(&self) -> &[u8];

/// Fills `dst` with potentially multiple slices starting at `self`'s
/// current position.
///
/// If the `Buf` is backed by disjoint slices of bytes, `bytes_vectored` enables
/// If the `Buf` is backed by disjoint slices of bytes, `chunk_vectored` enables
/// fetching more than one slice at once. `dst` is a slice of `IoSlice`
/// references, enabling the slice to be directly used with [`writev`]
/// without any further conversion. The sum of the lengths of all the
/// buffers in `dst` will be less than or equal to `Buf::remaining()`.
///
/// The entries in `dst` will be overwritten, but the data **contained** by
/// the slices **will not** be modified. If `bytes_vectored` does not fill every
/// the slices **will not** be modified. If `chunk_vectored` does not fill every
/// entry in `dst`, then `dst` is guaranteed to contain all remaining slices
/// in `self.
///
Expand All @@ -149,21 +149,21 @@ pub trait Buf {
/// # Implementer notes
///
/// This function should never panic. Once the end of the buffer is reached,
/// i.e., `Buf::remaining` returns 0, calls to `bytes_vectored` must return 0
/// i.e., `Buf::remaining` returns 0, calls to `chunk_vectored` must return 0
/// without mutating `dst`.
///
/// Implementations should also take care to properly handle being called
/// with `dst` being a zero length slice.
///
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
#[cfg(feature = "std")]
fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
if dst.is_empty() {
return 0;
}

if self.has_remaining() {
dst[0] = IoSlice::new(self.bytes());
dst[0] = IoSlice::new(self.chunk());
1
} else {
0
Expand All @@ -172,7 +172,7 @@ pub trait Buf {

/// Advance the internal cursor of the Buf
///
/// The next call to `bytes` will return a slice starting `cnt` bytes
/// The next call to `chunk()` will return a slice starting `cnt` bytes
/// further into the underlying buffer.
///
/// # Examples
Expand All @@ -182,11 +182,11 @@ pub trait Buf {
///
/// let mut buf = &b"hello world"[..];
///
/// assert_eq!(buf.bytes(), &b"hello world"[..]);
/// assert_eq!(buf.chunk(), &b"hello world"[..]);
///
/// buf.advance(6);
///
/// assert_eq!(buf.bytes(), &b"world"[..]);
/// assert_eq!(buf.chunk(), &b"world"[..]);
/// ```
///
/// # Panics
Expand Down Expand Up @@ -253,7 +253,7 @@ pub trait Buf {
let cnt;

unsafe {
let src = self.bytes();
let src = self.chunk();
cnt = cmp::min(src.len(), dst.len() - off);

ptr::copy_nonoverlapping(src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
Expand Down Expand Up @@ -283,7 +283,7 @@ pub trait Buf {
/// This function panics if there is no more remaining data in `self`.
fn get_u8(&mut self) -> u8 {
assert!(self.remaining() >= 1);
let ret = self.bytes()[0];
let ret = self.chunk()[0];
self.advance(1);
ret
}
Expand All @@ -306,7 +306,7 @@ pub trait Buf {
/// This function panics if there is no more remaining data in `self`.
fn get_i8(&mut self) -> i8 {
assert!(self.remaining() >= 1);
let ret = self.bytes()[0] as i8;
let ret = self.chunk()[0] as i8;
self.advance(1);
ret
}
Expand Down Expand Up @@ -861,7 +861,7 @@ pub trait Buf {
/// let mut chain = b"hello "[..].chain(&b"world"[..]);
///
/// let full = chain.copy_to_bytes(11);
/// assert_eq!(full.bytes(), b"hello world");
/// assert_eq!(full.chunk(), b"hello world");
/// ```
fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
where
Expand Down Expand Up @@ -908,13 +908,13 @@ macro_rules! deref_forward_buf {
(**self).remaining()
}

fn bytes(&self) -> &[u8] {
(**self).bytes()
fn chunk(&self) -> &[u8] {
(**self).chunk()
}

#[cfg(feature = "std")]
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
(**self).bytes_vectored(dst)
fn chunks_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
(**self).chunks_vectored(dst)
}

fn advance(&mut self, cnt: usize) {
Expand Down Expand Up @@ -1022,7 +1022,7 @@ impl Buf for &[u8] {
}

#[inline]
fn bytes(&self) -> &[u8] {
fn chunk(&self) -> &[u8] {
self
}

Expand All @@ -1045,7 +1045,7 @@ impl<T: AsRef<[u8]>> Buf for std::io::Cursor<T> {
len - pos as usize
}

fn bytes(&self) -> &[u8] {
fn chunk(&self) -> &[u8] {
let len = self.get_ref().as_ref().len();
let pos = self.position();

Expand Down
44 changes: 22 additions & 22 deletions src/buf/buf_mut.rs
Expand Up @@ -31,7 +31,7 @@ pub unsafe trait BufMut {
/// position until the end of the buffer is reached.
///
/// This value is greater than or equal to the length of the slice returned
/// by `bytes_mut`.
/// by `chunk_mut()`.
///
/// # Examples
///
Expand All @@ -56,7 +56,7 @@ pub unsafe trait BufMut {

/// Advance the internal cursor of the BufMut
///
/// The next call to `bytes_mut` will return a slice starting `cnt` bytes
/// The next call to `chunk_mut` will return a slice starting `cnt` bytes
/// further into the underlying buffer.
///
/// This function is unsafe because there is no guarantee that the bytes
Expand All @@ -70,11 +70,11 @@ pub unsafe trait BufMut {
/// let mut buf = Vec::with_capacity(16);
///
/// // Write some data
/// buf.bytes_mut()[0..2].copy_from_slice(b"he");
/// buf.chunk_mut()[0..2].copy_from_slice(b"he");
/// unsafe { buf.advance_mut(2) };
///
/// // write more bytes
/// buf.bytes_mut()[0..3].copy_from_slice(b"llo");
/// buf.chunk_mut()[0..3].copy_from_slice(b"llo");
///
/// unsafe { buf.advance_mut(3); }
///
Expand Down Expand Up @@ -135,14 +135,14 @@ pub unsafe trait BufMut {
///
/// unsafe {
/// // MaybeUninit::as_mut_ptr
/// buf.bytes_mut()[0..].as_mut_ptr().write(b'h');
/// buf.bytes_mut()[1..].as_mut_ptr().write(b'e');
/// buf.chunk_mut()[0..].as_mut_ptr().write(b'h');
/// buf.chunk_mut()[1..].as_mut_ptr().write(b'e');
///
/// buf.advance_mut(2);
///
/// buf.bytes_mut()[0..].as_mut_ptr().write(b'l');
/// buf.bytes_mut()[1..].as_mut_ptr().write(b'l');
/// buf.bytes_mut()[2..].as_mut_ptr().write(b'o');
/// buf.chunk_mut()[0..].as_mut_ptr().write(b'l');
/// buf.chunk_mut()[1..].as_mut_ptr().write(b'l');
/// buf.chunk_mut()[2..].as_mut_ptr().write(b'o');
///
/// buf.advance_mut(3);
/// }
Expand All @@ -153,12 +153,12 @@ pub unsafe trait BufMut {
///
/// # Implementer notes
///
/// This function should never panic. `bytes_mut` should return an empty
/// slice **if and only if** `remaining_mut` returns 0. In other words,
/// `bytes_mut` returning an empty slice implies that `remaining_mut` will
/// return 0 and `remaining_mut` returning 0 implies that `bytes_mut` will
/// This function should never panic. `chunk_mut` should return an empty
/// slice **if and only if** `remaining_mut()` returns 0. In other words,
/// `chunk_mut()` returning an empty slice implies that `remaining_mut()` will
/// return 0 and `remaining_mut()` returning 0 implies that `chunk_mut()` will
/// return an empty slice.
fn bytes_mut(&mut self) -> &mut UninitSlice;
fn chunk_mut(&mut self) -> &mut UninitSlice;

/// Transfer bytes into `self` from `src` and advance the cursor by the
/// number of bytes written.
Expand Down Expand Up @@ -190,8 +190,8 @@ pub unsafe trait BufMut {
let l;

unsafe {
let s = src.bytes();
let d = self.bytes_mut();
let s = src.chunk();
let d = self.chunk_mut();
l = cmp::min(s.len(), d.len());

ptr::copy_nonoverlapping(s.as_ptr(), d.as_mut_ptr() as *mut u8, l);
Expand Down Expand Up @@ -237,7 +237,7 @@ pub unsafe trait BufMut {
let cnt;

unsafe {
let dst = self.bytes_mut();
let dst = self.chunk_mut();
cnt = cmp::min(dst.len(), src.len() - off);

ptr::copy_nonoverlapping(src[off..].as_ptr(), dst.as_mut_ptr() as *mut u8, cnt);
Expand Down Expand Up @@ -913,8 +913,8 @@ macro_rules! deref_forward_bufmut {
(**self).remaining_mut()
}

fn bytes_mut(&mut self) -> &mut UninitSlice {
(**self).bytes_mut()
fn chunk_mut(&mut self) -> &mut UninitSlice {
(**self).chunk_mut()
}

unsafe fn advance_mut(&mut self, cnt: usize) {
Expand Down Expand Up @@ -998,7 +998,7 @@ unsafe impl BufMut for &mut [u8] {
}

#[inline]
fn bytes_mut(&mut self) -> &mut UninitSlice {
fn chunk_mut(&mut self) -> &mut UninitSlice {
// UninitSlice is repr(transparent), so safe to transmute
unsafe { &mut *(*self as *mut [u8] as *mut _) }
}
Expand Down Expand Up @@ -1033,7 +1033,7 @@ unsafe impl BufMut for Vec<u8> {
}

#[inline]
fn bytes_mut(&mut self) -> &mut UninitSlice {
fn chunk_mut(&mut self) -> &mut UninitSlice {
if self.capacity() == self.len() {
self.reserve(64); // Grow the vec
}
Expand All @@ -1060,7 +1060,7 @@ unsafe impl BufMut for Vec<u8> {

// a block to contain the src.bytes() borrow
{
let s = src.bytes();
let s = src.chunk();
l = s.len();
self.extend_from_slice(s);
}
Expand Down
18 changes: 9 additions & 9 deletions src/buf/chain.rs
Expand Up @@ -138,11 +138,11 @@ where
self.a.remaining() + self.b.remaining()
}

fn bytes(&self) -> &[u8] {
fn chunk(&self) -> &[u8] {
if self.a.has_remaining() {
self.a.bytes()
self.a.chunk()
} else {
self.b.bytes()
self.b.chunk()
}
}

Expand All @@ -165,9 +165,9 @@ where
}

#[cfg(feature = "std")]
fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
let mut n = self.a.bytes_vectored(dst);
n += self.b.bytes_vectored(&mut dst[n..]);
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
let mut n = self.a.chunks_vectored(dst);
n += self.b.chunks_vectored(&mut dst[n..]);
n
}
}
Expand All @@ -181,11 +181,11 @@ where
self.a.remaining_mut() + self.b.remaining_mut()
}

fn bytes_mut(&mut self) -> &mut UninitSlice {
fn chunk_mut(&mut self) -> &mut UninitSlice {
if self.a.has_remaining_mut() {
self.a.bytes_mut()
self.a.chunk_mut()
} else {
self.b.bytes_mut()
self.b.chunk_mut()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/buf/iter.rs
Expand Up @@ -117,7 +117,7 @@ impl<T: Buf> Iterator for IntoIter<T> {
return None;
}

let b = self.inner.bytes()[0];
let b = self.inner.chunk()[0];
self.inner.advance(1);

Some(b)
Expand Down

0 comments on commit 06907f3

Please sign in to comment.