pub unsafe trait Poke {
// Required methods
fn max_size() -> usize;
unsafe fn poke_into(&self, bytes: *mut u8) -> *mut u8;
}
Expand description
A trait for values that provide serialization into buffers of bytes.
§Example
use peek_poke::Poke;
struct Bar {
a: u32,
b: u8,
c: i16,
}
unsafe impl Poke for Bar {
fn max_size() -> usize {
<u32>::max_size() + <u8>::max_size() + <i16>::max_size()
}
unsafe fn poke_into(&self, bytes: *mut u8) -> *mut u8 {
let bytes = self.a.poke_into(bytes);
let bytes = self.b.poke_into(bytes);
self.c.poke_into(bytes)
}
}
§Safety
The Poke
trait is an unsafe
trait for the reasons, and implementors must
ensure that they adhere to these contracts:
max_size()
query and calculations in general must be correct. Callers of this trait are expected to rely on the contract defined on each method, and implementors must ensure such contracts remain true.
Required Methods§
Sourcefn max_size() -> usize
fn max_size() -> usize
Return the maximum number of bytes that the serialized version of Self
will occupy.
§Safety
Implementors of Poke
guarantee to not write more than the result of
calling max_size()
into the buffer pointed to by bytes
when
poke_into()
is called.
Sourceunsafe fn poke_into(&self, bytes: *mut u8) -> *mut u8
unsafe fn poke_into(&self, bytes: *mut u8) -> *mut u8
Serialize into the buffer pointed to by bytes
.
Returns a pointer to the next byte after the serialized representation of Self
.
§Safety
This function is unsafe because undefined behavior can result if the caller does not ensure all of the following:
-
bytes
must denote a valid pointer to a block of memory. -
bytes
must pointer to at least the number of bytes returned bymax_size()
.
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.