Skip to content

Commit a3a7d5c

Browse files
committed
Add std::io::IoSlice::as_slice method
This allows getting a slice with the same lifetime as the wrapped bytes.
1 parent 0d7b2fb commit a3a7d5c

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

library/std/src/io/mod.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,34 @@ impl<'a> IoSlice<'a> {
13851385
IoSlice(sys::io::IoSlice::new(buf))
13861386
}
13871387

1388+
/// Returns a copy of the wrapped slice.
1389+
///
1390+
/// The lifetime of the slice is not bound to the `IoSlice` itself, instead it references the
1391+
/// wrapped slice itself. This is unlike the slice which can be obtained from the [`Deref`]
1392+
/// implementation, its lifetime is tied to the specific `IoSlice`. This makes `as_slice`
1393+
/// suitable for changing the length of the slice in-place.
1394+
///
1395+
/// # Examples
1396+
///
1397+
/// ```
1398+
/// #![feature(io_slice_as_slice)]
1399+
///
1400+
/// use std::io::IoSlice;
1401+
///
1402+
/// let data = [0, 1, 2, 3, 4];
1403+
/// let mut buf = IoSlice::new(&data);
1404+
///
1405+
/// // Discard the first and last bytes
1406+
/// buf = IoSlice::new(buf.as_slice()[1..4]);
1407+
/// assert_eq!(buf.as_slice(), [1, 2, 3].as_ref());
1408+
/// ```
1409+
#[unstable(feature = "io_slice_as_slice", issue = "124659")]
1410+
#[must_use]
1411+
#[inline]
1412+
pub fn as_slice(&self) -> &'a [u8] {
1413+
self.0.as_slice()
1414+
}
1415+
13881416
/// Advance the internal cursor of the slice.
13891417
///
13901418
/// Also see [`IoSlice::advance_slices`] to advance the cursors of multiple

library/std/src/sys/pal/solid/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'a> IoSlice<'a> {
3333
}
3434

3535
#[inline]
36-
pub fn as_slice(&self) -> &[u8] {
36+
pub fn as_slice(&self) -> &'a [u8] {
3737
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) }
3838
}
3939
}

library/std/src/sys/pal/unix/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'a> IoSlice<'a> {
3333
}
3434

3535
#[inline]
36-
pub fn as_slice(&self) -> &[u8] {
36+
pub fn as_slice(&self) -> &'a [u8] {
3737
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) }
3838
}
3939
}

library/std/src/sys/pal/unsupported/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<'a> IoSlice<'a> {
1515
}
1616

1717
#[inline]
18-
pub fn as_slice(&self) -> &[u8] {
18+
pub fn as_slice(&self) -> &'a [u8] {
1919
self.0
2020
}
2121
}

library/std/src/sys/pal/wasi/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'a> IoSlice<'a> {
3030
}
3131

3232
#[inline]
33-
pub fn as_slice(&self) -> &[u8] {
33+
pub fn as_slice(&self) -> &'a [u8] {
3434
unsafe { slice::from_raw_parts(self.vec.buf as *const u8, self.vec.buf_len) }
3535
}
3636
}

library/std/src/sys/pal/windows/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'a> IoSlice<'a> {
3535
}
3636

3737
#[inline]
38-
pub fn as_slice(&self) -> &[u8] {
38+
pub fn as_slice(&self) -> &'a [u8] {
3939
unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) }
4040
}
4141
}

0 commit comments

Comments
 (0)