pub trait EncodeSink {
const IS_SEGMENTED: bool = false;
// Required methods
fn put_u8(&mut self, value: u8);
fn put_slice(&mut self, src: &[u8]);
fn put_u32_le(&mut self, value: u32);
fn put_u64_le(&mut self, value: u64);
// Provided methods
fn put_i32_le(&mut self, value: i32) { ... }
fn put_i64_le(&mut self, value: i64) { ... }
fn put_f32_le(&mut self, value: f32) { ... }
fn put_f64_le(&mut self, value: f64) { ... }
fn put_shared(&mut self, bytes: Bytes) { ... }
}Expand description
Byte sink for message encoding.
The write surface is deliberately the narrow subset of BufMut that
the encoding primitives use (put_u8,
put_slice, the little-endian fixed-width writers),
plus put_shared for owned segments. Other BufMut
methods (put_u16, big-endian writers, advance_mut, …) are
intentionally absent — a manual write_to that needs them should
assemble into a concrete buffer first.
§Implementors
You almost never implement this trait: the blanket impl covers every
BufMut (Vec<u8>, BytesMut, &mut [u8], …), and Rope is the
built-in segmented sink. A custom implementation must append bytes in
call order — the encoders rely on the sink being strictly sequential —
and receives every write individually.
§Contiguous callers
The blanket impl’s put_shared copies. The encode entry points write a
BufMut through its chunk_mut and advance_mut rather than through
put_slice and put_u8 (see the module documentation), with
two consequences:
- A
write_tothat produces more bytes than itscompute_sizedeclared panics. - A
BufMutwrapper that observes bytes in its ownput_*overrides, such as a checksum or a tee, does not see them individually: it seesadvance_mutwhen the message fits its chunk and oneput_sliceotherwise. Observe them inadvance_mut, or implementEncodeSinkand notBufMut.
§Method-name overlap with BufMut
The method names intentionally match BufMut’s. If both traits are in
scope, calling put_u8/put_slice/put_u32_le/put_u64_le on a
concrete BufMut type is ambiguous (E0034) — disambiguate with
BufMut::put_slice(&mut buf, ..) or drop the unneeded import.
Provided Associated Constants§
Sourceconst IS_SEGMENTED: bool = false
const IS_SEGMENTED: bool = false
Whether this sink can take ownership of Bytes segments without
copying (put_shared is more than a copy).
Encode helpers use this to skip producing a shared handle (an atomic
refcount clone) when the sink would only copy it anyway, and the
encode entry points write a segmented sink through its own write_to
instantiation instead of the shared pre-sized cursor. The constant
folds at monomorphization. false is correct for any sink that does not
capture segments.
Required Methods§
Sourcefn put_u32_le(&mut self, value: u32)
fn put_u32_le(&mut self, value: u32)
Append a u32 in little-endian byte order (protobuf fixed32).
Sourcefn put_u64_le(&mut self, value: u64)
fn put_u64_le(&mut self, value: u64)
Append a u64 in little-endian byte order (protobuf fixed64).
Provided Methods§
Sourcefn put_i32_le(&mut self, value: i32)
fn put_i32_le(&mut self, value: i32)
Append an i32 in little-endian byte order (protobuf sfixed32).
Sourcefn put_i64_le(&mut self, value: i64)
fn put_i64_le(&mut self, value: i64)
Append an i64 in little-endian byte order (protobuf sfixed64).
Sourcefn put_f32_le(&mut self, value: f32)
fn put_f32_le(&mut self, value: f32)
Append an f32 in little-endian byte order (protobuf float).
Sourcefn put_f64_le(&mut self, value: f64)
fn put_f64_le(&mut self, value: f64)
Append an f64 in little-endian byte order (protobuf double).
Append an owned segment.
Contiguous sinks copy (the default). Segmented sinks such as
Rope may take ownership of bytes by reference count when it is
large enough to be worth a segment, making the append O(1) in the
segment’s length.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".