Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. fn i_from_slice<T>(slice: &[u8]) -> Option<T>
  2. {
  3. let n = std::mem::size_of::<T>();
  4. if slice.len() == n {
  5. let mut buf = vec![0; n];
  6. buf.copy_from_slice(slice);
  7. Some(unsafe { std::mem::transmute_copy(&buf) })
  8. } else {
  9. None
  10. }
  11. }
  12.  
  13. fn main() {
  14. let x = [0xFF,0xFF]; // should transmute to be 65535
  15. println!("{:?}", i_from_slice::<u16>(&x));
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement