Skip to content

Commit 664bde0

Browse files
committed
rename remaining to remaining_slice and add a new remaining
1 parent 212e91a commit 664bde0

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

library/std/src/io/cursor.rs

+34-8
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,32 @@ impl<T> Cursor<T>
209209
where
210210
T: AsRef<[u8]>,
211211
{
212+
/// Returns the remaining length.
213+
///
214+
/// # Examples
215+
///
216+
/// ```
217+
/// #![feature(cursor_remaining)]
218+
/// use std::io::Cursor;
219+
///
220+
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
221+
///
222+
/// assert_eq!(buff.remaining(), 5);
223+
///
224+
/// buff.set_position(2);
225+
/// assert_eq!(buff.remaining(), 3);
226+
///
227+
/// buff.set_position(4);
228+
/// assert_eq!(buff.remaining(), 1);
229+
///
230+
/// buff.set_position(6);
231+
/// assert_eq!(buff.remaining(), 0);
232+
/// ```
233+
#[unstable(feature = "cursor_remaining", issue = "86369")]
234+
pub fn remaining(&self) -> u64 {
235+
(self.inner.as_ref().len() as u64).checked_sub(self.pos).unwrap_or(0)
236+
}
237+
212238
/// Returns the remaining slice.
213239
///
214240
/// # Examples
@@ -219,19 +245,19 @@ where
219245
///
220246
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
221247
///
222-
/// assert_eq!(buff.remaining(), &[1, 2, 3, 4, 5]);
248+
/// assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]);
223249
///
224250
/// buff.set_position(2);
225-
/// assert_eq!(buff.remaining(), &[3, 4, 5]);
251+
/// assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
226252
///
227253
/// buff.set_position(4);
228-
/// assert_eq!(buff.remaining(), &[5]);
254+
/// assert_eq!(buff.remaining_slice(), &[5]);
229255
///
230256
/// buff.set_position(6);
231-
/// assert_eq!(buff.remaining(), &[]);
257+
/// assert_eq!(buff.remaining_slice(), &[]);
232258
/// ```
233259
#[unstable(feature = "cursor_remaining", issue = "86369")]
234-
pub fn remaining(&self) -> &[u8] {
260+
pub fn remaining_slice(&self) -> &[u8] {
235261
let len = self.pos.min(self.inner.as_ref().len() as u64);
236262
&self.inner.as_ref()[(len as usize)..]
237263
}
@@ -324,7 +350,7 @@ where
324350
T: AsRef<[u8]>,
325351
{
326352
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
327-
let n = Read::read(&mut self.remaining(), buf)?;
353+
let n = Read::read(&mut self.remaining_slice(), buf)?;
328354
self.pos += n as u64;
329355
Ok(n)
330356
}
@@ -347,7 +373,7 @@ where
347373

348374
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
349375
let n = buf.len();
350-
Read::read_exact(&mut self.remaining(), buf)?;
376+
Read::read_exact(&mut self.remaining_slice(), buf)?;
351377
self.pos += n as u64;
352378
Ok(())
353379
}
@@ -364,7 +390,7 @@ where
364390
T: AsRef<[u8]>,
365391
{
366392
fn fill_buf(&mut self) -> io::Result<&[u8]> {
367-
Ok(self.remaining())
393+
Ok(self.remaining_slice())
368394
}
369395
fn consume(&mut self, amt: usize) {
370396
self.pos += amt as u64;

0 commit comments

Comments
 (0)