[go: up one dir, main page]

root_position

Function root_position 

Source
pub fn root_position<T: Portable>(size: usize) -> usize
Expand description

Return the position of the root within a buffer of length bytes.

Most accessing functions have a variant which automatically calculates this value for you. For example, prefer to call access_unchecked over access_pos_unchecked.

The root position of a buffer is calculated by subtracing the size of the root object from the end of the buffer. If the buffer size is too small to accomodate a root of the given type, then this function will return zero.

ยงExample

use rkyv::{api::root_position, Archive};

#[derive(Archive)]
pub struct MyData {
    inner: u32,
}

assert_eq!(size_of::<ArchivedMyData>(), 4);

// This is too small, and so returns 0
assert_eq!(root_position::<ArchivedMyData>(3), 0);
assert_eq!(root_position::<ArchivedMyData>(4), 0);
assert_eq!(root_position::<ArchivedMyData>(5), 1);