pub trait WriteExtProtobuf {
// Required methods
fn write_protobuf_field<L: AsRef<[u8]>>(
&mut self,
field: &Field<L>,
) -> Result<usize>;
fn write_protobuf_fields<'a, L: AsRef<[u8]> + 'a, I>(
&mut self,
fields: I,
) -> Result<usize>
where I: IntoIterator<Item = &'a Field<L>>;
}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§
Sourcefn write_protobuf_field<L: AsRef<[u8]>>(
&mut self,
field: &Field<L>,
) -> Result<usize>
fn write_protobuf_field<L: AsRef<[u8]>>( &mut self, field: &Field<L>, ) -> Result<usize>
Write a single raw protobuf field to the writer (tag + value)
Returns the number of bytes written.
§Example
use ::protobuf_core::{WriteExtProtobuf, Field, FieldValue, FieldNumber};
fn main() -> Result<(), Box<dyn ::std::error::Error>> {
let mut buffer = Vec::new();
let field: Field<Vec<u8>> = 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(())
}Sourcefn write_protobuf_fields<'a, L: AsRef<[u8]> + 'a, I>(
&mut self,
fields: I,
) -> Result<usize>where
I: IntoIterator<Item = &'a Field<L>>,
fn write_protobuf_fields<'a, L: AsRef<[u8]> + 'a, I>(
&mut self,
fields: I,
) -> Result<usize>where
I: IntoIterator<Item = &'a Field<L>>,
Write multiple raw protobuf fields to the writer
Returns the total number of bytes written.
§Example
use ::protobuf_core::{WriteExtProtobuf, Field, FieldValue, 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::Len("Hello".to_string().into_bytes())),
];
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.