As DK suggests , you probably shouldn't use unsafecode to rethink memory ... but you can if you want.
If you really want to go this route, you should know a couple of errors:
- You may have an alignment problem. If you just take it from somewhere
&mut [u8]and convert it to &mut [u16], it may refer to some area of memory that is not properly aligned to access it as u16. Depending on which computer you are running this code on, such unaligned memory access may not be acceptable. In this case, the program is likely to be interrupted somehow. For example, a processor may generate some kind of signal that the operating system responds to complete the process. - It will be unbearable. Even without the alignment problem, you will get different results on different machines (ordinal machines little- and big-).
( u16 u16 ), :
unsafe fn raw_byte_access(s16: &mut [u16]) -> &mut [u8] {
use std::slice;
slice::from_raw_parts_mut(s16.as_mut_ptr() as *mut u8, s16.len() * 2)
}
big- , ; little-. little-, , DK, big- .