pub struct Encoder { /* private fields */ }Expand description
A protobuf message under construction.
Sub-messages are built as their own Encoder and then handed to
Encoder::message, because a length-delimited field has to know its
length before it writes its body and the only honest way to know that is to
have encoded the body already.
Implementations§
Source§impl Encoder
impl Encoder
pub fn new() -> Self
pub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn uint64(&mut self, field: u32, value: u64)
pub fn uint64(&mut self, field: u32, value: u64)
A uint32/uint64 field. Zero is proto3’s default for these, and a
field left out decodes back to the default, so writing it would only
make the payload bigger.
pub fn uint32(&mut self, field: u32, value: u32)
Sourcepub fn int64(&mut self, field: u32, value: i64)
pub fn int64(&mut self, field: u32, value: i64)
An int64 field. Negative values sign-extend to a full 64 bits, which
is why protobuf spends ten bytes on -1 — sint64 is the zig-zag
alternative, and OTLP does not use it anywhere this crate encodes.
pub fn bool(&mut self, field: u32, value: bool)
Sourcepub fn bool_present(&mut self, field: u32, value: bool)
pub fn bool_present(&mut self, field: u32, value: bool)
Fields inside a oneof have explicit presence, so their default value
still has to be written.
This is not a micro-optimisation in reverse — it is the difference
between an attribute arriving as false and arriving as nothing at all.
A decoder tracks which arm of a oneof is set by which tag it saw, so
omitting bool_value = false sets no arm and the collector prints
Empty(). OTLP’s AnyValue and NumberDataPoint.value are both
oneofs, and a real collector is where this was caught.
Sourcepub fn int64_present(&mut self, field: u32, value: i64)
pub fn int64_present(&mut self, field: u32, value: i64)
See Encoder::bool_present — AnyValue.int_value.
Sourcepub fn string_present(&mut self, field: u32, value: &str)
pub fn string_present(&mut self, field: u32, value: &str)
See Encoder::bool_present — AnyValue.string_value.
Sourcepub fn sfixed64_present(&mut self, field: u32, value: i64)
pub fn sfixed64_present(&mut self, field: u32, value: i64)
See Encoder::bool_present — NumberDataPoint.as_int.
Sourcepub fn enumeration(&mut self, field: u32, value: i32)
pub fn enumeration(&mut self, field: u32, value: i32)
An enum field. Enums travel as varints, and every OTLP enum reserves zero for “unspecified”, so omitting zero is right here too.
Sourcepub fn fixed64(&mut self, field: u32, value: u64)
pub fn fixed64(&mut self, field: u32, value: u64)
A fixed64 field: eight little-endian bytes, no varint compression.
OTLP uses it for timestamps, where the values are large enough that a
varint would cost more than eight bytes anyway.
Sourcepub fn fixed32(&mut self, field: u32, value: u32)
pub fn fixed32(&mut self, field: u32, value: u32)
A fixed32 field: four little-endian bytes. LogRecord.flags is the one
user here, carrying the W3C trace flags.
Sourcepub fn sfixed64(&mut self, field: u32, value: i64)
pub fn sfixed64(&mut self, field: u32, value: i64)
A sfixed64 field — NumberDataPoint.as_int, and nothing else here.
Sourcepub fn present_double(&mut self, field: u32, value: f64)
pub fn present_double(&mut self, field: u32, value: f64)
A double field that is written even when it holds the default.
Proto3 optional double has explicit presence, and OTLP uses it for
HistogramDataPoint.sum, min and max. There, absent means “this
histogram does not report a sum” while zero means “the sum is zero” —
two different facts that the default-skipping Encoder::double would
collapse into one.
Sourcepub fn bytes_field(&mut self, field: u32, value: &[u8])
pub fn bytes_field(&mut self, field: u32, value: &[u8])
A bytes field. Named to leave bytes free for the buffer itself.
Sourcepub fn message(&mut self, field: u32, message: &Encoder)
pub fn message(&mut self, field: u32, message: &Encoder)
A nested message.
Written even when the sub-message encoded to nothing: message fields
have explicit presence in proto3, so an empty Status still says “this
span carries a status” rather than “no status was set”. Callers that
mean the second one simply do not call this.
Sourcepub fn packed_fixed64(&mut self, field: u32, values: &[u64])
pub fn packed_fixed64(&mut self, field: u32, values: &[u64])
A packed repeated fixed64 — HistogramDataPoint.bucket_counts.
Packed means one length-delimited field holding every element back to back, rather than one tag per element. Proto3 packs repeated scalars by default, so a collector expects exactly this.
Sourcepub fn packed_double(&mut self, field: u32, values: &[f64])
pub fn packed_double(&mut self, field: u32, values: &[f64])
A packed repeated double — HistogramDataPoint.explicit_bounds.