Read a `T` from `bytes` at the `curr_offset` and advance by `size` bytes. # Safety Let `value = &bytes[range_move(0..size_of:: (), *curr_offset)]`. Then `value` must point to a valid `T` and must be properly aligned for `T`.
(bytes: &Bytes, curr_offset: &mut usize)
| 217 | /// Let `value = &bytes[range_move(0..size_of::<T>(), *curr_offset)]`. |
| 218 | /// Then `value` must point to a valid `T` and must be properly aligned for `T`. |
| 219 | pub unsafe fn read_from_bytes<T: Copy>(bytes: &Bytes, curr_offset: &mut usize) -> T { |
| 220 | let bytes = &bytes[*curr_offset..]; |
| 221 | *curr_offset += mem::size_of::<T>(); |
| 222 | // TODO: Endianness concerns? Do we need to explicitly read as little-endian here? |
| 223 | let ptr: *const T = bytes.as_ptr().cast(); |
| 224 | // SAFETY: Caller promised that `ptr` points to a `T`. |
| 225 | // Moreover, `ptr` is derived from a shared reference with permission to read this range. |
| 226 | unsafe { *ptr } |
| 227 | } |
| 228 | |
| 229 | #[cfg(test)] |
| 230 | mod tests { |
no test coverage detected
searching dependent graphs…