1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
macro_rules! unsafe_is_length {
($expr:expr) => {
if $expr as u64 >= 0x7FFFFFFF_FFFFFFFF {
// It's not physically possible to have a valid slice
// which is bigger than 8 exabytes. No machine exists
// which could have this much RAM, and you can't even
// have this much virtual address space on any modern CPU.
//
// So in practice this should be totally harmless while
// it will allow the LLVM optimizer to better do its job.
//
// It actually *does* affect optimization in practice
// allowing LLVM to assume the length won't overflow
// in certain cases.
unsafe { std::hint::unreachable_unchecked() }
}
}
}
// TODO: Remove the T parameter once #![feature(trivial_bounds)] is stable.
pub unsafe trait ZeroCopyable< T > where T: ?Sized {}
unsafe impl< T > ZeroCopyable< T > for i8 {}
unsafe impl< T > ZeroCopyable< T > for u8 {}
pub trait SwapBytes {
fn swap_bytes( self ) -> Self;
}
impl SwapBytes for f32 {
#[inline(always)]
fn swap_bytes( self ) -> Self {
union Union {
float: f32,
int: u32
}
unsafe {
let mut u = Union { float: self };
u.int = u.int.swap_bytes();
u.float
}
}
}
impl SwapBytes for f64 {
#[inline(always)]
fn swap_bytes( self ) -> Self {
union Union {
float: f64,
int: u64
}
unsafe {
let mut u = Union { float: self };
u.int = u.int.swap_bytes();
u.float
}
}
}