mirror of
https://github.com/hinto-janai/cuprate.git
synced 2025-01-07 03:19:43 +00:00
85 lines
2.7 KiB
Rust
85 lines
2.7 KiB
Rust
|
//! Casting.
|
||
|
//!
|
||
|
//! This modules provides utilities for casting between types.
|
||
|
//!
|
||
|
//! `#[no_std]` compatible.
|
||
|
|
||
|
#[rustfmt::skip]
|
||
|
//============================ SAFETY: DO NOT REMOVE ===========================//
|
||
|
// //
|
||
|
// //
|
||
|
// Only allow building 64-bit targets. //
|
||
|
// This allows us to assume 64-bit invariants in this file. //
|
||
|
#[cfg(not(target_pointer_width = "64"))]
|
||
|
compile_error!("Cuprate is only compatible with 64-bit CPUs");
|
||
|
// //
|
||
|
// //
|
||
|
//============================ SAFETY: DO NOT REMOVE ===========================//
|
||
|
|
||
|
//---------------------------------------------------------------------------------------------------- Free functions
|
||
|
/// 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() {
|
||
|
assert_eq!(u32_to_usize(u32::MAX), u32::MAX as usize);
|
||
|
assert_eq!(usize_to_u64(u32_to_usize(u32::MAX)), u32::MAX as u64);
|
||
|
|
||
|
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() {
|
||
|
assert_eq!(i32_to_isize(i32::MAX), i32::MAX as isize);
|
||
|
assert_eq!(isize_to_i64(i32_to_isize(i32::MAX)), i32::MAX as i64);
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|