From 4038ca0965151b42afa4692b23b2eeaa4236a29f Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 7 Jan 2022 18:11:59 -0800 Subject: [PATCH 1/4] Refactor tests of Write for Cursor<_> --- library/std/src/io/cursor/tests.rs | 72 ++++++++++++------------------ 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/library/std/src/io/cursor/tests.rs b/library/std/src/io/cursor/tests.rs index 5da31ce0ba761..2d0836d9a8595 100644 --- a/library/std/src/io/cursor/tests.rs +++ b/library/std/src/io/cursor/tests.rs @@ -50,9 +50,11 @@ fn test_mem_mut_writer() { assert_eq!(&writer.get_ref()[..], b); } -#[test] -fn test_box_slice_writer() { - let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice()); +fn test_slice_writer(writer: &mut Cursor) +where + T: AsRef<[u8]>, + Cursor: Write, +{ assert_eq!(writer.position(), 0); assert_eq!(writer.write(&[0]).unwrap(), 1); assert_eq!(writer.position(), 1); @@ -65,12 +67,14 @@ fn test_box_slice_writer() { assert_eq!(writer.write(&[8, 9]).unwrap(), 1); assert_eq!(writer.write(&[10]).unwrap(), 0); let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8]; - assert_eq!(&**writer.get_ref(), b); + assert_eq!(writer.get_ref().as_ref(), b); } -#[test] -fn test_box_slice_writer_vectored() { - let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice()); +fn test_slice_writer_vectored(writer: &mut Cursor) +where + T: AsRef<[u8]>, + Cursor: Write, +{ assert_eq!(writer.position(), 0); assert_eq!(writer.write_vectored(&[IoSlice::new(&[0])]).unwrap(), 1); assert_eq!(writer.position(), 1); @@ -85,53 +89,33 @@ fn test_box_slice_writer_vectored() { assert_eq!(writer.write_vectored(&[IoSlice::new(&[8, 9])]).unwrap(), 1); assert_eq!(writer.write_vectored(&[IoSlice::new(&[10])]).unwrap(), 0); let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8]; - assert_eq!(&**writer.get_ref(), b); + assert_eq!(writer.get_ref().as_ref(), b); +} + +#[test] +fn test_box_slice_writer() { + let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice()); + test_slice_writer(&mut writer); +} + +#[test] +fn test_box_slice_writer_vectored() { + let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice()); + test_slice_writer_vectored(&mut writer); } #[test] fn test_buf_writer() { let mut buf = [0 as u8; 9]; - { - let mut writer = Cursor::new(&mut buf[..]); - assert_eq!(writer.position(), 0); - assert_eq!(writer.write(&[0]).unwrap(), 1); - assert_eq!(writer.position(), 1); - assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3); - assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4); - assert_eq!(writer.position(), 8); - assert_eq!(writer.write(&[]).unwrap(), 0); - assert_eq!(writer.position(), 8); - - assert_eq!(writer.write(&[8, 9]).unwrap(), 1); - assert_eq!(writer.write(&[10]).unwrap(), 0); - } - let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8]; - assert_eq!(buf, b); + let mut writer = Cursor::new(&mut buf[..]); + test_slice_writer(&mut writer); } #[test] fn test_buf_writer_vectored() { let mut buf = [0 as u8; 9]; - { - let mut writer = Cursor::new(&mut buf[..]); - assert_eq!(writer.position(), 0); - assert_eq!(writer.write_vectored(&[IoSlice::new(&[0])]).unwrap(), 1); - assert_eq!(writer.position(), 1); - assert_eq!( - writer - .write_vectored(&[IoSlice::new(&[1, 2, 3]), IoSlice::new(&[4, 5, 6, 7])],) - .unwrap(), - 7, - ); - assert_eq!(writer.position(), 8); - assert_eq!(writer.write_vectored(&[]).unwrap(), 0); - assert_eq!(writer.position(), 8); - - assert_eq!(writer.write_vectored(&[IoSlice::new(&[8, 9])]).unwrap(), 1); - assert_eq!(writer.write_vectored(&[IoSlice::new(&[10])]).unwrap(), 0); - } - let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8]; - assert_eq!(buf, b); + let mut writer = Cursor::new(&mut buf[..]); + test_slice_writer_vectored(&mut writer); } #[test] From a04d553e882196146aaa632cf00877c4af37d447 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 19 Jan 2022 15:57:36 -0800 Subject: [PATCH 2/4] impl Write for Cursor<[u8; N]> --- library/std/src/io/cursor.rs | 23 +++++++++++++++++++++++ library/std/src/io/cursor/tests.rs | 12 ++++++++++++ 2 files changed, 35 insertions(+) diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs index 416cc906e65a5..7670a8592925f 100644 --- a/library/std/src/io/cursor.rs +++ b/library/std/src/io/cursor.rs @@ -525,3 +525,26 @@ impl Write for Cursor> { Ok(()) } } + +#[stable(feature = "cursor_array", since = "1.60.0")] +impl Write for Cursor<[u8; N]> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { + slice_write(&mut self.pos, &mut self.inner, buf) + } + + #[inline] + fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { + slice_write_vectored(&mut self.pos, &mut self.inner, bufs) + } + + #[inline] + fn is_write_vectored(&self) -> bool { + true + } + + #[inline] + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} diff --git a/library/std/src/io/cursor/tests.rs b/library/std/src/io/cursor/tests.rs index 2d0836d9a8595..f1ee177b7f3bc 100644 --- a/library/std/src/io/cursor/tests.rs +++ b/library/std/src/io/cursor/tests.rs @@ -104,6 +104,18 @@ fn test_box_slice_writer_vectored() { test_slice_writer_vectored(&mut writer); } +#[test] +fn test_array_writer() { + let mut writer = Cursor::new([0u8; 9]); + test_slice_writer(&mut writer); +} + +#[test] +fn test_array_writer_vectored() { + let mut writer = Cursor::new([0u8; 9]); + test_slice_writer_vectored(&mut writer); +} + #[test] fn test_buf_writer() { let mut buf = [0 as u8; 9]; From 38cef656d8df6e77630f923e2e8e20862adfc4e4 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 19 Jan 2022 16:07:14 -0800 Subject: [PATCH 3/4] Write for Cursor with a custom Allocator --- library/std/src/io/cursor.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs index 7670a8592925f..ea22ebd3e68e8 100644 --- a/library/std/src/io/cursor.rs +++ b/library/std/src/io/cursor.rs @@ -3,6 +3,7 @@ mod tests; use crate::io::prelude::*; +use crate::alloc::Allocator; use crate::cmp; use crate::io::{self, Error, ErrorKind, IoSlice, IoSliceMut, ReadBuf, SeekFrom}; @@ -398,7 +399,10 @@ fn slice_write_vectored( } // Resizing write implementation -fn vec_write(pos_mut: &mut u64, vec: &mut Vec, buf: &[u8]) -> io::Result { +fn vec_write(pos_mut: &mut u64, vec: &mut Vec, buf: &[u8]) -> io::Result +where + A: Allocator, +{ let pos: usize = (*pos_mut).try_into().map_err(|_| { Error::new_const( ErrorKind::InvalidInput, @@ -426,11 +430,14 @@ fn vec_write(pos_mut: &mut u64, vec: &mut Vec, buf: &[u8]) -> io::Result( pos_mut: &mut u64, - vec: &mut Vec, + vec: &mut Vec, bufs: &[IoSlice<'_>], -) -> io::Result { +) -> io::Result +where + A: Allocator, +{ let mut nwritten = 0; for buf in bufs { nwritten += vec_write(pos_mut, vec, buf)?; @@ -462,7 +469,10 @@ impl Write for Cursor<&mut [u8]> { } #[stable(feature = "cursor_mut_vec", since = "1.25.0")] -impl Write for Cursor<&mut Vec> { +impl Write for Cursor<&mut Vec> +where + A: Allocator, +{ fn write(&mut self, buf: &[u8]) -> io::Result { vec_write(&mut self.pos, self.inner, buf) } @@ -483,7 +493,10 @@ impl Write for Cursor<&mut Vec> { } #[stable(feature = "rust1", since = "1.0.0")] -impl Write for Cursor> { +impl Write for Cursor> +where + A: Allocator, +{ fn write(&mut self, buf: &[u8]) -> io::Result { vec_write(&mut self.pos, &mut self.inner, buf) } @@ -504,7 +517,10 @@ impl Write for Cursor> { } #[stable(feature = "cursor_box_slice", since = "1.5.0")] -impl Write for Cursor> { +impl Write for Cursor> +where + A: Allocator, +{ #[inline] fn write(&mut self, buf: &[u8]) -> io::Result { slice_write(&mut self.pos, &mut self.inner, buf) From 7d44316bcf3fbe220412589f08befb3116328b21 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 18 Mar 2022 15:04:37 -0700 Subject: [PATCH 4/4] Bump impl Write for Cursor<[u8; N]> to 1.61 --- library/std/src/io/cursor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs index ea22ebd3e68e8..ddba9f8e9e380 100644 --- a/library/std/src/io/cursor.rs +++ b/library/std/src/io/cursor.rs @@ -542,7 +542,7 @@ where } } -#[stable(feature = "cursor_array", since = "1.60.0")] +#[stable(feature = "cursor_array", since = "1.61.0")] impl Write for Cursor<[u8; N]> { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result {