exonum_proto

Struct ProtobufBase64

Source
pub struct ProtobufBase64(/* private fields */);
Expand description

Marker type for use with #[serde(with)], which provides Protobuf-compatible base64 encoding and decoding. For now, works only on Vec<u8> fields.

The encoder uses the standard base64 alphabet (i.e., 0..9A..Za..z+/) with no padding. The decoder accepts any of the 4 possible combinations: the standard or the URL-safe alphabet with or without padding.

If the (de)serializer is not human-readable (e.g., CBOR or bincode), the bytes will be (de)serialized without base64 transform, directly as a byte slice.

§Examples

use exonum_proto::ProtobufBase64;

#[derive(Serialize, Deserialize)]
struct Test {
    /// Corresponds to a `bytes buffer = ...` field in Protobuf.
    #[serde(with = "ProtobufBase64")]
    buffer: Vec<u8>,
    // other fields...
}

let test = Test {
    buffer: b"Hello!".to_vec(),
    // ...
};
let obj = serde_json::to_value(&test)?;
assert_eq!(obj, json!({ "buffer": "SGVsbG8h", /* ... */ }));

Implementations§

Source§

impl ProtobufBase64

Source

pub fn serialize<S, T>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer, T: AsRef<[u8]> + ?Sized,

Serializes the provided bytes with the serializer.

Source

pub fn decode(value: &str) -> Result<Vec<u8>, DecodeError>

Decodes bytes from any of four base64 variations supported as per Protobuf spec (standard or URL-safe alphabet, with or without padding).

Source

pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where D: Deserializer<'de>,

Deserializes Vec<u8> using the provided serializer.

Trait Implementations§

Source§

impl Debug for ProtobufBase64

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> 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, 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.