devela::_dep::rkyv::api::low

Function from_bytes

pub fn from_bytes<T, E>(bytes: &[u8]) -> Result<T, E> 
Available on crate feature dep_rkyv only.
Expand description

Deserialize a value from the given bytes.

This is a safe alternative to from_bytes_unchecked and is part of the low-level API.

§Example

use core::mem::MaybeUninit;

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

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

#[derive(Archive, Serialize, Deserialize, PartialEq, Debug)]
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 = from_bytes::<Example, Failure>(&*bytes).unwrap();
assert_eq!(value, deserialized);