Skip to content

Commit 06907f3

Browse files
authored
Rename Buf/BufMut, methods to chunk/chunk_mut (#450)
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
1 parent 54f5ced commit 06907f3

18 files changed

+96
-95
lines changed

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ on:
1111
env:
1212
RUSTFLAGS: -Dwarnings
1313
RUST_BACKTRACE: 1
14+
nightly: nightly-2020-12-17
1415

1516
defaults:
1617
run:
@@ -120,7 +121,7 @@ jobs:
120121
steps:
121122
- uses: actions/checkout@v2
122123
- name: Install Rust
123-
run: rustup update nightly && rustup default nightly
124+
run: rustup update $nightly && rustup default $nightly
124125
- name: Install rust-src
125126
run: rustup component add rust-src
126127
- name: ASAN / TSAN

benches/buf.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl Buf for TestBuf {
5353
assert!(self.pos <= self.buf.len());
5454
self.next_readlen();
5555
}
56-
fn bytes(&self) -> &[u8] {
56+
fn chunk(&self) -> &[u8] {
5757
if self.readlen == 0 {
5858
Default::default()
5959
} else {
@@ -87,8 +87,8 @@ impl Buf for TestBufC {
8787
self.inner.advance(cnt)
8888
}
8989
#[inline(never)]
90-
fn bytes(&self) -> &[u8] {
91-
self.inner.bytes()
90+
fn chunk(&self) -> &[u8] {
91+
self.inner.chunk()
9292
}
9393
}
9494

src/buf/buf_impl.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ macro_rules! buf_get_impl {
1616
// this Option<ret> trick is to avoid keeping a borrow on self
1717
// when advance() is called (mut borrow) and to call bytes() only once
1818
let ret = $this
19-
.bytes()
19+
.chunk()
2020
.get(..SIZE)
2121
.map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) });
2222

@@ -78,7 +78,7 @@ pub trait Buf {
7878
/// the buffer.
7979
///
8080
/// This value is greater than or equal to the length of the slice returned
81-
/// by `bytes`.
81+
/// by `chunk()`.
8282
///
8383
/// # Examples
8484
///
@@ -115,31 +115,31 @@ pub trait Buf {
115115
///
116116
/// let mut buf = &b"hello world"[..];
117117
///
118-
/// assert_eq!(buf.bytes(), &b"hello world"[..]);
118+
/// assert_eq!(buf.chunk(), &b"hello world"[..]);
119119
///
120120
/// buf.advance(6);
121121
///
122-
/// assert_eq!(buf.bytes(), &b"world"[..]);
122+
/// assert_eq!(buf.chunk(), &b"world"[..]);
123123
/// ```
124124
///
125125
/// # Implementer notes
126126
///
127127
/// This function should never panic. Once the end of the buffer is reached,
128-
/// i.e., `Buf::remaining` returns 0, calls to `bytes` should return an
128+
/// i.e., `Buf::remaining` returns 0, calls to `chunk()` should return an
129129
/// empty slice.
130-
fn bytes(&self) -> &[u8];
130+
fn chunk(&self) -> &[u8];
131131

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

165165
if self.has_remaining() {
166-
dst[0] = IoSlice::new(self.bytes());
166+
dst[0] = IoSlice::new(self.chunk());
167167
1
168168
} else {
169169
0
@@ -172,7 +172,7 @@ pub trait Buf {
172172

173173
/// Advance the internal cursor of the Buf
174174
///
175-
/// The next call to `bytes` will return a slice starting `cnt` bytes
175+
/// The next call to `chunk()` will return a slice starting `cnt` bytes
176176
/// further into the underlying buffer.
177177
///
178178
/// # Examples
@@ -182,11 +182,11 @@ pub trait Buf {
182182
///
183183
/// let mut buf = &b"hello world"[..];
184184
///
185-
/// assert_eq!(buf.bytes(), &b"hello world"[..]);
185+
/// assert_eq!(buf.chunk(), &b"hello world"[..]);
186186
///
187187
/// buf.advance(6);
188188
///
189-
/// assert_eq!(buf.bytes(), &b"world"[..]);
189+
/// assert_eq!(buf.chunk(), &b"world"[..]);
190190
/// ```
191191
///
192192
/// # Panics
@@ -253,7 +253,7 @@ pub trait Buf {
253253
let cnt;
254254

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

259259
ptr::copy_nonoverlapping(src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
@@ -283,7 +283,7 @@ pub trait Buf {
283283
/// This function panics if there is no more remaining data in `self`.
284284
fn get_u8(&mut self) -> u8 {
285285
assert!(self.remaining() >= 1);
286-
let ret = self.bytes()[0];
286+
let ret = self.chunk()[0];
287287
self.advance(1);
288288
ret
289289
}
@@ -306,7 +306,7 @@ pub trait Buf {
306306
/// This function panics if there is no more remaining data in `self`.
307307
fn get_i8(&mut self) -> i8 {
308308
assert!(self.remaining() >= 1);
309-
let ret = self.bytes()[0] as i8;
309+
let ret = self.chunk()[0] as i8;
310310
self.advance(1);
311311
ret
312312
}
@@ -861,7 +861,7 @@ pub trait Buf {
861861
/// let mut chain = b"hello "[..].chain(&b"world"[..]);
862862
///
863863
/// let full = chain.copy_to_bytes(11);
864-
/// assert_eq!(full.bytes(), b"hello world");
864+
/// assert_eq!(full.chunk(), b"hello world");
865865
/// ```
866866
fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
867867
where
@@ -908,13 +908,13 @@ macro_rules! deref_forward_buf {
908908
(**self).remaining()
909909
}
910910

911-
fn bytes(&self) -> &[u8] {
912-
(**self).bytes()
911+
fn chunk(&self) -> &[u8] {
912+
(**self).chunk()
913913
}
914914

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

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

10241024
#[inline]
1025-
fn bytes(&self) -> &[u8] {
1025+
fn chunk(&self) -> &[u8] {
10261026
self
10271027
}
10281028

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

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

src/buf/buf_mut.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub unsafe trait BufMut {
3131
/// position until the end of the buffer is reached.
3232
///
3333
/// This value is greater than or equal to the length of the slice returned
34-
/// by `bytes_mut`.
34+
/// by `chunk_mut()`.
3535
///
3636
/// # Examples
3737
///
@@ -56,7 +56,7 @@ pub unsafe trait BufMut {
5656

5757
/// Advance the internal cursor of the BufMut
5858
///
59-
/// The next call to `bytes_mut` will return a slice starting `cnt` bytes
59+
/// The next call to `chunk_mut` will return a slice starting `cnt` bytes
6060
/// further into the underlying buffer.
6161
///
6262
/// This function is unsafe because there is no guarantee that the bytes
@@ -70,11 +70,11 @@ pub unsafe trait BufMut {
7070
/// let mut buf = Vec::with_capacity(16);
7171
///
7272
/// // Write some data
73-
/// buf.bytes_mut()[0..2].copy_from_slice(b"he");
73+
/// buf.chunk_mut()[0..2].copy_from_slice(b"he");
7474
/// unsafe { buf.advance_mut(2) };
7575
///
7676
/// // write more bytes
77-
/// buf.bytes_mut()[0..3].copy_from_slice(b"llo");
77+
/// buf.chunk_mut()[0..3].copy_from_slice(b"llo");
7878
///
7979
/// unsafe { buf.advance_mut(3); }
8080
///
@@ -135,14 +135,14 @@ pub unsafe trait BufMut {
135135
///
136136
/// unsafe {
137137
/// // MaybeUninit::as_mut_ptr
138-
/// buf.bytes_mut()[0..].as_mut_ptr().write(b'h');
139-
/// buf.bytes_mut()[1..].as_mut_ptr().write(b'e');
138+
/// buf.chunk_mut()[0..].as_mut_ptr().write(b'h');
139+
/// buf.chunk_mut()[1..].as_mut_ptr().write(b'e');
140140
///
141141
/// buf.advance_mut(2);
142142
///
143-
/// buf.bytes_mut()[0..].as_mut_ptr().write(b'l');
144-
/// buf.bytes_mut()[1..].as_mut_ptr().write(b'l');
145-
/// buf.bytes_mut()[2..].as_mut_ptr().write(b'o');
143+
/// buf.chunk_mut()[0..].as_mut_ptr().write(b'l');
144+
/// buf.chunk_mut()[1..].as_mut_ptr().write(b'l');
145+
/// buf.chunk_mut()[2..].as_mut_ptr().write(b'o');
146146
///
147147
/// buf.advance_mut(3);
148148
/// }
@@ -153,12 +153,12 @@ pub unsafe trait BufMut {
153153
///
154154
/// # Implementer notes
155155
///
156-
/// This function should never panic. `bytes_mut` should return an empty
157-
/// slice **if and only if** `remaining_mut` returns 0. In other words,
158-
/// `bytes_mut` returning an empty slice implies that `remaining_mut` will
159-
/// return 0 and `remaining_mut` returning 0 implies that `bytes_mut` will
156+
/// This function should never panic. `chunk_mut` should return an empty
157+
/// slice **if and only if** `remaining_mut()` returns 0. In other words,
158+
/// `chunk_mut()` returning an empty slice implies that `remaining_mut()` will
159+
/// return 0 and `remaining_mut()` returning 0 implies that `chunk_mut()` will
160160
/// return an empty slice.
161-
fn bytes_mut(&mut self) -> &mut UninitSlice;
161+
fn chunk_mut(&mut self) -> &mut UninitSlice;
162162

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

192192
unsafe {
193-
let s = src.bytes();
194-
let d = self.bytes_mut();
193+
let s = src.chunk();
194+
let d = self.chunk_mut();
195195
l = cmp::min(s.len(), d.len());
196196

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

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

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

916-
fn bytes_mut(&mut self) -> &mut UninitSlice {
917-
(**self).bytes_mut()
916+
fn chunk_mut(&mut self) -> &mut UninitSlice {
917+
(**self).chunk_mut()
918918
}
919919

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

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

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

10611061
// a block to contain the src.bytes() borrow
10621062
{
1063-
let s = src.bytes();
1063+
let s = src.chunk();
10641064
l = s.len();
10651065
self.extend_from_slice(s);
10661066
}

src/buf/chain.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ where
138138
self.a.remaining() + self.b.remaining()
139139
}
140140

141-
fn bytes(&self) -> &[u8] {
141+
fn chunk(&self) -> &[u8] {
142142
if self.a.has_remaining() {
143-
self.a.bytes()
143+
self.a.chunk()
144144
} else {
145-
self.b.bytes()
145+
self.b.chunk()
146146
}
147147
}
148148

@@ -165,9 +165,9 @@ where
165165
}
166166

167167
#[cfg(feature = "std")]
168-
fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
169-
let mut n = self.a.bytes_vectored(dst);
170-
n += self.b.bytes_vectored(&mut dst[n..]);
168+
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
169+
let mut n = self.a.chunks_vectored(dst);
170+
n += self.b.chunks_vectored(&mut dst[n..]);
171171
n
172172
}
173173
}
@@ -181,11 +181,11 @@ where
181181
self.a.remaining_mut() + self.b.remaining_mut()
182182
}
183183

184-
fn bytes_mut(&mut self) -> &mut UninitSlice {
184+
fn chunk_mut(&mut self) -> &mut UninitSlice {
185185
if self.a.has_remaining_mut() {
186-
self.a.bytes_mut()
186+
self.a.chunk_mut()
187187
} else {
188-
self.b.bytes_mut()
188+
self.b.chunk_mut()
189189
}
190190
}
191191

src/buf/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<T: Buf> Iterator for IntoIter<T> {
117117
return None;
118118
}
119119

120-
let b = self.inner.bytes()[0];
120+
let b = self.inner.chunk()[0];
121121
self.inner.advance(1);
122122

123123
Some(b)

0 commit comments

Comments
 (0)