Function rkyv::api::low::from_bytes_unchecked

source ·
pub unsafe fn from_bytes_unchecked<T, E>(bytes: &[u8]) -> Result<T, E>
Expand description

Deserialize a value from the given bytes.

This function does not check that the data is valid. Use from_bytes to validate the data instead.

This is part of the low-level API.

§Safety

The byte slice must represent a valid archived type when accessed at the default root position. See the module docs for more information.

§Example

use core::mem::MaybeUninit;

use rkyv::{
    access_unchecked,
    api::low::{from_bytes_unchecked, to_bytes_in_with_alloc},
    rancor::Failure,
    ser::{allocator::SubAllocator, writer::Buffer},
    util::Align,
    with::InlineAsBox,
    Archive, Deserialize, Serialize,
};

let mut output = Align([MaybeUninit::<u8>::uninit(); 256]);
let mut alloc = [MaybeUninit::<u8>::uninit(); 256];

#[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
struct Example {
    inner: i32,
}

let value = Example { inner: 42 };

let bytes = to_bytes_in_with_alloc::<_, _, Failure>(
    &value,
    Buffer::from(&mut *output),
    SubAllocator::new(&mut alloc),
)
.unwrap();

let deserialized =
    unsafe { from_bytes_unchecked::<Example, Failure>(&*bytes).unwrap() };
assert_eq!(value, deserialized);