From 3803c090f85b3601414e7b6ed4467b38e1de8bf4 Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Sat, 29 May 2021 10:08:00 +0200 Subject: [PATCH 1/4] Rename IoSlice(Mut)::advance to advance_slice To make way for a new IoSlice(Mut)::advance function that advances a single slice. Also changes the signature to accept a `&mut &mut [IoSlice]`, not returning anything. This will better match the future IoSlice::advance function. --- library/std/src/io/mod.rs | 19 +++++++++---------- library/std/src/io/tests.rs | 36 ++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 4c154dbe01a..39b5ba41843 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -253,6 +253,7 @@ mod tests; use crate::cmp; use crate::fmt; +use crate::mem::replace; use crate::ops::{Deref, DerefMut}; use crate::ptr; use crate::slice; @@ -1070,13 +1071,13 @@ impl<'a> IoSliceMut<'a> { /// ][..]; /// /// // Mark 10 bytes as read. - /// bufs = IoSliceMut::advance(bufs, 10); + /// IoSliceMut::advance_slice(&mut bufs, 10); /// assert_eq!(bufs[0].deref(), [2; 14].as_ref()); /// assert_eq!(bufs[1].deref(), [3; 8].as_ref()); /// ``` #[unstable(feature = "io_slice_advance", issue = "62726")] #[inline] - pub fn advance<'b>(bufs: &'b mut [IoSliceMut<'a>], n: usize) -> &'b mut [IoSliceMut<'a>] { + pub fn advance_slice(bufs: &mut &mut [IoSliceMut<'a>], n: usize) { // Number of buffers to remove. let mut remove = 0; // Total length of all the to be removed buffers. @@ -1090,11 +1091,10 @@ impl<'a> IoSliceMut<'a> { } } - let bufs = &mut bufs[remove..]; + *bufs = &mut replace(bufs, &mut [])[remove..]; if !bufs.is_empty() { bufs[0].0.advance(n - accumulated_len) } - bufs } } @@ -1179,12 +1179,12 @@ impl<'a> IoSlice<'a> { /// ][..]; /// /// // Mark 10 bytes as written. - /// bufs = IoSlice::advance(bufs, 10); + /// IoSlice::advance_slice(&mut bufs, 10); /// assert_eq!(bufs[0].deref(), [2; 14].as_ref()); /// assert_eq!(bufs[1].deref(), [3; 8].as_ref()); #[unstable(feature = "io_slice_advance", issue = "62726")] #[inline] - pub fn advance<'b>(bufs: &'b mut [IoSlice<'a>], n: usize) -> &'b mut [IoSlice<'a>] { + pub fn advance_slice(bufs: &mut &mut [IoSlice<'a>], n: usize) { // Number of buffers to remove. let mut remove = 0; // Total length of all the to be removed buffers. @@ -1198,11 +1198,10 @@ impl<'a> IoSlice<'a> { } } - let bufs = &mut bufs[remove..]; + *bufs = &mut replace(bufs, &mut [])[remove..]; if !bufs.is_empty() { bufs[0].0.advance(n - accumulated_len) } - bufs } } @@ -1511,7 +1510,7 @@ pub trait Write { fn write_all_vectored(&mut self, mut bufs: &mut [IoSlice<'_>]) -> Result<()> { // Guarantee that bufs is empty if it contains no data, // to avoid calling write_vectored if there is no data to be written. - bufs = IoSlice::advance(bufs, 0); + IoSlice::advance_slice(&mut bufs, 0); while !bufs.is_empty() { match self.write_vectored(bufs) { Ok(0) => { @@ -1520,7 +1519,7 @@ pub trait Write { &"failed to write whole buffer", )); } - Ok(n) => bufs = IoSlice::advance(bufs, n), + Ok(n) => IoSlice::advance_slice(&mut bufs, n), Err(ref e) if e.kind() == ErrorKind::Interrupted => {} Err(e) => return Err(e), } diff --git a/library/std/src/io/tests.rs b/library/std/src/io/tests.rs index 2b14e161503..0618a03a680 100644 --- a/library/std/src/io/tests.rs +++ b/library/std/src/io/tests.rs @@ -353,7 +353,7 @@ fn test_read_to_end_capacity() -> io::Result<()> { } #[test] -fn io_slice_mut_advance() { +fn io_slice_mut_advance_slice() { let mut buf1 = [1; 8]; let mut buf2 = [2; 16]; let mut buf3 = [3; 8]; @@ -364,75 +364,75 @@ fn io_slice_mut_advance() { ][..]; // Only in a single buffer.. - bufs = IoSliceMut::advance(bufs, 1); + IoSliceMut::advance_slice(&mut bufs, 1); assert_eq!(bufs[0].deref(), [1; 7].as_ref()); assert_eq!(bufs[1].deref(), [2; 16].as_ref()); assert_eq!(bufs[2].deref(), [3; 8].as_ref()); // Removing a buffer, leaving others as is. - bufs = IoSliceMut::advance(bufs, 7); + IoSliceMut::advance_slice(&mut bufs, 7); assert_eq!(bufs[0].deref(), [2; 16].as_ref()); assert_eq!(bufs[1].deref(), [3; 8].as_ref()); // Removing a buffer and removing from the next buffer. - bufs = IoSliceMut::advance(bufs, 18); + IoSliceMut::advance_slice(&mut bufs, 18); assert_eq!(bufs[0].deref(), [3; 6].as_ref()); } #[test] -fn io_slice_mut_advance_empty_slice() { - let empty_bufs = &mut [][..]; +fn io_slice_mut_advance_slice_empty_slice() { + let mut empty_bufs = &mut [][..]; // Shouldn't panic. - IoSliceMut::advance(empty_bufs, 1); + IoSliceMut::advance_slice(&mut empty_bufs, 1); } #[test] -fn io_slice_mut_advance_beyond_total_length() { +fn io_slice_mut_advance_slice_beyond_total_length() { let mut buf1 = [1; 8]; let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..]; // Going beyond the total length should be ok. - bufs = IoSliceMut::advance(bufs, 9); + IoSliceMut::advance_slice(&mut bufs, 9); assert!(bufs.is_empty()); } #[test] -fn io_slice_advance() { +fn io_slice_advance_slice() { let buf1 = [1; 8]; let buf2 = [2; 16]; let buf3 = [3; 8]; let mut bufs = &mut [IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3)][..]; // Only in a single buffer.. - bufs = IoSlice::advance(bufs, 1); + IoSlice::advance_slice(&mut bufs, 1); assert_eq!(bufs[0].deref(), [1; 7].as_ref()); assert_eq!(bufs[1].deref(), [2; 16].as_ref()); assert_eq!(bufs[2].deref(), [3; 8].as_ref()); // Removing a buffer, leaving others as is. - bufs = IoSlice::advance(bufs, 7); + IoSlice::advance_slice(&mut bufs, 7); assert_eq!(bufs[0].deref(), [2; 16].as_ref()); assert_eq!(bufs[1].deref(), [3; 8].as_ref()); // Removing a buffer and removing from the next buffer. - bufs = IoSlice::advance(bufs, 18); + IoSlice::advance_slice(&mut bufs, 18); assert_eq!(bufs[0].deref(), [3; 6].as_ref()); } #[test] -fn io_slice_advance_empty_slice() { - let empty_bufs = &mut [][..]; +fn io_slice_advance_slice_empty_slice() { + let mut empty_bufs = &mut [][..]; // Shouldn't panic. - IoSlice::advance(empty_bufs, 1); + IoSlice::advance_slice(&mut empty_bufs, 1); } #[test] -fn io_slice_advance_beyond_total_length() { +fn io_slice_advance_slice_beyond_total_length() { let buf1 = [1; 8]; let mut bufs = &mut [IoSlice::new(&buf1)][..]; // Going beyond the total length should be ok. - bufs = IoSlice::advance(bufs, 9); + IoSlice::advance_slice(&mut bufs, 9); assert!(bufs.is_empty()); } From 49e25b5ef2be2577693aee585b58e0b6dbd79c1d Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Sat, 29 May 2021 10:18:19 +0200 Subject: [PATCH 2/4] Add IoSlice(Mut)::advance Advance the internal cursor of a single slice. --- library/std/src/io/mod.rs | 56 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 39b5ba41843..4746db5a9fd 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1045,6 +1045,32 @@ impl<'a> IoSliceMut<'a> { /// Advance the internal cursor of the slice. /// + /// Also see [`IoSliceMut::advance_slice`] to advance the cursors of + /// multiple buffers. + /// + /// # Examples + /// + /// ``` + /// #![feature(io_slice_advance)] + /// + /// use std::io::IoSliceMut; + /// use std::ops::Deref; + /// + /// let mut data = [1; 8]; + /// let mut buf = IoSliceMut::new(&mut data); + /// + /// // Mark 10 bytes as read. + /// buf.advance(3); + /// assert_eq!(buf.deref(), [1; 5].as_ref()); + /// ``` + #[unstable(feature = "io_slice_advance", issue = "62726")] + #[inline] + pub fn advance(&mut self, n: usize) { + self.0.advance(n) + } + + /// Advance the internal cursor of the slices. + /// /// # Notes /// /// Elements in the slice may be modified if the cursor is not advanced to @@ -1093,7 +1119,7 @@ impl<'a> IoSliceMut<'a> { *bufs = &mut replace(bufs, &mut [])[remove..]; if !bufs.is_empty() { - bufs[0].0.advance(n - accumulated_len) + bufs[0].advance(n - accumulated_len) } } } @@ -1153,6 +1179,32 @@ impl<'a> IoSlice<'a> { /// Advance the internal cursor of the slice. /// + /// Also see [`IoSlice::advance_slice`] to advance the cursors of multiple + /// buffers. + /// + /// # Examples + /// + /// ``` + /// #![feature(io_slice_advance)] + /// + /// use std::io::IoSlice; + /// use std::ops::Deref; + /// + /// let mut data = [1; 8]; + /// let mut buf = IoSlice::new(&mut data); + /// + /// // Mark 10 bytes as read. + /// buf.advance(3); + /// assert_eq!(buf.deref(), [1; 5].as_ref()); + /// ``` + #[unstable(feature = "io_slice_advance", issue = "62726")] + #[inline] + pub fn advance(&mut self, n: usize) { + self.0.advance(n) + } + + /// Advance the internal cursor of the slices. + /// /// # Notes /// /// Elements in the slice may be modified if the cursor is not advanced to @@ -1200,7 +1252,7 @@ impl<'a> IoSlice<'a> { *bufs = &mut replace(bufs, &mut [])[remove..]; if !bufs.is_empty() { - bufs[0].0.advance(n - accumulated_len) + bufs[0].advance(n - accumulated_len) } } } From fd14c5207544971aab92bbbd3d2a47723b91b284 Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Sat, 5 Jun 2021 13:06:10 +0200 Subject: [PATCH 3/4] Rename IoSlice(Mut)::advance_slice to advance_slices --- library/std/src/io/mod.rs | 16 ++++++++-------- library/std/src/io/tests.rs | 32 ++++++++++++++++---------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 4746db5a9fd..7187ec71bc9 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1045,7 +1045,7 @@ impl<'a> IoSliceMut<'a> { /// Advance the internal cursor of the slice. /// - /// Also see [`IoSliceMut::advance_slice`] to advance the cursors of + /// Also see [`IoSliceMut::advance_slices`] to advance the cursors of /// multiple buffers. /// /// # Examples @@ -1097,13 +1097,13 @@ impl<'a> IoSliceMut<'a> { /// ][..]; /// /// // Mark 10 bytes as read. - /// IoSliceMut::advance_slice(&mut bufs, 10); + /// IoSliceMut::advance_slices(&mut bufs, 10); /// assert_eq!(bufs[0].deref(), [2; 14].as_ref()); /// assert_eq!(bufs[1].deref(), [3; 8].as_ref()); /// ``` #[unstable(feature = "io_slice_advance", issue = "62726")] #[inline] - pub fn advance_slice(bufs: &mut &mut [IoSliceMut<'a>], n: usize) { + pub fn advance_slices(bufs: &mut &mut [IoSliceMut<'a>], n: usize) { // Number of buffers to remove. let mut remove = 0; // Total length of all the to be removed buffers. @@ -1179,7 +1179,7 @@ impl<'a> IoSlice<'a> { /// Advance the internal cursor of the slice. /// - /// Also see [`IoSlice::advance_slice`] to advance the cursors of multiple + /// Also see [`IoSlice::advance_slices`] to advance the cursors of multiple /// buffers. /// /// # Examples @@ -1231,12 +1231,12 @@ impl<'a> IoSlice<'a> { /// ][..]; /// /// // Mark 10 bytes as written. - /// IoSlice::advance_slice(&mut bufs, 10); + /// IoSlice::advance_slices(&mut bufs, 10); /// assert_eq!(bufs[0].deref(), [2; 14].as_ref()); /// assert_eq!(bufs[1].deref(), [3; 8].as_ref()); #[unstable(feature = "io_slice_advance", issue = "62726")] #[inline] - pub fn advance_slice(bufs: &mut &mut [IoSlice<'a>], n: usize) { + pub fn advance_slices(bufs: &mut &mut [IoSlice<'a>], n: usize) { // Number of buffers to remove. let mut remove = 0; // Total length of all the to be removed buffers. @@ -1562,7 +1562,7 @@ pub trait Write { fn write_all_vectored(&mut self, mut bufs: &mut [IoSlice<'_>]) -> Result<()> { // Guarantee that bufs is empty if it contains no data, // to avoid calling write_vectored if there is no data to be written. - IoSlice::advance_slice(&mut bufs, 0); + IoSlice::advance_slices(&mut bufs, 0); while !bufs.is_empty() { match self.write_vectored(bufs) { Ok(0) => { @@ -1571,7 +1571,7 @@ pub trait Write { &"failed to write whole buffer", )); } - Ok(n) => IoSlice::advance_slice(&mut bufs, n), + Ok(n) => IoSlice::advance_slices(&mut bufs, n), Err(ref e) if e.kind() == ErrorKind::Interrupted => {} Err(e) => return Err(e), } diff --git a/library/std/src/io/tests.rs b/library/std/src/io/tests.rs index 0618a03a680..df0dc7e9d31 100644 --- a/library/std/src/io/tests.rs +++ b/library/std/src/io/tests.rs @@ -353,7 +353,7 @@ fn test_read_to_end_capacity() -> io::Result<()> { } #[test] -fn io_slice_mut_advance_slice() { +fn io_slice_mut_advance_slices() { let mut buf1 = [1; 8]; let mut buf2 = [2; 16]; let mut buf3 = [3; 8]; @@ -364,75 +364,75 @@ fn io_slice_mut_advance_slice() { ][..]; // Only in a single buffer.. - IoSliceMut::advance_slice(&mut bufs, 1); + IoSliceMut::advance_slices(&mut bufs, 1); assert_eq!(bufs[0].deref(), [1; 7].as_ref()); assert_eq!(bufs[1].deref(), [2; 16].as_ref()); assert_eq!(bufs[2].deref(), [3; 8].as_ref()); // Removing a buffer, leaving others as is. - IoSliceMut::advance_slice(&mut bufs, 7); + IoSliceMut::advance_slices(&mut bufs, 7); assert_eq!(bufs[0].deref(), [2; 16].as_ref()); assert_eq!(bufs[1].deref(), [3; 8].as_ref()); // Removing a buffer and removing from the next buffer. - IoSliceMut::advance_slice(&mut bufs, 18); + IoSliceMut::advance_slices(&mut bufs, 18); assert_eq!(bufs[0].deref(), [3; 6].as_ref()); } #[test] -fn io_slice_mut_advance_slice_empty_slice() { +fn io_slice_mut_advance_slices_empty_slice() { let mut empty_bufs = &mut [][..]; // Shouldn't panic. - IoSliceMut::advance_slice(&mut empty_bufs, 1); + IoSliceMut::advance_slices(&mut empty_bufs, 1); } #[test] -fn io_slice_mut_advance_slice_beyond_total_length() { +fn io_slice_mut_advance_slices_beyond_total_length() { let mut buf1 = [1; 8]; let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..]; // Going beyond the total length should be ok. - IoSliceMut::advance_slice(&mut bufs, 9); + IoSliceMut::advance_slices(&mut bufs, 9); assert!(bufs.is_empty()); } #[test] -fn io_slice_advance_slice() { +fn io_slice_advance_slices() { let buf1 = [1; 8]; let buf2 = [2; 16]; let buf3 = [3; 8]; let mut bufs = &mut [IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3)][..]; // Only in a single buffer.. - IoSlice::advance_slice(&mut bufs, 1); + IoSlice::advance_slices(&mut bufs, 1); assert_eq!(bufs[0].deref(), [1; 7].as_ref()); assert_eq!(bufs[1].deref(), [2; 16].as_ref()); assert_eq!(bufs[2].deref(), [3; 8].as_ref()); // Removing a buffer, leaving others as is. - IoSlice::advance_slice(&mut bufs, 7); + IoSlice::advance_slices(&mut bufs, 7); assert_eq!(bufs[0].deref(), [2; 16].as_ref()); assert_eq!(bufs[1].deref(), [3; 8].as_ref()); // Removing a buffer and removing from the next buffer. - IoSlice::advance_slice(&mut bufs, 18); + IoSlice::advance_slices(&mut bufs, 18); assert_eq!(bufs[0].deref(), [3; 6].as_ref()); } #[test] -fn io_slice_advance_slice_empty_slice() { +fn io_slice_advance_slices_empty_slice() { let mut empty_bufs = &mut [][..]; // Shouldn't panic. - IoSlice::advance_slice(&mut empty_bufs, 1); + IoSlice::advance_slices(&mut empty_bufs, 1); } #[test] -fn io_slice_advance_slice_beyond_total_length() { +fn io_slice_advance_slices_beyond_total_length() { let buf1 = [1; 8]; let mut bufs = &mut [IoSlice::new(&buf1)][..]; // Going beyond the total length should be ok. - IoSlice::advance_slice(&mut bufs, 9); + IoSlice::advance_slices(&mut bufs, 9); assert!(bufs.is_empty()); } From 5e7a8c6eb12e1fb84cf2538cf87db32c54b938a2 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 17 Jun 2021 12:13:06 +0200 Subject: [PATCH 4/4] Fix typos in code examples. --- library/std/src/io/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 7187ec71bc9..f57529767a9 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1059,7 +1059,7 @@ impl<'a> IoSliceMut<'a> { /// let mut data = [1; 8]; /// let mut buf = IoSliceMut::new(&mut data); /// - /// // Mark 10 bytes as read. + /// // Mark 3 bytes as read. /// buf.advance(3); /// assert_eq!(buf.deref(), [1; 5].as_ref()); /// ``` @@ -1193,7 +1193,7 @@ impl<'a> IoSlice<'a> { /// let mut data = [1; 8]; /// let mut buf = IoSlice::new(&mut data); /// - /// // Mark 10 bytes as read. + /// // Mark 3 bytes as read. /// buf.advance(3); /// assert_eq!(buf.deref(), [1; 5].as_ref()); /// ```