Function rkyv::validation::validators::check_archived_value

source ·
pub fn check_archived_value<'a, T: Archive>(
    bytes: &'a [u8],
    pos: usize,
) -> Result<&T::Archived, CheckTypeError<T::Archived, DefaultValidator<'a>>>
Expand description

Checks the given archive at the given position for an archived version of the given type.

This is a safe alternative to archived_value for types that implement CheckBytes.

§Examples

use rkyv::{
    check_archived_value,
    ser::{Serializer, serializers::AlignedSerializer},
    AlignedVec,
    Archive,
    Serialize,
};
use bytecheck::CheckBytes;

#[derive(Archive, Serialize)]
#[archive_attr(derive(CheckBytes))]
struct Example {
    name: String,
    value: i32,
}

let value = Example {
    name: "pi".to_string(),
    value: 31415926,
};

let mut serializer = AlignedSerializer::new(AlignedVec::new());
let pos = serializer.serialize_value(&value)
    .expect("failed to archive test");
let buf = serializer.into_inner();
let archived = check_archived_value::<Example>(buf.as_ref(), pos).unwrap();