Use inline const expressions

Inline const expressions were stablised in Rust 1.79

Part of #12339
This commit is contained in:
xtqqczze
2026-01-17 00:57:48 +00:00
committed by Johannes Altmanninger
parent c99c84558f
commit 66ea0f78be
6 changed files with 28 additions and 20 deletions

View File

@@ -261,7 +261,10 @@ pub const fn help_section_exists(section: &str) -> bool {
#[macro_export]
macro_rules! help_section {
($section:expr) => {{
const _: () = assert!($crate::help_section_exists($section));
const {
assert!($crate::help_section_exists($section));
}
$section
}};
}

View File

@@ -36,7 +36,10 @@ pub fn wcwidth(c: char) -> isize {
pub unsafe fn wcwidth(c: libc::wchar_t) -> libc::c_int;
}
const _: () = assert!(std::mem::size_of::<libc::wchar_t>() >= std::mem::size_of::<char>());
const {
assert!(size_of::<libc::wchar_t>() >= size_of::<char>());
}
let width = unsafe { wcwidth(c as libc::wchar_t) };
isize::try_from(width).unwrap()
}

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap;
const U32_SIZE: usize = std::mem::size_of::<u32>();
const U32_SIZE: usize = size_of::<u32>();
fn read_le_u32(bytes: &[u8]) -> u32 {
u32::from_le_bytes(bytes[..U32_SIZE].try_into().unwrap())
@@ -47,9 +47,12 @@ fn check_if_revision_is_supported(revision: u32) -> std::io::Result<()> {
}
fn as_usize(value: u32) -> usize {
use std::mem::size_of;
const _: () = assert!(size_of::<u32>() <= size_of::<usize>());
usize::try_from(value).unwrap()
const {
assert!(size_of::<u32>() <= size_of::<usize>());
}
// SAFETY: `usize` is guaranteed to be at least as wide as `u32` by the const assert above.
unsafe { usize::try_from(value).unwrap_unchecked() }
}
fn parse_strings(

View File

@@ -87,10 +87,9 @@ macro_rules! define_on {
fn convert_resource(resource: c_uint) -> ResourceEnum {
let resource: i32 = resource.try_into().unwrap();
use std::mem::{size_of, transmute};
// Resource is #[repr(i32)] so this is ok
const _: () = assert!(size_of::<c_uint>() == size_of::<ResourceEnum>());
unsafe { transmute(resource) }
// SAFETY: Resource is #[repr(i32)] so this is sound
unsafe { std::mem::transmute(resource) }
}
/// Calls getrlimit.

View File

@@ -148,12 +148,13 @@ pub fn from_exit_code(ret: i32) -> ProcStatus {
or invalid builtin exit code!"
);
// Some paranoia.
const _zerocode: i32 = ProcStatus::w_exitcode(0, 0);
const _: () = assert!(
WIFEXITED(_zerocode),
"Synthetic exit status not reported as exited"
);
const {
let _zerocode = ProcStatus::w_exitcode(0, 0);
assert!(
WIFEXITED(_zerocode),
"Synthetic exit status not reported as exited"
);
}
assert!(ret < 256);
ProcStatus::new(Some(Self::w_exitcode(ret, 0 /* sig */)))

View File

@@ -269,10 +269,9 @@ pub fn next(&mut self) -> Option<io::Result<&DirEntry>> {
};
// dent.d_name is c_char; pretend it's u8.
assert_eq!(
std::mem::size_of::<libc::c_char>(),
std::mem::size_of::<u8>()
);
const {
assert!(size_of::<libc::c_char>() == size_of::<u8>());
}
// Do not rely on `libc::dirent::d_name.len()` as dirent names may exceed
// the nominal buffer size; instead use the terminating nul byte.