Skip to main content

Encoder

Struct Encoder 

Source
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

Source

pub fn new() -> Self

Source

pub fn into_bytes(self) -> Vec<u8> ⓘ

Source

pub fn as_bytes(&self) -> &[u8] ⓘ

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

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.

Source

pub fn uint32(&mut self, field: u32, value: u32)

Source

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.

Source

pub fn bool(&mut self, field: u32, value: bool)

Source

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.

Source

pub fn int64_present(&mut self, field: u32, value: i64)

See Encoder::bool_present — AnyValue.int_value.

Source

pub fn string_present(&mut self, field: u32, value: &str)

See Encoder::bool_present — AnyValue.string_value.

Source

pub fn sfixed64_present(&mut self, field: u32, value: i64)

See Encoder::bool_present — NumberDataPoint.as_int.

Source

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.

Source

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.

Source

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.

Source

pub fn sfixed64(&mut self, field: u32, value: i64)

A sfixed64 field — NumberDataPoint.as_int, and nothing else here.

Source

pub fn double(&mut self, field: u32, value: f64)

A double field, IEEE 754 little-endian.

Source

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.

Source

pub fn string(&mut self, field: u32, value: &str)

A string field: length-delimited UTF-8.

Source

pub fn bytes_field(&mut self, field: u32, value: &[u8])

A bytes field. Named to leave bytes free for the buffer itself.

Source

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.

Source

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.

Source

pub fn packed_double(&mut self, field: u32, values: &[f64])

A packed repeated double — HistogramDataPoint.explicit_bounds.

Trait Implementations§

Source§

impl Clone for Encoder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Encoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Encoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.