unsafe_transmute.rs

RustMaster

Explorer

C40
C++40
C#40
Dart40
Go40
Java40
JavaScript40
Kotlin40
Objective-C40
Perl40
PHP40
Python40
R40
Ruby40
Rust
Beginner10
Intermediate10
Advanced10
Expert5
Master5
async_channel.rs
lifetime_bounds.rs
trait_object.rs
macro_recursive.rs
unsafe_transmute.rs
Shell40
Swift40
TypeScript40
unsafe_transmute.rs
Click to focus
1
2
3
4
5
6
7
8
9
10
unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
   std::slice::from_raw_parts(
       (p as *const T) as *const u8,
       std::mem::size_of::<T>()
   )
}
fn hash<T: Sized>(value: &T) -> u64 {
   let bytes = unsafe { any_as_u8_slice(value) };
   bytes.iter().fold(0u64, |acc, &b| acc.wrapping_mul(31).wrapping_add(b as u64))
}
0WPM0%0:000/332(0%)|Ln 1, Col 1
UTF-8Rust

Related Exercises