devela::_dep::rkyv::bytecheck

Function check_bytes

pub unsafe fn check_bytes<T, E>(value: *const T) -> Result<(), E> 
where T: CheckBytes<Strategy<(), E>> + ?Sized,
Available on crate feature dep_rkyv only.
Expand description

Checks whether the given pointer points to a valid value.

§Safety

The passed pointer must be aligned and point to enough initialized bytes to represent the type.

§Example

use bytecheck::check_bytes;
use rancor::Failure;

unsafe {
    // 0 and 1 are valid values for bools
    check_bytes::<bool, Failure>((&0u8 as *const u8).cast()).unwrap();
    check_bytes::<bool, Failure>((&1u8 as *const u8).cast()).unwrap();

    // 2 is not a valid value
    check_bytes::<bool, Failure>((&2u8 as *const u8).cast()).unwrap_err();
}