From 768e28cb4ea10b745bec25d4a5f6b45bdd1d9beb Mon Sep 17 00:00:00 2001 From: auronandace Date: Fri, 20 Feb 2026 14:10:34 +0000 Subject: [PATCH] stdio and c_vec cleanup --- src/c_str.rs | 2 +- src/c_vec.rs | 10 +++++----- src/header/stdio/mod.rs | 2 +- src/header/stdio/printf.rs | 14 +++++++------- src/header/sys_mman/mod.rs | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/c_str.rs b/src/c_str.rs index 99fb1fab4f..37880314a7 100644 --- a/src/c_str.rs +++ b/src/c_str.rs @@ -117,7 +117,7 @@ impl<'a, T: Kind> NulStr<'a, T> { /// The ptr must be valid up to and including the first NUL byte from the base ptr. pub const unsafe fn from_ptr(ptr: *const T::C) -> Self { Self { - ptr: unsafe { NonNull::new_unchecked(ptr as *mut T::C) }, + ptr: unsafe { NonNull::new_unchecked(ptr.cast_mut()) }, _marker: PhantomData, } } diff --git a/src/c_vec.rs b/src/c_vec.rs index 6d67b58662..42b5fe8e25 100644 --- a/src/c_vec.rs +++ b/src/c_vec.rs @@ -37,7 +37,7 @@ impl CVec { } } fn check_bounds(i: usize) -> Result { - if i > core::isize::MAX as usize { + if i > isize::MAX as usize { Err(AllocError) } else { Ok(i) @@ -53,7 +53,7 @@ impl CVec { return Ok(Self::new()); } let size = Self::check_mul(cap, mem::size_of::())?; - let ptr = NonNull::new(unsafe { platform::alloc(size) as *mut T }).ok_or(AllocError)?; + let ptr = NonNull::new(unsafe { platform::alloc(size).cast::() }).ok_or(AllocError)?; Ok(Self { ptr, len: 0, cap }) } unsafe fn resize(&mut self, cap: usize) -> Result<(), AllocError> { @@ -62,11 +62,11 @@ impl CVec { NonNull::dangling() } else if self.cap > 0 { NonNull::new( - unsafe { platform::realloc(self.ptr.as_ptr() as *mut c_void, size) } as *mut T, + unsafe { platform::realloc(self.ptr.as_ptr().cast::(), size) }.cast::(), ) .ok_or(AllocError)? } else { - NonNull::new((unsafe { platform::alloc(size) }) as *mut T).ok_or(AllocError)? + NonNull::new((unsafe { platform::alloc(size) }).cast::()).ok_or(AllocError)? }; self.ptr = ptr; self.cap = cap; @@ -90,7 +90,7 @@ impl CVec { .ok_or(AllocError) .and_then(Self::check_bounds)?; if required_len > self.cap { - let new_cap = cmp::min(required_len.next_power_of_two(), core::isize::MAX as usize); + let new_cap = cmp::min(required_len.next_power_of_two(), isize::MAX as usize); unsafe { self.resize(new_cap)?; } diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index fa9bb72aa0..b526a4c684 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -1124,7 +1124,7 @@ pub unsafe extern "C" fn puts(s: *const c_char) -> c_int { if stream.write_all(&buf).is_err() { return -1; } - if stream.write(&[b'\n']).is_err() { + if stream.write(b"\n").is_err() { return -1; } 0 diff --git a/src/header/stdio/printf.rs b/src/header/stdio/printf.rs index 22d8cb8f8e..3d33ae7813 100644 --- a/src/header/stdio/printf.rs +++ b/src/header/stdio/printf.rs @@ -412,7 +412,7 @@ fn fmt_float_exp( pad(w, !left, b' ', len..pad_space)?; let bytes = if string.starts_with('-') { - w.write_all(&[b'-'])?; + w.write_all(b"-")?; &string.as_bytes()[1..] } else { string.as_bytes() @@ -438,7 +438,7 @@ fn fmt_float_normal( pad(w, !left, b' ', string.len()..pad_space)?; let bytes = if string.starts_with('-') { - w.write_all(&[b'-'])?; + w.write_all(b"-")?; &string.as_bytes()[1..] } else { string.as_bytes() @@ -477,7 +477,7 @@ fn fmt_float_nonfinite( // Infinity is always padded with spaces, rather than zeroes pad(w, !left, b' ', string.len()..pad_space + pad_zero)?; if float.is_sign_negative() { - w.write_all(&[b'-'])?; + w.write_all(b"-")?; } w.write_all(string.as_bytes())?; pad(w, left, b' ', string.len()..pad_space + pad_zero)?; @@ -748,7 +748,7 @@ pub(crate) unsafe fn inner_printf( }); match fmtkind { - FmtKind::Percent => w.write_all(&[b'%'])?, + FmtKind::Percent => w.write_all(b"%")?, FmtKind::Signed => { let string = match unsafe { varargs.get(index, &mut ap, Some((arg.fmtkind, arg.intkind))) @@ -782,13 +782,13 @@ pub(crate) unsafe fn inner_printf( let bytes = if positive { if sign_reserve { - w.write_all(&[b' '])?; + w.write_all(b" ")?; } else if sign_always { - w.write_all(&[b'+'])?; + w.write_all(b"+")?; } string.as_bytes() } else { - w.write_all(&[b'-'])?; + w.write_all(b"-")?; &string.as_bytes()[1..] }; pad(w, true, b'0', len..precision.unwrap_or(pad_zero))?; diff --git a/src/header/sys_mman/mod.rs b/src/header/sys_mman/mod.rs index 5415be294a..5287889fea 100644 --- a/src/header/sys_mman/mod.rs +++ b/src/header/sys_mman/mod.rs @@ -154,10 +154,10 @@ pub unsafe extern "C" fn munmap(addr: *mut c_void, len: size_t) -> c_int { } #[cfg(target_os = "linux")] -static SHM_PATH: &'static [u8] = b"/dev/shm/"; +static SHM_PATH: &[u8] = b"/dev/shm/"; #[cfg(target_os = "redox")] -static SHM_PATH: &'static [u8] = b"/scheme/shm/"; +static SHM_PATH: &[u8] = b"/scheme/shm/"; unsafe fn shm_path(name: *const c_char) -> CString { let name_c = unsafe { CStr::from_ptr(name) };