Skip to content

Commit

Permalink
Add as_slice/into_slice for IoSlice/IoSliceMut
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdn committed Aug 29, 2022
1 parent 332cc8f commit a623c52
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 7 deletions.
14 changes: 14 additions & 0 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down
14 changes: 14 additions & 0 deletions library/std/src/io/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions library/std/src/sys/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 fn as_slice(&self) -> &'a [u8] {
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) }
}
}
Expand Down Expand Up @@ -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) }
}
}
7 changes: 6 additions & 1 deletion library/std/src/sys/unix/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
Expand Down Expand Up @@ -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) }
}
}
7 changes: 6 additions & 1 deletion library/std/src/sys/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 fn as_slice(&self) -> &'a [u8] {
self.0
}
}
Expand Down Expand Up @@ -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
}
}
7 changes: 6 additions & 1 deletion library/std/src/sys/wasi/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
Expand Down Expand Up @@ -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) }
}
}
7 changes: 6 additions & 1 deletion library/std/src/sys/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 fn as_slice(&self) -> &'a [u8] {
unsafe { slice::from_raw_parts(self.vec.buf as *mut u8, self.vec.len as usize) }
}
}
Expand Down Expand Up @@ -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) }
}
}

0 comments on commit a623c52

Please sign in to comment.