Line data Source code
1 : //! `u64`` and `usize`` aren't guaranteed to be identical in Rust, but life is much simpler if that's the case.
2 :
3 : pub(crate) const _ASSERT_U64_EQ_USIZE: () = {
4 : if std::mem::size_of::<usize>() != std::mem::size_of::<u64>() {
5 : panic!(
6 : "the traits defined in this module assume that usize and u64 can be converted to each other without loss of information"
7 : );
8 : }
9 : };
10 :
11 : pub(crate) trait U64IsUsize {
12 : fn into_usize(self) -> usize;
13 : }
14 :
15 : impl U64IsUsize for u64 {
16 : #[inline(always)]
17 16786551 : fn into_usize(self) -> usize {
18 16786551 : #[allow(clippy::let_unit_value)]
19 16786551 : let _ = _ASSERT_U64_EQ_USIZE;
20 16786551 : self as usize
21 16786551 : }
22 : }
23 :
24 : pub(crate) trait UsizeIsU64 {
25 : fn into_u64(self) -> u64;
26 : }
27 :
28 : impl UsizeIsU64 for usize {
29 : #[inline(always)]
30 39199348 : fn into_u64(self) -> u64 {
31 39199348 : #[allow(clippy::let_unit_value)]
32 39199348 : let _ = _ASSERT_U64_EQ_USIZE;
33 39199348 : self as u64
34 39199348 : }
35 : }
36 :
37 488 : pub const fn u64_to_usize(x: u64) -> usize {
38 488 : #[allow(clippy::let_unit_value)]
39 488 : let _ = _ASSERT_U64_EQ_USIZE;
40 488 : x as usize
41 488 : }
|