devela::_dep::rkyv::api

Function root_position

pub fn root_position<T>(size: usize) -> usize 
where T: Portable,
Available on crate feature dep_rkyv only.
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);