2024-09-02 17:09:52 +00:00
|
|
|
//! Casting.
|
|
|
|
//!
|
|
|
|
//! This modules provides utilities for casting between types.
|
|
|
|
//!
|
|
|
|
//! `#[no_std]` compatible.
|
|
|
|
|
2024-09-02 21:46:11 +00:00
|
|
|
#![allow(clippy::cast_possible_truncation)]
|
|
|
|
|
2024-09-02 17:09:52 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
//============================ SAFETY: DO NOT REMOVE ===========================//
|
|
|
|
// //
|
|
|
|
// //
|
2024-09-02 21:46:11 +00:00
|
|
|
// Only allow building 64-bit targets. //
|
|
|
|
// This allows us to assume 64-bit invariants in this file. //
|
2024-09-02 17:09:52 +00:00
|
|
|
#[cfg(not(target_pointer_width = "64"))]
|
2024-09-02 21:46:11 +00:00
|
|
|
compile_error!("Cuprate is only compatible with 64-bit CPUs");
|
2024-09-02 17:09:52 +00:00
|
|
|
// //
|
|
|
|
// //
|
|
|
|
//============================ SAFETY: DO NOT REMOVE ===========================//
|
|
|
|
|
|
|
|
/// Cast [`u64`] to [`usize`].
|
|
|
|
#[inline(always)]
|
|
|
|
pub const fn u64_to_usize(u: u64) -> usize {
|
|
|
|
u as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cast [`u32`] to [`usize`].
|
|
|
|
#[inline(always)]
|
|
|
|
pub const fn u32_to_usize(u: u32) -> usize {
|
|
|
|
u as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cast [`usize`] to [`u64`].
|
|
|
|
#[inline(always)]
|
|
|
|
pub const fn usize_to_u64(u: usize) -> u64 {
|
|
|
|
u as u64
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cast [`i64`] to [`isize`].
|
|
|
|
#[inline(always)]
|
|
|
|
pub const fn i64_to_isize(i: i64) -> isize {
|
|
|
|
i as isize
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cast [`i32`] to [`isize`].
|
|
|
|
#[inline(always)]
|
|
|
|
pub const fn i32_to_isize(i: i32) -> isize {
|
|
|
|
i as isize
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cast [`isize`] to [`i64`].
|
|
|
|
#[inline(always)]
|
|
|
|
pub const fn isize_to_i64(i: isize) -> i64 {
|
|
|
|
i as i64
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------- Tests
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn max_unsigned() {
|
2024-09-02 21:46:11 +00:00
|
|
|
assert_eq!(u32_to_usize(u32::MAX), usize::try_from(u32::MAX).unwrap());
|
|
|
|
assert_eq!(usize_to_u64(u32_to_usize(u32::MAX)), u64::from(u32::MAX));
|
2024-09-02 17:09:52 +00:00
|
|
|
|
|
|
|
assert_eq!(u64_to_usize(u64::MAX), usize::MAX);
|
|
|
|
assert_eq!(usize_to_u64(u64_to_usize(u64::MAX)), u64::MAX);
|
|
|
|
|
|
|
|
assert_eq!(usize_to_u64(usize::MAX), u64::MAX);
|
|
|
|
assert_eq!(u64_to_usize(usize_to_u64(usize::MAX)), usize::MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn max_signed() {
|
2024-09-02 21:46:11 +00:00
|
|
|
assert_eq!(i32_to_isize(i32::MAX), isize::try_from(i32::MAX).unwrap());
|
|
|
|
assert_eq!(isize_to_i64(i32_to_isize(i32::MAX)), i64::from(i32::MAX));
|
2024-09-02 17:09:52 +00:00
|
|
|
|
|
|
|
assert_eq!(i64_to_isize(i64::MAX), isize::MAX);
|
|
|
|
assert_eq!(isize_to_i64(i64_to_isize(i64::MAX)), i64::MAX);
|
|
|
|
|
|
|
|
assert_eq!(isize_to_i64(isize::MAX), i64::MAX);
|
|
|
|
assert_eq!(i64_to_isize(isize_to_i64(isize::MAX)), isize::MAX);
|
|
|
|
}
|
|
|
|
}
|