Skip to content

Make IoSlice and IoSliceMut methods unstably const #144090

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,8 +1338,9 @@ impl<'a> IoSliceMut<'a> {
///
/// Panics on Windows if the slice is larger than 4GB.
#[stable(feature = "iovec", since = "1.36.0")]
#[rustc_const_unstable(feature = "io_slice_const", issue = "none")]
#[inline]
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
pub const fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
IoSliceMut(sys::io::IoSliceMut::new(buf))
}

Expand All @@ -1366,8 +1367,9 @@ impl<'a> IoSliceMut<'a> {
/// assert_eq!(buf.deref(), [1; 5].as_ref());
/// ```
#[stable(feature = "io_slice_advance", since = "1.81.0")]
#[rustc_const_unstable(feature = "io_slice_const", issue = "none")]
#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
self.0.advance(n)
}

Expand Down Expand Up @@ -1496,9 +1498,10 @@ impl<'a> IoSlice<'a> {
///
/// Panics on Windows if the slice is larger than 4GB.
#[stable(feature = "iovec", since = "1.36.0")]
#[rustc_const_unstable(feature = "io_slice_const", issue = "none")]
#[must_use]
#[inline]
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
pub const fn new(buf: &'a [u8]) -> IoSlice<'a> {
IoSlice(sys::io::IoSlice::new(buf))
}

Expand All @@ -1525,8 +1528,9 @@ impl<'a> IoSlice<'a> {
/// assert_eq!(buf.deref(), [1; 5].as_ref());
/// ```
#[stable(feature = "io_slice_advance", since = "1.81.0")]
#[rustc_const_unstable(feature = "io_slice_const", issue = "none")]
#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
self.0.advance(n)
}

Expand Down
12 changes: 6 additions & 6 deletions library/std/src/sys/io/io_slice/iovec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ pub struct IoSlice<'a> {

impl<'a> IoSlice<'a> {
#[inline]
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
pub const fn new(buf: &'a [u8]) -> IoSlice<'a> {
IoSlice {
vec: iovec { iov_base: buf.as_ptr() as *mut u8 as *mut c_void, iov_len: buf.len() },
_p: PhantomData,
}
}

#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
if self.vec.iov_len < n {
panic!("advancing IoSlice beyond its length");
}
Expand All @@ -51,15 +51,15 @@ pub struct IoSliceMut<'a> {

impl<'a> IoSliceMut<'a> {
#[inline]
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
pub const fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
IoSliceMut {
vec: iovec { iov_base: buf.as_mut_ptr() as *mut c_void, iov_len: buf.len() },
_p: PhantomData,
}
}

#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
if self.vec.iov_len < n {
panic!("advancing IoSliceMut beyond its length");
}
Expand All @@ -71,7 +71,7 @@ impl<'a> IoSliceMut<'a> {
}

#[inline]
pub fn as_slice(&self) -> &[u8] {
pub const fn as_slice(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len) }
}

Expand All @@ -81,7 +81,7 @@ impl<'a> IoSliceMut<'a> {
}

#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
pub const 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) }
}
}
12 changes: 6 additions & 6 deletions library/std/src/sys/io/io_slice/unsupported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ pub struct IoSlice<'a>(&'a [u8]);

impl<'a> IoSlice<'a> {
#[inline]
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
pub const fn new(buf: &'a [u8]) -> IoSlice<'a> {
IoSlice(buf)
}

#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
self.0 = &self.0[n..]
}

Expand All @@ -24,19 +24,19 @@ pub struct IoSliceMut<'a>(&'a mut [u8]);

impl<'a> IoSliceMut<'a> {
#[inline]
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
pub const fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
IoSliceMut(buf)
}

#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
let slice = mem::take(&mut self.0);
let (_, remaining) = slice.split_at_mut(n);
self.0 = remaining;
}

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

Expand All @@ -46,7 +46,7 @@ impl<'a> IoSliceMut<'a> {
}

#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
pub const fn as_mut_slice(&mut self) -> &mut [u8] {
self.0
}
}
12 changes: 6 additions & 6 deletions library/std/src/sys/io/io_slice/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ pub struct IoSlice<'a> {

impl<'a> IoSlice<'a> {
#[inline]
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
pub const fn new(buf: &'a [u8]) -> IoSlice<'a> {
IoSlice { vec: wasi::Ciovec { buf: buf.as_ptr(), buf_len: buf.len() }, _p: PhantomData }
}

#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
if self.vec.buf_len < n {
panic!("advancing IoSlice beyond its length");
}
Expand All @@ -40,15 +40,15 @@ pub struct IoSliceMut<'a> {

impl<'a> IoSliceMut<'a> {
#[inline]
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
pub const fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
IoSliceMut {
vec: wasi::Iovec { buf: buf.as_mut_ptr(), buf_len: buf.len() },
_p: PhantomData,
}
}

#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
if self.vec.buf_len < n {
panic!("advancing IoSlice beyond its length");
}
Expand All @@ -60,7 +60,7 @@ impl<'a> IoSliceMut<'a> {
}

#[inline]
pub fn as_slice(&self) -> &[u8] {
pub const fn as_slice(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.vec.buf as *const u8, self.vec.buf_len) }
}

Expand All @@ -70,7 +70,7 @@ impl<'a> IoSliceMut<'a> {
}

#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
pub const fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.buf_len) }
}
}
12 changes: 6 additions & 6 deletions library/std/src/sys/io/io_slice/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct IoSlice<'a> {

impl<'a> IoSlice<'a> {
#[inline]
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
pub const fn new(buf: &'a [u8]) -> IoSlice<'a> {
assert!(buf.len() <= u32::MAX as usize);
IoSlice {
vec: c::WSABUF { len: buf.len() as u32, buf: buf.as_ptr() as *mut u8 },
Expand All @@ -20,7 +20,7 @@ impl<'a> IoSlice<'a> {
}

#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
if (self.vec.len as usize) < n {
panic!("advancing IoSlice beyond its length");
}
Expand All @@ -45,7 +45,7 @@ pub struct IoSliceMut<'a> {

impl<'a> IoSliceMut<'a> {
#[inline]
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
pub const fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
assert!(buf.len() <= u32::MAX as usize);
IoSliceMut {
vec: c::WSABUF { len: buf.len() as u32, buf: buf.as_mut_ptr() },
Expand All @@ -54,7 +54,7 @@ impl<'a> IoSliceMut<'a> {
}

#[inline]
pub fn advance(&mut self, n: usize) {
pub const fn advance(&mut self, n: usize) {
if (self.vec.len as usize) < n {
panic!("advancing IoSliceMut beyond its length");
}
Expand All @@ -66,7 +66,7 @@ impl<'a> IoSliceMut<'a> {
}

#[inline]
pub fn as_slice(&self) -> &[u8] {
pub const fn as_slice(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.vec.buf, self.vec.len as usize) }
}

Expand All @@ -76,7 +76,7 @@ impl<'a> IoSliceMut<'a> {
}

#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
pub const fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.vec.buf, self.vec.len as usize) }
}
}
Loading