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§
Sourcefn as_bytes_mut(&mut self) -> &mut [u8]where
Self: FromBytes,
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.