MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / read_from_bytes

Function read_from_bytes

crates/table/src/row_hash.rs:219–227  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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`.
219pub 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)]
230mod tests {

Callers 1

hash_valueFunction · 0.70

Calls 1

castMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…