From a623c5233ae7f6b540e5c00f2be02f40b33b0793 Mon Sep 17 00:00:00 2001 From: Mike Pedersen Date: Mon, 29 Aug 2022 19:20:22 +0200 Subject: [PATCH] Add as_slice/into_slice for IoSlice/IoSliceMut --- library/std/src/io/mod.rs | 14 ++++++++++++++ library/std/src/io/tests.rs | 14 ++++++++++++++ library/std/src/sys/solid/io.rs | 11 ++++++++--- library/std/src/sys/unix/io.rs | 7 ++++++- library/std/src/sys/unsupported/io.rs | 7 ++++++- library/std/src/sys/wasi/io.rs | 7 ++++++- library/std/src/sys/windows/io.rs | 7 ++++++- 7 files changed, 60 insertions(+), 7 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 96addbd1a0558..9a5827b800a6a 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1167,6 +1167,13 @@ impl<'a> IoSliceMut<'a> { bufs[0].advance(n - accumulated_len) } } + + /// Returns the slice this `IoSlice` was originally created with. + #[unstable(feature = "io_slice_cast", issue = "none")] + #[inline] + pub fn into_slice(self) -> &'a mut [u8] { + self.0.into_slice() + } } #[stable(feature = "iovec", since = "1.36.0")] @@ -1310,6 +1317,13 @@ impl<'a> IoSlice<'a> { bufs[0].advance(n - accumulated_len) } } + + /// Returns the slice this `IoSlice` was originally created with. + #[unstable(feature = "io_slice_cast", issue = "none")] + #[inline] + pub fn as_slice(&self) -> &'a [u8] { + self.0.as_slice() + } } #[stable(feature = "iovec", since = "1.36.0")] diff --git a/library/std/src/io/tests.rs b/library/std/src/io/tests.rs index 68a19eccc0e7c..e71cad64682ec 100644 --- a/library/std/src/io/tests.rs +++ b/library/std/src/io/tests.rs @@ -480,6 +480,20 @@ fn io_slice_advance_slices_beyond_total_length() { assert!(bufs.is_empty()); } +#[test] +fn io_slice_as_slice() { + let buf = [1; 8]; + let slice = IoSlice::new(&buf).as_slice(); + assert_eq!(slice, buf); +} + +#[test] +fn io_slice_into_slice() { + let mut buf = [1; 8]; + let slice = IoSliceMut::new(&mut buf).into_slice(); + assert_eq!(slice, [1; 8]); +} + /// Create a new writer that reads from at most `n_bufs` and reads /// `per_call` bytes (in total) per call to write. fn test_writer(n_bufs: usize, per_call: usize) -> TestWriter { diff --git a/library/std/src/sys/solid/io.rs b/library/std/src/sys/solid/io.rs index 9eb17a10daa28..1107c6d4bde06 100644 --- a/library/std/src/sys/solid/io.rs +++ b/library/std/src/sys/solid/io.rs @@ -33,7 +33,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + pub fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) } } } @@ -66,12 +66,17 @@ impl<'a> IoSliceMut<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + pub fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) } } #[inline] - pub fn as_mut_slice(&mut self) -> &mut [u8] { + pub fn as_mut_slice(&mut self) -> &'a mut [u8] { + unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) } + } + + #[inline] + pub fn into_slice(self) -> &'a mut [u8] { unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) } } } diff --git a/library/std/src/sys/unix/io.rs b/library/std/src/sys/unix/io.rs index deb5ee76bd035..83af3c3e44938 100644 --- a/library/std/src/sys/unix/io.rs +++ b/library/std/src/sys/unix/io.rs @@ -32,7 +32,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + pub fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) } } } @@ -73,4 +73,9 @@ impl<'a> IoSliceMut<'a> { pub fn as_mut_slice(&mut self) -> &mut [u8] { unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) } } + + #[inline] + pub fn into_slice(self) -> &'a mut [u8] { + unsafe { slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len) } + } } diff --git a/library/std/src/sys/unsupported/io.rs b/library/std/src/sys/unsupported/io.rs index d5f475b4310fd..bfc4f4fead620 100644 --- a/library/std/src/sys/unsupported/io.rs +++ b/library/std/src/sys/unsupported/io.rs @@ -15,7 +15,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + pub fn as_slice(&self) -> &'a [u8] { self.0 } } @@ -44,4 +44,9 @@ impl<'a> IoSliceMut<'a> { pub fn as_mut_slice(&mut self) -> &mut [u8] { self.0 } + + #[inline] + pub fn into_slice(self) -> &'a mut [u8] { + self.0 + } } diff --git a/library/std/src/sys/wasi/io.rs b/library/std/src/sys/wasi/io.rs index ee017d13a4ca0..ddbb75635abe6 100644 --- a/library/std/src/sys/wasi/io.rs +++ b/library/std/src/sys/wasi/io.rs @@ -29,7 +29,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + pub fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.buf as *const u8, self.vec.buf_len) } } } @@ -70,4 +70,9 @@ impl<'a> IoSliceMut<'a> { pub fn as_mut_slice(&mut self) -> &mut [u8] { unsafe { slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.buf_len) } } + + #[inline] + pub fn into_slice(self) -> &'a mut [u8] { + unsafe { slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.buf_len) } + } } diff --git a/library/std/src/sys/windows/io.rs b/library/std/src/sys/windows/io.rs index fb06df1f80cda..e79de64b220b8 100644 --- a/library/std/src/sys/windows/io.rs +++ b/library/std/src/sys/windows/io.rs @@ -35,7 +35,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + pub fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.buf as *mut u8, self.vec.len as usize) } } } @@ -77,4 +77,9 @@ impl<'a> IoSliceMut<'a> { pub fn as_mut_slice(&mut self) -> &mut [u8] { unsafe { slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.len as usize) } } + + #[inline] + pub fn into_slice(self) -> &'a mut [u8] { + unsafe { slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.len as usize) } + } }