Trait AsBytes

Source
pub unsafe trait AsBytes {
    // Provided methods
    fn as_bytes(&self) -> &[u8] { ... }
    fn as_bytes_mut(&mut self) -> &mut [u8]
       where Self: FromBytes { ... }
}
Expand description

Types that can be viewed as an immutable slice of initialized bytes.

If a struct implements this trait, then it is okay to copy it byte-for-byte to userspace. This means that it should not have any padding, as padding bytes are uninitialized. Reading uninitialized memory is not just undefined behavior, it may even lead to leaking sensitive information on the stack to userspace.

The struct should also not hold kernel pointers, as kernel pointer addresses are also considered sensitive. However, leaking kernel pointers is not considered undefined behavior by Rust, so this is a correctness requirement, but not a safety requirement.

§Safety

Values of this type may not contain any uninitialized bytes. This type must not have interior mutability.

Provided Methods§

Source

fn as_bytes(&self) -> &[u8]

Returns self as a slice of bytes.

Source

fn as_bytes_mut(&mut self) -> &mut [u8]
where Self: FromBytes,

Returns self as a mutable slice of bytes.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl AsBytes for bool

Source§

impl AsBytes for char

Source§

impl AsBytes for i8

Source§

impl AsBytes for i16

Source§

impl AsBytes for i32

Source§

impl AsBytes for i64

Source§

impl AsBytes for isize

Source§

impl AsBytes for str

Source§

impl AsBytes for u8

Source§

impl AsBytes for u16

Source§

impl AsBytes for u32

Source§

impl AsBytes for u64

Source§

impl AsBytes for usize

Source§

impl<T: AsBytes> AsBytes for [T]

Source§

impl<T: AsBytes, const N: usize> AsBytes for [T; N]

Implementors§