From f4d6d8717c8ab6eebd319ddf40bc3d193fb83476 Mon Sep 17 00:00:00 2001 From: Nathan West Date: Sat, 6 May 2023 00:47:51 -0400 Subject: [PATCH 1/8] Add IoSlice::as_bytes --- library/std/src/io/mod.rs | 24 +++++++++++++++++++++++ library/std/src/sys/pal/solid/io.rs | 2 +- library/std/src/sys/pal/unix/io.rs | 2 +- library/std/src/sys/pal/unsupported/io.rs | 2 +- library/std/src/sys/pal/wasi/io.rs | 2 +- library/std/src/sys/windows/io.rs | 0 6 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 library/std/src/sys/windows/io.rs diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index a238e74ed95cf..792934e25d55f 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1491,6 +1491,30 @@ impl<'a> IoSlice<'a> { bufs[0].advance(left); } } + + /// Get the underlying bytes as a rust slice with the original lifetime + /// + /// # Example + /// + /// ``` + /// use std::io::IoSlice; + /// + /// let data = b"abcdef"; + /// + /// let mut io_slice = IoSlice::new(data); + /// let tail = io_slice.as_bytes()[3..]; + /// + /// // This works because `tail` doesn't borrow `io_slice` + /// io_slice = IoSlice::new(tail); + /// + /// assert_eq!(io_slice.as_bytes(), b"def"); + /// ``` + #[unstable(feature = "io_slice_as_bytes")] + #[inline] + #[must_use] + pub const fn as_bytes(&self) -> &'a [u8] { + self.0.as_slice() + } } #[stable(feature = "iovec", since = "1.36.0")] diff --git a/library/std/src/sys/pal/solid/io.rs b/library/std/src/sys/pal/solid/io.rs index a862bb7870264..2ec1578dc43a9 100644 --- a/library/std/src/sys/pal/solid/io.rs +++ b/library/std/src/sys/pal/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) } } } diff --git a/library/std/src/sys/pal/unix/io.rs b/library/std/src/sys/pal/unix/io.rs index 29c340dd34942..aa6a9fe0dd621 100644 --- a/library/std/src/sys/pal/unix/io.rs +++ b/library/std/src/sys/pal/unix/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) } } } diff --git a/library/std/src/sys/pal/unsupported/io.rs b/library/std/src/sys/pal/unsupported/io.rs index 6372fca74e0d7..7afcee3cb5dc9 100644 --- a/library/std/src/sys/pal/unsupported/io.rs +++ b/library/std/src/sys/pal/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 } } diff --git a/library/std/src/sys/pal/wasi/io.rs b/library/std/src/sys/pal/wasi/io.rs index 2cd45df88fad1..c12e30e2ec748 100644 --- a/library/std/src/sys/pal/wasi/io.rs +++ b/library/std/src/sys/pal/wasi/io.rs @@ -30,7 +30,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) } } } diff --git a/library/std/src/sys/windows/io.rs b/library/std/src/sys/windows/io.rs new file mode 100644 index 0000000000000..e69de29bb2d1d From a36bed91e7ca0dcfbd8e05819853d40fe0a492db Mon Sep 17 00:00:00 2001 From: Nathan West Date: Sat, 6 May 2023 00:53:22 -0400 Subject: [PATCH 2/8] Add const to the `sys` getters; add issue number to #[unstable] --- library/std/src/io/mod.rs | 2 +- library/std/src/sys/pal/solid/io.rs | 2 +- library/std/src/sys/pal/unix/io.rs | 2 +- library/std/src/sys/pal/unsupported/io.rs | 2 +- library/std/src/sys/pal/wasi/io.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 792934e25d55f..454404db235d0 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1509,7 +1509,7 @@ impl<'a> IoSlice<'a> { /// /// assert_eq!(io_slice.as_bytes(), b"def"); /// ``` - #[unstable(feature = "io_slice_as_bytes")] + #[unstable(feature = "io_slice_as_bytes", issue = "111277")] #[inline] #[must_use] pub const fn as_bytes(&self) -> &'a [u8] { diff --git a/library/std/src/sys/pal/solid/io.rs b/library/std/src/sys/pal/solid/io.rs index 2ec1578dc43a9..75453a640f9ef 100644 --- a/library/std/src/sys/pal/solid/io.rs +++ b/library/std/src/sys/pal/solid/io.rs @@ -33,7 +33,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &'a [u8] { + pub const fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) } } } diff --git a/library/std/src/sys/pal/unix/io.rs b/library/std/src/sys/pal/unix/io.rs index aa6a9fe0dd621..ce3f00b118df3 100644 --- a/library/std/src/sys/pal/unix/io.rs +++ b/library/std/src/sys/pal/unix/io.rs @@ -33,7 +33,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &'a [u8] { + pub const fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) } } } diff --git a/library/std/src/sys/pal/unsupported/io.rs b/library/std/src/sys/pal/unsupported/io.rs index 7afcee3cb5dc9..4c86acc496382 100644 --- a/library/std/src/sys/pal/unsupported/io.rs +++ b/library/std/src/sys/pal/unsupported/io.rs @@ -15,7 +15,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &'a [u8] { + pub const fn as_slice(&self) -> &'a [u8] { self.0 } } diff --git a/library/std/src/sys/pal/wasi/io.rs b/library/std/src/sys/pal/wasi/io.rs index c12e30e2ec748..be9f8a20b892d 100644 --- a/library/std/src/sys/pal/wasi/io.rs +++ b/library/std/src/sys/pal/wasi/io.rs @@ -30,7 +30,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &'a [u8] { + pub const fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.buf as *const u8, self.vec.buf_len) } } } From 593289f8972aa75e96abc5170150918ca3f5fa17 Mon Sep 17 00:00:00 2001 From: Nathan West Date: Sat, 6 May 2023 13:28:14 -0400 Subject: [PATCH 3/8] Rename `IoSlice::as_bytes` to `IoSlice::into_bytes` --- library/std/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 454404db235d0..b99074749aba7 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1512,7 +1512,7 @@ impl<'a> IoSlice<'a> { #[unstable(feature = "io_slice_as_bytes", issue = "111277")] #[inline] #[must_use] - pub const fn as_bytes(&self) -> &'a [u8] { + pub const fn into_bytes(&self) -> &'a [u8] { self.0.as_slice() } } From b479bf20be96301b8f007c20a53ab43a3a574069 Mon Sep 17 00:00:00 2001 From: Nathan West Date: Mon, 8 May 2023 17:37:36 -0400 Subject: [PATCH 4/8] Fix broken test, probably --- 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 b99074749aba7..a0c609af4fcdb 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1502,12 +1502,12 @@ impl<'a> IoSlice<'a> { /// let data = b"abcdef"; /// /// let mut io_slice = IoSlice::new(data); - /// let tail = io_slice.as_bytes()[3..]; + /// let tail = io_slice.into_bytes()[3..]; /// /// // This works because `tail` doesn't borrow `io_slice` /// io_slice = IoSlice::new(tail); /// - /// assert_eq!(io_slice.as_bytes(), b"def"); + /// assert_eq!(io_slice.into_bytes(), b"def"); /// ``` #[unstable(feature = "io_slice_as_bytes", issue = "111277")] #[inline] From f5b65e040446c59affb403d82b9fc3744f7feefa Mon Sep 17 00:00:00 2001 From: Nathan West Date: Wed, 14 Feb 2024 13:03:28 -0500 Subject: [PATCH 5/8] Restore windows `as_slice` method --- library/std/src/sys/pal/windows/io.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/pal/windows/io.rs b/library/std/src/sys/pal/windows/io.rs index b73d9f3ff4c4a..3ba8556b85d1a 100644 --- a/library/std/src/sys/pal/windows/io.rs +++ b/library/std/src/sys/pal/windows/io.rs @@ -35,7 +35,7 @@ impl<'a> IoSlice<'a> { } #[inline] - pub fn as_slice(&self) -> &[u8] { + pub const fn as_slice(&self) -> &'a [u8] { unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) } } } From 6d3b2a1db483be235b0d7e838be56db1fda9c9cc Mon Sep 17 00:00:00 2001 From: Nathan West Date: Wed, 14 Feb 2024 13:04:27 -0500 Subject: [PATCH 6/8] Remove defunct windows file --- library/std/src/sys/windows/io.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 library/std/src/sys/windows/io.rs diff --git a/library/std/src/sys/windows/io.rs b/library/std/src/sys/windows/io.rs deleted file mode 100644 index e69de29bb2d1d..0000000000000 From 73eb0858e9641a75df422dbd82cea427f853592f Mon Sep 17 00:00:00 2001 From: Nathan West Date: Wed, 14 Feb 2024 14:31:08 -0500 Subject: [PATCH 7/8] Add missing borrow --- library/std/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index a0c609af4fcdb..900b65e71d5d5 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1502,7 +1502,7 @@ impl<'a> IoSlice<'a> { /// let data = b"abcdef"; /// /// let mut io_slice = IoSlice::new(data); - /// let tail = io_slice.into_bytes()[3..]; + /// let tail = &io_slice.into_bytes()[3..]; /// /// // This works because `tail` doesn't borrow `io_slice` /// io_slice = IoSlice::new(tail); From 2c85c3674243c053753b74bcbbbb7df37a16fd30 Mon Sep 17 00:00:00 2001 From: Nathan West Date: Thu, 15 Feb 2024 16:15:24 -0500 Subject: [PATCH 8/8] Add feature attribute to doctest --- library/std/src/io/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 900b65e71d5d5..3320a9f8d3cfd 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1497,6 +1497,7 @@ impl<'a> IoSlice<'a> { /// # Example /// /// ``` + /// #![feature(io_slice_as_bytes)] /// use std::io::IoSlice; /// /// let data = b"abcdef";