Skip to main content

Anybuf

Struct Anybuf 

Source
pub struct Anybuf { /* private fields */ }
Expand description

A minmal protobuf encoder.

Implementations§

Source§

impl Anybuf

Source

pub fn new() -> Self

Creates a new serializer.

See Anybuf::append_message for how to created nested messages using new Anybuf instances.

§Example
// Creates an empty document
let serialized = Anybuf::new().into_vec();
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.

§Example
// A string with field number 4 and 5
let b = String::from("hello, world");
let serialized = Anybuf::new()
    .append_string(4, "nice content")
    .append_string(5, b)
    .into_vec();
Source

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

Appends a uint64 field with the given field number.

§Example
// A uint64 with field number 4 and 5
let serialized = Anybuf::new()
    .append_uint64(4, 3)
    .append_uint64(5, u64::MAX)
    .into_vec();
Source

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

Appends a uint32 field with the given field number.

§Example
// A uint32 with field number 4 and 5
let serialized = Anybuf::new()
    .append_uint32(4, 3)
    .append_uint32(5, u32::MAX)
    .into_vec();
Source

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

Appends a bool field with the given field number.

§Example
// A boolean with field number 4 and 5
let serialized = Anybuf::new()
    .append_bool(4, true)
    .append_bool(5, false)
    .into_vec();
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 sint64.

§Example
// An sint64 with field number 4 and 5
let serialized = Anybuf::new()
    .append_sint64(4, -700)
    .append_sint64(5, i64::MAX)
    .into_vec();
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 sint32.

§Example
// An sint32 with field number 4 and 5
let serialized = Anybuf::new()
    .append_sint32(4, -700)
    .append_sint32(5, i32::MAX)
    .into_vec();
Source

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

Appends an int64 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 later.

§Example
// An int64 with field number 4 and 5
let serialized = Anybuf::new()
    .append_int64(4, -700)
    .append_int64(5, i64::MAX)
    .into_vec();
Source

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

Appends an int32 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 later.

§Example
// An int32 with field number 4 and 5
let serialized = Anybuf::new()
    .append_int32(4, -700)
    .append_int32(5, i32::MAX)
    .into_vec();
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.

§Example
// repeated uint32 fields with number 4 and 5
let serialized = Anybuf::new()
    .append_repeated_uint32(4, &[0, 1, u32::MAX])
    .append_repeated_uint32(5, &[])
    .into_vec();
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.

§Example
// repeated uint64 fields with number 4 and 5
let serialized = Anybuf::new()
    .append_repeated_uint64(4, &[0, 1, u64::MAX])
    .append_repeated_uint64(5, &[])
    .into_vec();
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.

Please note that int32 and sint32 are two different signed integer types. This encodes only correctly for sint32.

§Example
// Three signed ints with field number 4
let serialized = Anybuf::new()
    .append_repeated_sint32(4, &[-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.

Please note that int64 and sint64 are two different signed integer types. This encodes only correctly for sint64.

§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.

§Example
// Some booleans with field number 4
let serialized = Anybuf::new()
    .append_repeated_bool(4, &[true, false, true, true, false])
    .into_vec();
Source

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

Appends a repeated field of type int32.

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

Please note that int32 and sint32 are two different signed integer types. This encodes only correctly for int32.

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

pub fn append_repeated_int64(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.

Please note that int64 and sint64 are two different signed integer types. This encodes only correctly for int64.

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

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

Appends a repeated field of type string.

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

The generic type S is the type of a single element in the input slice. This is typically something like &str or String.

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

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

// Works with String too
let owned: Vec<String> = vec![
    "green".to_string(),
    "orange".to_string(),
];
let serialized = Anybuf::new()
    .append_repeated_string(4, &owned)
    .into_vec();

// Or array of constant values
const A: &str = "Alice";
const B: &str = "Bob";
const ARRAY: [&str; 2] = [A, B];
let serialized = Anybuf::new()
    .append_repeated_string(4, &ARRAY)
    .into_vec();
Source

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

Appends a repeated field of type bytes.

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

The generic type B is the type of a single element in the input slice. This is typically something like &[u8], Vec<u8> but also &str and String work.

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

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

// Works with Vec<u8> elements too
let owned: Vec<Vec<u8>> = vec![
    vec![1u8; 10],
    vec![2u8; 10],
    vec![3u8; 10],
];
let serialized = Anybuf::new()
    .append_repeated_bytes(5, &owned)
    .into_vec();

// Or array of constant values
const A: &[u8] = b"Alice";
const B: &[u8] = b"Bob";
const ARRAY: [&[u8]; 2] = [A, B];
let serialized = Anybuf::new()
    .append_repeated_bytes(5, &ARRAY)
    .into_vec();
Source

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

Appends a repeated field of type message.

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

The generic type M is the type of a single element in the input slice. This is typically &Anybuf or Anybuf.

§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.

Source

pub fn into_x<T: From<Vec<u8>>>(self) -> T

Takes the instance and returns the protobuf bytes. The return type is defined by the caller and can be anything that implements From<Vec<u8>>. Roughly speaking, data.into_x() is the same as calling data.into_vec().into().

§Examples

We create an Anybuf instance and then convert it into Bytes, which just serves as an example for a type that can be created from Vec<u8>.

use bytes::Bytes;

// variable type known
let serialized: Bytes = Anybuf::new()
    .append_repeated_int64(4, &[-30, 0, 17])
    .into_x();

// explicit type parameter
let serialized = Anybuf::new()
    .append_repeated_int64(4, &[-30, 0, 17])
    .into_x::<Bytes>();

Trait Implementations§

Source§

impl AsRef<Anybuf> for Anybuf

Source§

fn as_ref(&self) -> &Anybuf

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Anybuf

Source§

fn clone(&self) -> Anybuf

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
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 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 = 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 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.