pub trait Encode {
const ENCODED_SIZE_HINT: usize = ENCODED_SIZE_DYNAMIC;
// Required methods
fn encoded_size(&self) -> usize;
unsafe fn encode(&self, buffer: *mut u8, offset: &mut usize);
unsafe fn decode_and_format(
buffer: *const u8,
offset: &mut usize,
output: &mut Vec<u8>,
format_spec: &str,
);
// Provided methods
unsafe fn encode_at_offset(&self, buffer: *mut u8, offset: usize) { ... }
unsafe fn decode_and_format_at_offset(
buffer: *const u8,
offset: usize,
output: &mut Vec<u8>,
format_spec: &str,
) { ... }
}Expand description
Trait that must be implemented by all types that can be written to the log queue.
Built-in types are implemented by the library; custom types can derive it
via #[derive(Encode)].
§Three-layer type path
- Layer 1 (Fast Primitive): i32/u64/f64/bool, etc. – dedicated number_format
- Layer 2 (Fast String): &str/String – length-prefixed encoding
- Layer 3 (General Display): user-defined types – Display fallback
Provided Associated Constants§
Sourceconst ENCODED_SIZE_HINT: usize = ENCODED_SIZE_DYNAMIC
const ENCODED_SIZE_HINT: usize = ENCODED_SIZE_DYNAMIC
Compile-time hint for encoded byte width. Dynamic-width types keep the sentinel value.
Required Methods§
Sourcefn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
Returns the number of bytes needed to encode this value into the queue.
Sourceunsafe fn encode(&self, buffer: *mut u8, offset: &mut usize)
unsafe fn encode(&self, buffer: *mut u8, offset: &mut usize)
Encodes the value at buffer + offset (hot path).
§Safety
buffer + offset must have sufficient space (guaranteed by alloc_message).
Sourceunsafe fn decode_and_format(
buffer: *const u8,
offset: &mut usize,
output: &mut Vec<u8>,
format_spec: &str,
)
unsafe fn decode_and_format( buffer: *const u8, offset: &mut usize, output: &mut Vec<u8>, format_spec: &str, )
Decodes from buffer + offset and appends the formatted result to output (cold path).
format_spec is a format specifier such as ".4" or "#x";
an empty string means the default format.
§Safety
buffer + offset must point to valid encoded data.
Provided Methods§
Sourceunsafe fn encode_at_offset(&self, buffer: *mut u8, offset: usize)
unsafe fn encode_at_offset(&self, buffer: *mut u8, offset: usize)
Encodes the value at a fixed byte offset from buffer.
The default implementation forwards to Encode::encode using a local
offset variable. Fixed-width types can override this to perform a direct
write at a compile-time-known payload position.
§Safety
buffer + offset must have sufficient space for the encoded value.
Sourceunsafe fn decode_and_format_at_offset(
buffer: *const u8,
offset: usize,
output: &mut Vec<u8>,
format_spec: &str,
)
unsafe fn decode_and_format_at_offset( buffer: *const u8, offset: usize, output: &mut Vec<u8>, format_spec: &str, )
Decodes from buffer + offset and appends the formatted result.
The default implementation forwards to Encode::decode_and_format
using a local mutable offset. Fixed-width types can override this to
perform a direct read at a compile-time-known payload position.
§Safety
buffer + offset must point to valid encoded data.
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.