Struct anybuf::Anybuf

source ·
pub struct Anybuf { /* private fields */ }

Implementations§

source§

impl Anybuf

source

pub fn new() -> Self

Creates a new serializer.

source

pub fn append_bytes(self, field_number: u32, data: impl AsRef<[u8]>) -> Self

Appends a bytes field with the given field number.

source

pub fn append_string(self, field_number: u32, data: impl AsRef<str>) -> Self

Appends a string field with the given field number.

source

pub fn append_uint64(self, field_number: u32, value: u64) -> Self

Appends a uint64 field with the given field number.

source

pub fn append_uint32(self, field_number: u32, value: u32) -> Self

Appends a uint32 field with the given field number.

source

pub fn append_bool(self, field_number: u32, value: bool) -> Self

Appends a bool field with the given field number.

source

pub fn append_sint32(self, field_number: u32, value: i32) -> Self

Appends an sint32 field with the given field number.

Please note that protobuf has two different 32 bit signed integer types with different encodings: sint32 and int32. This only works for the former.

source

pub fn append_sint64(self, field_number: u32, value: i64) -> Self

Appends an sint64 field with the given field number.

Please note that protobuf has two different 64 bit signed integer types with different encodings: sint64 and int64. This only works for the former.

source

pub fn append_message(self, field_number: u32, value: &Anybuf) -> Self

Appends a nested protobuf message with the given field number.

source

pub fn append_repeated_uint32(self, field_number: u32, data: &[u32]) -> Self

Appends a repeated field of type uint32.

Use this instead of multiple Anybuf::append_uint32 to ensure 0 values are not lost.

source

pub fn append_repeated_uint64(self, field_number: u32, data: &[u64]) -> Self

Appends a repeated field of type uint64.

Use this instead of multiple Anybuf::append_uint64 to ensure 0 values are not lost.

source

pub fn append_repeated_sint32(self, field_number: u32, data: &[i32]) -> Self

Appends a repeated field of type sint32.

Use this instead of multiple Anybuf::append_sint32 to ensure 0 values are not lost.

Example
// Three signed ints with field number 4
let serialized = Anybuf::new()
    .append_repeated_sint32(3, &[-30, 0, 17])
    .into_vec();
source

pub fn append_repeated_sint64(self, field_number: u32, data: &[i64]) -> Self

Appends a repeated field of type sint64.

Use this instead of multiple Anybuf::append_sint64 to ensure 0 values are not lost.

Example
// Three signed ints with field number 4
let serialized = Anybuf::new()
    .append_repeated_sint64(4, &[-30, 0, 17])
    .into_vec();
source

pub fn append_repeated_bool(self, field_number: u32, data: &[bool]) -> Self

Appends a repeated field of type bool.

Use this instead of multiple Anybuf::append_bool to ensure false values are not lost.

source

pub fn append_repeated_string(self, field_number: u32, data: &[&str]) -> Self

Appends a repeated field of type string.

Use this instead of multiple Anybuf::append_string to ensure “” values are not lost.

Example
let name = "Bono".to_string();

// Three string fields with field number 4
let serialized = Anybuf::new()
    .append_repeated_string(4, &["", "Caro", &name])
    .into_vec();
source

pub fn append_repeated_bytes(self, field_number: u32, data: &[&[u8]]) -> Self

Appends a repeated field of type bytes.

Use this instead of multiple Anybuf::append_bytes to ensure empty values are not lost.

Example
let blob = vec![4u8; 75];

// Three bytes fields with field number 5
let serialized = Anybuf::new()
    .append_repeated_bytes(5, &[b"", b"abcd", &blob])
    .into_vec();
source

pub fn append_repeated_message( self, field_number: u32, messages: &[&Anybuf] ) -> Self

Appends a repeated field of type message.

Use this instead of multiple Anybuf::append_message to ensure empty values are not lost.

Example
// A repeated message at field number 11. This adds 3 elements, one of them is the default message.
let serialized = Anybuf::new()
    .append_repeated_message(11, &[
        &Anybuf::new().append_uint32(1, 1),
        &Anybuf::new(),
        &Anybuf::new().append_uint32(1, 3),
    ])
    .into_vec();
source

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

Returns the protobuf bytes of the instance.

The data is the same as Anybuf::into_vec but does not consume the instance.

source

pub fn into_vec(self) -> Vec<u8>

Takes the instance and returns the protobuf bytes.

The data is the same as Anybuf::as_bytes but consumes the instance in order to return an owned vector without cloning the data.

Trait Implementations§

source§

impl Default for Anybuf

source§

fn default() -> Anybuf

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

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.