Skip to main content

to_bytes_in_unchecked

Function to_bytes_in_unchecked 

Source
pub unsafe fn to_bytes_in_unchecked<'a, T: Serialize + ?Sized>(
    value: &T,
    buffer: &'a mut [MaybeUninit<u8>],
) -> usize
Expand description

Serialize a value and write the bytes to the given buffer without checking the length or validity.

§Safety

buffer must be large enough to store the given value

§Example

#![feature(ptr_alignment_type)]

use core::fmt::Debug;

use nibblecode::aligned_alloc::new_uninit_boxed_slice;
use nibblecode::{Serialize, access, to_bytes_in_unchecked};

#[derive(Serialize, Debug)]
#[nibblecode(compare(PartialEq), derive(Debug))]
struct Example {
	name: String,
	value: i32,
}

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

let mut bytes = new_uninit_boxed_slice::<Example>(2);
let bytes = unsafe {
	to_bytes_in_unchecked(&value, &mut bytes);
	bytes.assume_init()
};

let archived = access::<Example>(&bytes).unwrap();

assert_eq!(*archived, value);