pub struct Anybuf { /* private fields */ }Expand description
A minmal protobuf encoder.
Implementations§
Source§impl Anybuf
impl Anybuf
Sourcepub fn new() -> Self
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();Sourcepub fn append_bytes(self, field_number: u32, data: impl AsRef<[u8]>) -> Self
pub fn append_bytes(self, field_number: u32, data: impl AsRef<[u8]>) -> Self
Appends a bytes field with the given field number.
Sourcepub fn append_string(self, field_number: u32, data: impl AsRef<str>) -> Self
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();Sourcepub fn append_uint64(self, field_number: u32, value: u64) -> Self
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();Sourcepub fn append_uint32(self, field_number: u32, value: u32) -> Self
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();Sourcepub fn append_bool(self, field_number: u32, value: bool) -> Self
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();Sourcepub fn append_sint64(self, field_number: u32, value: i64) -> Self
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();Sourcepub fn append_sint32(self, field_number: u32, value: i32) -> Self
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();Sourcepub fn append_int64(self, field_number: u32, value: i64) -> Self
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();Sourcepub fn append_int32(self, field_number: u32, value: i32) -> Self
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();Sourcepub fn append_message(self, field_number: u32, value: &Anybuf) -> Self
pub fn append_message(self, field_number: u32, value: &Anybuf) -> Self
Appends a nested protobuf message with the given field number.
Sourcepub fn append_repeated_uint32(self, field_number: u32, data: &[u32]) -> Self
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();Sourcepub fn append_repeated_uint64(self, field_number: u32, data: &[u64]) -> Self
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();Sourcepub fn append_repeated_sint32(self, field_number: u32, data: &[i32]) -> Self
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();Sourcepub fn append_repeated_sint64(self, field_number: u32, data: &[i64]) -> Self
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();Sourcepub fn append_repeated_bool(self, field_number: u32, data: &[bool]) -> Self
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();Sourcepub fn append_repeated_int32(self, field_number: u32, data: &[i32]) -> Self
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();Sourcepub fn append_repeated_int64(self, field_number: u32, data: &[i64]) -> Self
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();Sourcepub fn append_repeated_string<S: AsRef<str>>(
self,
field_number: u32,
data: &[S],
) -> Self
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();Sourcepub fn append_repeated_bytes<B: AsRef<[u8]>>(
self,
field_number: u32,
data: &[B],
) -> Self
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();Sourcepub fn append_repeated_message<M: AsRef<Anybuf>>(
self,
field_number: u32,
messages: &[M],
) -> Self
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();Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
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.
Sourcepub fn into_vec(self) -> Vec<u8> ⓘ
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.
Sourcepub fn into_x<T: From<Vec<u8>>>(self) -> T
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>();