WriteExtProtobuf

Trait WriteExtProtobuf 

Source
pub trait WriteExtProtobuf {
    // Required methods
    fn write_protobuf_field(&mut self, field: &Field) -> Result<usize>;
    fn write_protobuf_fields<'a, I>(&mut self, fields: I) -> Result<usize>
       where I: IntoIterator<Item = &'a Field>;
}
Expand description

Extension trait for writing raw Protocol Buffer fields to Write types

This trait provides low-level utilities for writing fields to a byte stream. It is the counterpart to ReadExtProtobuf for serialization.

Required Methods§

Source

fn write_protobuf_field(&mut self, field: &Field) -> Result<usize>

Write a single raw protobuf field to the writer (tag + value)

Returns the number of bytes written.

§Example
use protobuf_core::field::{WriteExtProtobuf, Field, FieldValue};
use protobuf_core::field_number::FieldNumber;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut buffer = Vec::new();
     
    let field = Field::new(
        FieldNumber::try_from(1)?,
        FieldValue::from_uint64(150)
    );
     
    buffer.write_protobuf_field(&field)?;
    assert_eq!(buffer, vec![0x08, 0x96, 0x01]); // field 1: 150
    Ok(())
}
Source

fn write_protobuf_fields<'a, I>(&mut self, fields: I) -> Result<usize>
where I: IntoIterator<Item = &'a Field>,

Write multiple raw protobuf fields to the writer

Returns the total number of bytes written.

§Example
use protobuf_core::field::{WriteExtProtobuf, Field, FieldValue};
use protobuf_core::field_number::FieldNumber;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut buffer = Vec::new();
     
    let fields = vec![
        Field::new(FieldNumber::try_from(1)?, FieldValue::from_uint64(150)),
        Field::new(FieldNumber::try_from(2)?, FieldValue::from_string("Hello".to_string())),
    ];
     
    buffer.write_protobuf_fields(&fields)?;
    Ok(())
}

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.

Implementors§

Source§

impl<W> WriteExtProtobuf for W
where W: Write,