Trait bytecheck::CheckBytes

source ·
pub unsafe trait CheckBytes<C: Fallible + ?Sized> {
    // Required method
    unsafe fn check_bytes(
        value: *const Self,
        context: &mut C,
    ) -> Result<(), C::Error>;
}
Expand description

A type that can check whether a pointer points to a valid value.

CheckBytes can be derived with CheckBytes or implemented manually for custom behavior.

§Safety

check_bytes must only return Ok if value points to a valid instance of Self. Because value must always be properly aligned for Self and point to enough bytes to represent the type, this implies that value may be dereferenced safely.

§Example

use core::{error::Error, fmt};

use bytecheck::CheckBytes;
use rancor::{fail, Fallible, Source};

#[repr(C, align(4))]
pub struct NonMaxU32(u32);

unsafe impl<C: Fallible + ?Sized> CheckBytes<C> for NonMaxU32
where
    C::Error: Source,
{
    unsafe fn check_bytes(
        value: *const Self,
        context: &mut C,
    ) -> Result<(), C::Error> {
        #[derive(Debug)]
        struct NonMaxCheckError;

        impl fmt::Display for NonMaxCheckError {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "non-max u32 was set to u32::MAX")
            }
        }

        impl Error for NonMaxCheckError {}

        let value = unsafe { value.read() };
        if value.0 == u32::MAX {
            fail!(NonMaxCheckError);
        }

        Ok(())
    }
}

See Verify for an example which uses less unsafe.

Required Methods§

source

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

Checks whether the given pointer points to a valid value within the given context.

§Safety

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<C> CheckBytes<C> for bool
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for char
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(ptr: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for str
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for CStr
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for AtomicBool
where C: Fallible + ?Sized, C::Error: Source,

Available on target_has_atomic="8" only.
source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroI8
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroI16
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroI32
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroI64
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroI128
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroU8
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroU16
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroU32
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroU64
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C> CheckBytes<C> for NonZeroU128
where C: Fallible + ?Sized, C::Error: Source,

source§

unsafe fn check_bytes(value: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for f32

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for f64

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for i8

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for i16

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for i32

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for i64

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for i128

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for u8

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for u16

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for u32

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for u64

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for u128

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for ()

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for PhantomPinned

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for RangeFull

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for AtomicI8

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for AtomicI16

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for AtomicI32

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for AtomicI64

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for AtomicU8

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for AtomicU16

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for AtomicU32

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<C: Fallible + ?Sized> CheckBytes<C> for AtomicU64

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

source§

impl<T0, C> CheckBytes<C> for (T0,)
where T0: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, C> CheckBytes<C> for (T0, T1)
where T0: CheckBytes<C>, T1: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, C> CheckBytes<C> for (T0, T1, T2)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, C> CheckBytes<C> for (T0, T1, T2, T3)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, T4, C> CheckBytes<C> for (T0, T1, T2, T3, T4)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, T4: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, T4, T5, C> CheckBytes<C> for (T0, T1, T2, T3, T4, T5)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, T4: CheckBytes<C>, T5: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, T4, T5, T6, C> CheckBytes<C> for (T0, T1, T2, T3, T4, T5, T6)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, T4: CheckBytes<C>, T5: CheckBytes<C>, T6: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, C> CheckBytes<C> for (T0, T1, T2, T3, T4, T5, T6, T7)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, T4: CheckBytes<C>, T5: CheckBytes<C>, T6: CheckBytes<C>, T7: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, C> CheckBytes<C> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, T4: CheckBytes<C>, T5: CheckBytes<C>, T6: CheckBytes<C>, T7: CheckBytes<C>, T8: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, C> CheckBytes<C> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, T4: CheckBytes<C>, T5: CheckBytes<C>, T6: CheckBytes<C>, T7: CheckBytes<C>, T8: CheckBytes<C>, T9: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, C> CheckBytes<C> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, T4: CheckBytes<C>, T5: CheckBytes<C>, T6: CheckBytes<C>, T7: CheckBytes<C>, T8: CheckBytes<C>, T9: CheckBytes<C>, T10: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, C> CheckBytes<C> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, T4: CheckBytes<C>, T5: CheckBytes<C>, T6: CheckBytes<C>, T7: CheckBytes<C>, T8: CheckBytes<C>, T9: CheckBytes<C>, T10: CheckBytes<C>, T11: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, C> CheckBytes<C> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
where T0: CheckBytes<C>, T1: CheckBytes<C>, T2: CheckBytes<C>, T3: CheckBytes<C>, T4: CheckBytes<C>, T5: CheckBytes<C>, T6: CheckBytes<C>, T7: CheckBytes<C>, T8: CheckBytes<C>, T9: CheckBytes<C>, T10: CheckBytes<C>, T11: CheckBytes<C>, T12: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T, C> CheckBytes<C> for [T]
where T: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T, C> CheckBytes<C> for Cell<T>
where T: CheckBytes<C> + ?Sized, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes(value: *const Self, c: &mut C) -> Result<(), C::Error>

source§

impl<T, C> CheckBytes<C> for UnsafeCell<T>
where T: CheckBytes<C> + ?Sized, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes(value: *const Self, c: &mut C) -> Result<(), C::Error>

source§

impl<T, C> CheckBytes<C> for ManuallyDrop<T>
where T: CheckBytes<C> + ?Sized, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes(value: *const Self, c: &mut C) -> Result<(), C::Error>

source§

impl<T, C> CheckBytes<C> for Range<T>
where T: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T, C> CheckBytes<C> for RangeFrom<T>
where T: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T, C> CheckBytes<C> for RangeTo<T>
where T: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T, C> CheckBytes<C> for RangeToInclusive<T>
where T: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T, const N: usize, C> CheckBytes<C> for [T; N]
where T: CheckBytes<C>, C: Fallible + ?Sized, C::Error: Trace,

source§

unsafe fn check_bytes( value: *const Self, context: &mut C, ) -> Result<(), C::Error>

source§

impl<T: ?Sized, C: Fallible + ?Sized> CheckBytes<C> for PhantomData<T>

source§

unsafe fn check_bytes(_: *const Self, _: &mut C) -> Result<(), C::Error>

Implementors§