Skip to main content

EncodeSink

Trait EncodeSink 

Source
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_to that produces more bytes than its compute_size declared panics.
  • A BufMut wrapper that observes bytes in its own put_* overrides, such as a checksum or a tee, does not see them individually: it sees advance_mut when the message fits its chunk and one put_slice otherwise. Observe them in advance_mut, or implement EncodeSink and not BufMut.

§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§

Source

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§

Source

fn put_u8(&mut self, value: u8)

Append a single byte.

Source

fn put_slice(&mut self, src: &[u8])

Append a borrowed slice.

Source

fn put_u32_le(&mut self, value: u32)

Append a u32 in little-endian byte order (protobuf fixed32).

Source

fn put_u64_le(&mut self, value: u64)

Append a u64 in little-endian byte order (protobuf fixed64).

Provided Methods§

Source

fn put_i32_le(&mut self, value: i32)

Append an i32 in little-endian byte order (protobuf sfixed32).

Source

fn put_i64_le(&mut self, value: i64)

Append an i64 in little-endian byte order (protobuf sfixed64).

Source

fn put_f32_le(&mut self, value: f32)

Append an f32 in little-endian byte order (protobuf float).

Source

fn put_f64_le(&mut self, value: f64)

Append an f64 in little-endian byte order (protobuf double).

Source

fn put_shared(&mut self, bytes: Bytes)

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".

Implementors§