pub unsafe trait Encode {
// Required methods
fn size(&self) -> Size;
unsafe fn encode_unchecked(&self, w: &mut Writer<'_>);
}Expand description
A trait for encoding values into a buffer.
§Note
This trait is intended for low-level implementation, which is also unsafe to
call. Instead, use the EncodeExt trait, which allows safely constructing
encodable sequences.
§Safety
This trait is unsafe because unsafe code relies on the safety invariants upheld by implementations. To implement this trait correctly, the following invariants must be maintained:
-
sizemust always return the exact number of bytes required to encode the value. In particular,sizemust consistently return the same value ifselfhas not changed. -
encode_uncheckedmust initialize exactlysizebytes in the passed buffer.
Required Methods§
Sourcefn size(&self) -> Size
fn size(&self) -> Size
Returns byte size of encodable value.
§Note
Although the Size type contains a usize and checks for overflow in
its expand method, the Encode trait cannot encode a
value larger than isize::MAX bytes sequentially. However, it is
impossible to create a Writer with a buffer that exceeds this limit, so
calling encode_unchecked with such a size is
also impossible.