Skip to content

Commit 244e3d7

Browse files
authored
Unrolled build for rust-lang#109174
Rollup merge of rust-lang#109174 - soerenmeier:cursor_fns, r=dtolnay Replace `io::Cursor::{remaining_slice, is_empty}` This is a late follow up to the concerns raised in rust-lang#86369. rust-lang#86369 (comment) > This API seems focussed on the `Read` side of things. When `Seek`ing around and `Write`ing data, `is_empty` becomes confusing and `remaining_slice` is not very useful. When writing, the part of the slice before the cursor is much more interesting. Maybe we should have functions for both? Or a single function that returns both slices? (If we also have a `mut` version, a single function would be useful to allow mutable access to both sides at once.) New feature name: `cursor_remaining` > `cursor_split`. Added functions: ```rust fn split(&self) -> (&[u8], &[u8]); // fn before(&self) -> &[u8]; // fn after(&self) -> &[u8]; fn split_mut(&mut self) -> (&mut [u8], &mut [u8]); // fn before_mut(&mut self) -> &mut [u8]; // fn after_mut(&mut self) -> &mut [u8]; ``` A question was raised in rust-lang#86369 (comment) about whether to return a lifetime that would reflect the lifetime of the underlying bytes (`impl Cursor<&'a [u8]> { fn after(&self) -> &'a [u8] }`). The downside of doing this would be that it would not be possible to implement these functions generically over `T: AsRef<[u8]>`. ## Update Based on the review, before* and after* methods where removed.
2 parents 2e63026 + 10da555 commit 244e3d7

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

library/std/src/io/cursor.rs

+35-30
Original file line numberDiff line numberDiff line change
@@ -209,55 +209,60 @@ impl<T> Cursor<T>
209209
where
210210
T: AsRef<[u8]>,
211211
{
212-
/// Returns the remaining slice.
212+
/// Splits the underlying slice at the cursor position and returns them.
213213
///
214214
/// # Examples
215215
///
216216
/// ```
217-
/// #![feature(cursor_remaining)]
217+
/// #![feature(cursor_split)]
218218
/// use std::io::Cursor;
219219
///
220220
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
221221
///
222-
/// assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]);
222+
/// assert_eq!(buff.split(), ([].as_slice(), [1, 2, 3, 4, 5].as_slice()));
223223
///
224224
/// buff.set_position(2);
225-
/// assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
226-
///
227-
/// buff.set_position(4);
228-
/// assert_eq!(buff.remaining_slice(), &[5]);
225+
/// assert_eq!(buff.split(), ([1, 2].as_slice(), [3, 4, 5].as_slice()));
229226
///
230227
/// buff.set_position(6);
231-
/// assert_eq!(buff.remaining_slice(), &[]);
228+
/// assert_eq!(buff.split(), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()));
232229
/// ```
233-
#[unstable(feature = "cursor_remaining", issue = "86369")]
234-
pub fn remaining_slice(&self) -> &[u8] {
235-
let len = self.pos.min(self.inner.as_ref().len() as u64);
236-
&self.inner.as_ref()[(len as usize)..]
230+
#[unstable(feature = "cursor_split", issue = "86369")]
231+
pub fn split(&self) -> (&[u8], &[u8]) {
232+
let slice = self.inner.as_ref();
233+
let pos = self.pos.min(slice.len() as u64);
234+
slice.split_at(pos as usize)
237235
}
236+
}
238237

239-
/// Returns `true` if the remaining slice is empty.
238+
impl<T> Cursor<T>
239+
where
240+
T: AsMut<[u8]>,
241+
{
242+
/// Splits the underlying slice at the cursor position and returns them
243+
/// mutably.
240244
///
241245
/// # Examples
242246
///
243247
/// ```
244-
/// #![feature(cursor_remaining)]
248+
/// #![feature(cursor_split)]
245249
/// use std::io::Cursor;
246250
///
247251
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
248252
///
249-
/// buff.set_position(2);
250-
/// assert!(!buff.is_empty());
253+
/// assert_eq!(buff.split_mut(), ([].as_mut_slice(), [1, 2, 3, 4, 5].as_mut_slice()));
251254
///
252-
/// buff.set_position(5);
253-
/// assert!(buff.is_empty());
255+
/// buff.set_position(2);
256+
/// assert_eq!(buff.split_mut(), ([1, 2].as_mut_slice(), [3, 4, 5].as_mut_slice()));
254257
///
255-
/// buff.set_position(10);
256-
/// assert!(buff.is_empty());
258+
/// buff.set_position(6);
259+
/// assert_eq!(buff.split_mut(), ([1, 2, 3, 4, 5].as_mut_slice(), [].as_mut_slice()));
257260
/// ```
258-
#[unstable(feature = "cursor_remaining", issue = "86369")]
259-
pub fn is_empty(&self) -> bool {
260-
self.pos >= self.inner.as_ref().len() as u64
261+
#[unstable(feature = "cursor_split", issue = "86369")]
262+
pub fn split_mut(&mut self) -> (&mut [u8], &mut [u8]) {
263+
let slice = self.inner.as_mut();
264+
let pos = self.pos.min(slice.len() as u64);
265+
slice.split_at_mut(pos as usize)
261266
}
262267
}
263268

@@ -319,15 +324,15 @@ where
319324
T: AsRef<[u8]>,
320325
{
321326
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
322-
let n = Read::read(&mut self.remaining_slice(), buf)?;
327+
let n = Read::read(&mut Cursor::split(self).1, buf)?;
323328
self.pos += n as u64;
324329
Ok(n)
325330
}
326331

327332
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
328333
let prev_written = cursor.written();
329334

330-
Read::read_buf(&mut self.remaining_slice(), cursor.reborrow())?;
335+
Read::read_buf(&mut Cursor::split(self).1, cursor.reborrow())?;
331336

332337
self.pos += (cursor.written() - prev_written) as u64;
333338

@@ -351,7 +356,7 @@ where
351356
}
352357

353358
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
354-
let result = Read::read_exact(&mut self.remaining_slice(), buf);
359+
let result = Read::read_exact(&mut Cursor::split(self).1, buf);
355360

356361
match result {
357362
Ok(_) => self.pos += buf.len() as u64,
@@ -365,14 +370,14 @@ where
365370
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
366371
let prev_written = cursor.written();
367372

368-
let result = Read::read_buf_exact(&mut self.remaining_slice(), cursor.reborrow());
373+
let result = Read::read_buf_exact(&mut Cursor::split(self).1, cursor.reborrow());
369374
self.pos += (cursor.written() - prev_written) as u64;
370375

371376
result
372377
}
373378

374379
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
375-
let content = self.remaining_slice();
380+
let content = Cursor::split(self).1;
376381
let len = content.len();
377382
buf.try_reserve(len)?;
378383
buf.extend_from_slice(content);
@@ -383,7 +388,7 @@ where
383388

384389
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
385390
let content =
386-
crate::str::from_utf8(self.remaining_slice()).map_err(|_| io::Error::INVALID_UTF8)?;
391+
crate::str::from_utf8(Cursor::split(self).1).map_err(|_| io::Error::INVALID_UTF8)?;
387392
let len = content.len();
388393
buf.try_reserve(len)?;
389394
buf.push_str(content);
@@ -399,7 +404,7 @@ where
399404
T: AsRef<[u8]>,
400405
{
401406
fn fill_buf(&mut self) -> io::Result<&[u8]> {
402-
Ok(self.remaining_slice())
407+
Ok(Cursor::split(self).1)
403408
}
404409
fn consume(&mut self, amt: usize) {
405410
self.pos += amt as u64;

library/std/src/io/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -676,13 +676,13 @@ fn cursor_read_exact_eof() {
676676

677677
let mut r = slice.clone();
678678
assert!(r.read_exact(&mut [0; 10]).is_err());
679-
assert!(r.is_empty());
679+
assert!(Cursor::split(&r).1.is_empty());
680680

681681
let mut r = slice;
682682
let buf = &mut [0; 10];
683683
let mut buf = BorrowedBuf::from(buf.as_mut_slice());
684684
assert!(r.read_buf_exact(buf.unfilled()).is_err());
685-
assert!(r.is_empty());
685+
assert!(Cursor::split(&r).1.is_empty());
686686
assert_eq!(buf.filled(), b"123456");
687687
}
688688

0 commit comments

Comments
 (0)