Function from_bytes
pub fn from_bytes<T, E>(bytes: &[u8]) -> Result<T, E> ⓘwhere
T: Archive,
<T as Archive>::Archived: for<'a> CheckBytes<Strategy<Validator<ArchiveValidator<'a>, ()>, E>> + Deserialize<T, Strategy<Unpool, E>>,
E: Source,
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);