Skip to content

Add IoSlice::as_bytes #111277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,31 @@ impl<'a> IoSlice<'a> {
bufs[0].advance(left);
}
}

/// Get the underlying bytes as a rust slice with the original lifetime
///
/// # Example
///
/// ```
/// #![feature(io_slice_as_bytes)]
/// use std::io::IoSlice;
///
/// let data = b"abcdef";
///
/// let mut io_slice = IoSlice::new(data);
/// 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.into_bytes(), b"def");
/// ```
#[unstable(feature = "io_slice_as_bytes", issue = "111277")]
Copy link
Contributor

@tgross35 tgross35 Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue number points to this PR; you should open a tracking issue and link it instead.

Also feature gate io_slice_as_bytes -> io_slice_into_bytes if that is the name.

Edit: io_slice_as_slice with the below.

#[inline]
#[must_use]
pub const fn into_bytes(&self) -> &'a [u8] {
self.0.as_slice()
}
}

#[stable(feature = "iovec", since = "1.36.0")]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/solid/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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.iov_base as *mut u8, self.vec.iov_len) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/unix/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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.iov_base as *mut u8, self.vec.iov_len) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/unsupported/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<'a> IoSlice<'a> {
}

#[inline]
pub fn as_slice(&self) -> &[u8] {
pub const fn as_slice(&self) -> &'a [u8] {
self.0
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/wasi/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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 as *const u8, self.vec.buf_len) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/windows/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
Expand Down