1
Fork 0

Rollup merge of #109174 - soerenmeier:cursor_fns, r=dtolnay

Replace `io::Cursor::{remaining_slice, is_empty}`

This is a late follow up to the concerns raised in https://github.com/rust-lang/rust/issues/86369.

https://github.com/rust-lang/rust/issues/86369#issuecomment-953096691
> 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 https://github.com/rust-lang/rust/issues/86369#issuecomment-927124211 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.
This commit is contained in:
Matthias Krüger 2024-07-29 07:11:13 +02:00 committed by GitHub
commit 1a9f91a43e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 32 deletions

View file

@ -209,55 +209,60 @@ impl<T> Cursor<T>
where where
T: AsRef<[u8]>, T: AsRef<[u8]>,
{ {
/// Returns the remaining slice. /// Splits the underlying slice at the cursor position and returns them.
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(cursor_remaining)] /// #![feature(cursor_split)]
/// use std::io::Cursor; /// use std::io::Cursor;
/// ///
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]); /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
/// ///
/// assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]); /// assert_eq!(buff.split(), ([].as_slice(), [1, 2, 3, 4, 5].as_slice()));
/// ///
/// buff.set_position(2); /// buff.set_position(2);
/// assert_eq!(buff.remaining_slice(), &[3, 4, 5]); /// assert_eq!(buff.split(), ([1, 2].as_slice(), [3, 4, 5].as_slice()));
///
/// buff.set_position(4);
/// assert_eq!(buff.remaining_slice(), &[5]);
/// ///
/// buff.set_position(6); /// buff.set_position(6);
/// assert_eq!(buff.remaining_slice(), &[]); /// assert_eq!(buff.split(), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()));
/// ``` /// ```
#[unstable(feature = "cursor_remaining", issue = "86369")] #[unstable(feature = "cursor_split", issue = "86369")]
pub fn remaining_slice(&self) -> &[u8] { pub fn split(&self) -> (&[u8], &[u8]) {
let len = self.pos.min(self.inner.as_ref().len() as u64); let slice = self.inner.as_ref();
&self.inner.as_ref()[(len as usize)..] let pos = self.pos.min(slice.len() as u64);
slice.split_at(pos as usize)
} }
}
/// Returns `true` if the remaining slice is empty. impl<T> Cursor<T>
where
T: AsMut<[u8]>,
{
/// Splits the underlying slice at the cursor position and returns them
/// mutably.
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(cursor_remaining)] /// #![feature(cursor_split)]
/// use std::io::Cursor; /// use std::io::Cursor;
/// ///
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]); /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
/// ///
/// assert_eq!(buff.split_mut(), ([].as_mut_slice(), [1, 2, 3, 4, 5].as_mut_slice()));
///
/// buff.set_position(2); /// buff.set_position(2);
/// assert!(!buff.is_empty()); /// assert_eq!(buff.split_mut(), ([1, 2].as_mut_slice(), [3, 4, 5].as_mut_slice()));
/// ///
/// buff.set_position(5); /// buff.set_position(6);
/// assert!(buff.is_empty()); /// assert_eq!(buff.split_mut(), ([1, 2, 3, 4, 5].as_mut_slice(), [].as_mut_slice()));
///
/// buff.set_position(10);
/// assert!(buff.is_empty());
/// ``` /// ```
#[unstable(feature = "cursor_remaining", issue = "86369")] #[unstable(feature = "cursor_split", issue = "86369")]
pub fn is_empty(&self) -> bool { pub fn split_mut(&mut self) -> (&mut [u8], &mut [u8]) {
self.pos >= self.inner.as_ref().len() as u64 let slice = self.inner.as_mut();
let pos = self.pos.min(slice.len() as u64);
slice.split_at_mut(pos as usize)
} }
} }
@ -319,7 +324,7 @@ where
T: AsRef<[u8]>, T: AsRef<[u8]>,
{ {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let n = Read::read(&mut self.remaining_slice(), buf)?; let n = Read::read(&mut Cursor::split(self).1, buf)?;
self.pos += n as u64; self.pos += n as u64;
Ok(n) Ok(n)
} }
@ -327,7 +332,7 @@ where
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> { fn read_buf(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
let prev_written = cursor.written(); let prev_written = cursor.written();
Read::read_buf(&mut self.remaining_slice(), cursor.reborrow())?; Read::read_buf(&mut Cursor::split(self).1, cursor.reborrow())?;
self.pos += (cursor.written() - prev_written) as u64; self.pos += (cursor.written() - prev_written) as u64;
@ -351,7 +356,7 @@ where
} }
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
let result = Read::read_exact(&mut self.remaining_slice(), buf); let result = Read::read_exact(&mut Cursor::split(self).1, buf);
match result { match result {
Ok(_) => self.pos += buf.len() as u64, Ok(_) => self.pos += buf.len() as u64,
@ -365,14 +370,14 @@ where
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> { fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
let prev_written = cursor.written(); let prev_written = cursor.written();
let result = Read::read_buf_exact(&mut self.remaining_slice(), cursor.reborrow()); let result = Read::read_buf_exact(&mut Cursor::split(self).1, cursor.reborrow());
self.pos += (cursor.written() - prev_written) as u64; self.pos += (cursor.written() - prev_written) as u64;
result result
} }
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
let content = self.remaining_slice(); let content = Cursor::split(self).1;
let len = content.len(); let len = content.len();
buf.try_reserve(len)?; buf.try_reserve(len)?;
buf.extend_from_slice(content); buf.extend_from_slice(content);
@ -383,7 +388,7 @@ where
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
let content = let content =
crate::str::from_utf8(self.remaining_slice()).map_err(|_| io::Error::INVALID_UTF8)?; crate::str::from_utf8(Cursor::split(self).1).map_err(|_| io::Error::INVALID_UTF8)?;
let len = content.len(); let len = content.len();
buf.try_reserve(len)?; buf.try_reserve(len)?;
buf.push_str(content); buf.push_str(content);
@ -399,7 +404,7 @@ where
T: AsRef<[u8]>, T: AsRef<[u8]>,
{ {
fn fill_buf(&mut self) -> io::Result<&[u8]> { fn fill_buf(&mut self) -> io::Result<&[u8]> {
Ok(self.remaining_slice()) Ok(Cursor::split(self).1)
} }
fn consume(&mut self, amt: usize) { fn consume(&mut self, amt: usize) {
self.pos += amt as u64; self.pos += amt as u64;

View file

@ -676,13 +676,13 @@ fn cursor_read_exact_eof() {
let mut r = slice.clone(); let mut r = slice.clone();
assert!(r.read_exact(&mut [0; 10]).is_err()); assert!(r.read_exact(&mut [0; 10]).is_err());
assert!(r.is_empty()); assert!(Cursor::split(&r).1.is_empty());
let mut r = slice; let mut r = slice;
let buf = &mut [0; 10]; let buf = &mut [0; 10];
let mut buf = BorrowedBuf::from(buf.as_mut_slice()); let mut buf = BorrowedBuf::from(buf.as_mut_slice());
assert!(r.read_buf_exact(buf.unfilled()).is_err()); assert!(r.read_buf_exact(buf.unfilled()).is_err());
assert!(r.is_empty()); assert!(Cursor::split(&r).1.is_empty());
assert_eq!(buf.filled(), b"123456"); assert_eq!(buf.filled(), b"123456");
} }