Skip to content

Commit a2285c9

Browse files
committed
Replace io::Cursor::{remaining_slice, is_empty} with io::Cursor::{split, split_mut, before, before_mut, after, after_mut}
1 parent e4b9f86 commit a2285c9

File tree

1 file changed

+121
-24
lines changed

1 file changed

+121
-24
lines changed

library/std/src/io/cursor.rs

+121-24
Original file line numberDiff line numberDiff line change
@@ -208,55 +208,152 @@ impl<T> Cursor<T>
208208
where
209209
T: AsRef<[u8]>,
210210
{
211-
/// Returns the remaining slice.
211+
/// Splits the underlying slice at the cursor position and returns them.
212212
///
213213
/// # Examples
214214
///
215215
/// ```
216-
/// #![feature(cursor_remaining)]
216+
/// #![feature(cursor_split)]
217217
/// use std::io::Cursor;
218218
///
219219
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
220220
///
221-
/// assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]);
221+
/// assert_eq!(buff.split(), ([].as_slice(), [1, 2, 3, 4, 5].as_slice()));
222222
///
223223
/// buff.set_position(2);
224-
/// assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
224+
/// assert_eq!(buff.split(), ([1, 2].as_slice(), [3, 4, 5].as_slice()));
225225
///
226-
/// buff.set_position(4);
227-
/// assert_eq!(buff.remaining_slice(), &[5]);
226+
/// buff.set_position(6);
227+
/// assert_eq!(buff.split(), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()));
228+
/// ```
229+
#[unstable(feature = "cursor_split", issue = "86369")]
230+
pub fn split(&self) -> (&[u8], &[u8]) {
231+
let slice = self.inner.as_ref();
232+
let pos = self.pos.min(slice.len() as u64);
233+
slice.split_at(pos as usize)
234+
}
235+
236+
/// Returns the slice before the cursor position.
237+
///
238+
/// # Examples
239+
///
240+
/// ```
241+
/// #![feature(cursor_split)]
242+
/// use std::io::Cursor;
243+
///
244+
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
245+
///
246+
/// assert_eq!(buff.before(), &[]);
247+
///
248+
/// buff.set_position(2);
249+
/// assert_eq!(buff.before(), &[1, 2]);
228250
///
229251
/// buff.set_position(6);
230-
/// assert_eq!(buff.remaining_slice(), &[]);
252+
/// assert_eq!(buff.before(), &[1, 2, 3, 4, 5]);
231253
/// ```
232-
#[unstable(feature = "cursor_remaining", issue = "86369")]
233-
pub fn remaining_slice(&self) -> &[u8] {
234-
let len = self.pos.min(self.inner.as_ref().len() as u64);
235-
&self.inner.as_ref()[(len as usize)..]
254+
#[unstable(feature = "cursor_split", issue = "86369")]
255+
pub fn before(&self) -> &[u8] {
256+
self.split().0
236257
}
237258

238-
/// Returns `true` if the remaining slice is empty.
259+
/// Returns the slice after the cursor position.
239260
///
240261
/// # Examples
241262
///
242263
/// ```
243-
/// #![feature(cursor_remaining)]
264+
/// #![feature(cursor_split)]
244265
/// use std::io::Cursor;
245266
///
246267
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
247268
///
269+
/// assert_eq!(buff.after(), &[1, 2, 3, 4, 5]);
270+
///
248271
/// buff.set_position(2);
249-
/// assert!(!buff.is_empty());
272+
/// assert_eq!(buff.after(), &[3, 4, 5]);
250273
///
251-
/// buff.set_position(5);
252-
/// assert!(buff.is_empty());
274+
/// buff.set_position(6);
275+
/// assert_eq!(buff.after(), &[]);
276+
/// ```
277+
#[unstable(feature = "cursor_split", issue = "86369")]
278+
pub fn after(&self) -> &[u8] {
279+
self.split().1
280+
}
281+
}
282+
283+
impl<T> Cursor<T>
284+
where
285+
T: AsMut<[u8]>,
286+
{
287+
/// Splits the underlying slice at the cursor position and returns them
288+
/// mutably.
289+
///
290+
/// # Examples
291+
///
292+
/// ```
293+
/// #![feature(cursor_split)]
294+
/// use std::io::Cursor;
253295
///
254-
/// buff.set_position(10);
255-
/// assert!(buff.is_empty());
296+
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
297+
///
298+
/// assert_eq!(buff.split_mut(), ([].as_mut_slice(), [1, 2, 3, 4, 5].as_mut_slice()));
299+
///
300+
/// buff.set_position(2);
301+
/// assert_eq!(buff.split_mut(), ([1, 2].as_mut_slice(), [3, 4, 5].as_mut_slice()));
302+
///
303+
/// buff.set_position(6);
304+
/// assert_eq!(buff.split_mut(), ([1, 2, 3, 4, 5].as_mut_slice(), [].as_mut_slice()));
305+
/// ```
306+
#[unstable(feature = "cursor_split", issue = "86369")]
307+
pub fn split_mut(&mut self) -> (&mut [u8], &mut [u8]) {
308+
let slice = self.inner.as_mut();
309+
let pos = self.pos.min(slice.len() as u64);
310+
slice.split_at_mut(pos as usize)
311+
}
312+
313+
/// Returns the mutable slice before the cursor position.
314+
///
315+
/// # Examples
316+
///
317+
/// ```
318+
/// #![feature(cursor_split)]
319+
/// use std::io::Cursor;
320+
///
321+
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
322+
///
323+
/// assert_eq!(buff.before_mut(), &[]);
324+
///
325+
/// buff.set_position(2);
326+
/// assert_eq!(buff.before_mut(), &[1, 2]);
327+
///
328+
/// buff.set_position(6);
329+
/// assert_eq!(buff.before_mut(), &[1, 2, 3, 4, 5]);
330+
/// ```
331+
#[unstable(feature = "cursor_split", issue = "86369")]
332+
pub fn before_mut(&mut self) -> &mut [u8] {
333+
self.split_mut().0
334+
}
335+
336+
/// Returns the mutable slice after the cursor position.
337+
///
338+
/// # Examples
339+
///
340+
/// ```
341+
/// #![feature(cursor_split)]
342+
/// use std::io::Cursor;
343+
///
344+
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
345+
///
346+
/// assert_eq!(buff.after_mut(), &[1, 2, 3, 4, 5]);
347+
///
348+
/// buff.set_position(2);
349+
/// assert_eq!(buff.after_mut(), &[3, 4, 5]);
350+
///
351+
/// buff.set_position(6);
352+
/// assert_eq!(buff.after_mut(), &[]);
256353
/// ```
257-
#[unstable(feature = "cursor_remaining", issue = "86369")]
258-
pub fn is_empty(&self) -> bool {
259-
self.pos >= self.inner.as_ref().len() as u64
354+
#[unstable(feature = "cursor_split", issue = "86369")]
355+
pub fn after_mut(&mut self) -> &mut [u8] {
356+
self.split_mut().1
260357
}
261358
}
262359

@@ -318,7 +415,7 @@ where
318415
T: AsRef<[u8]>,
319416
{
320417
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
321-
let n = Read::read(&mut self.remaining_slice(), buf)?;
418+
let n = Read::read(&mut self.after(), buf)?;
322419
self.pos += n as u64;
323420
Ok(n)
324421
}
@@ -351,7 +448,7 @@ where
351448

352449
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
353450
let n = buf.len();
354-
Read::read_exact(&mut self.remaining_slice(), buf)?;
451+
Read::read_exact(&mut self.after(), buf)?;
355452
self.pos += n as u64;
356453
Ok(())
357454
}
@@ -363,7 +460,7 @@ where
363460
T: AsRef<[u8]>,
364461
{
365462
fn fill_buf(&mut self) -> io::Result<&[u8]> {
366-
Ok(self.remaining_slice())
463+
Ok(self.after())
367464
}
368465
fn consume(&mut self, amt: usize) {
369466
self.pos += amt as u64;

0 commit comments

Comments
 (0)