Skip to main content

BytesFormat

Struct BytesFormat 

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

How bytes are represented in formats without native bytes.

This is used in three places:

  • The serializers of formats without native bytes (JSON and TOML) can be configured with a format. It’s used for all bytes that do not request a format. The default is BytesFormat::BASE64.
  • Bytes can carry a format as fallback (see Bytes::fallback) which takes precedence over the configuration of the serializer. The BytesFallback adapter does this.
  • The types that expect bytes decode strings with the format placed into the State (see set). The deserializers of formats without native bytes can be configured to do this, otherwise lenient base64 is used. Strings are decoded as base64 for BytesFormat::SEQ.
use deser::adapters::Base64UrlNoPad;
use deser::BytesFormat;

const URL_SAFE: BytesFormat = BytesFormat::encoded::<Base64UrlNoPad>();
assert_eq!(URL_SAFE.encode(b"\xfb\xff").as_deref(), Some("-_8"));
// decoding is lenient
assert_eq!(URL_SAFE.decode("+/8=").unwrap(), b"\xfb\xff");
assert_eq!(BytesFormat::SEQ.encode(b"\x01\xff"), None);

Two formats are equal if they are both sequences or encodings with the same name.

Implementations§

Source§

impl BytesFormat

Source

pub const BASE64: BytesFormat

Bytes are strings in base64 with the standard alphabet and padding.

This is the default.

Source

pub const SEQ: BytesFormat

Bytes are sequences of integers.

This is how serde_json represents bytes.

Source

pub const fn encoded<E: BytesEncoding>() -> BytesFormat

Bytes are strings in the given encoding.

Source

pub fn of(state: &State) -> BytesFormat

Returns the format the types that expect bytes decode strings with.

This is BytesFormat::BASE64 unless the deserializer of the format set a different one.

Source

pub fn set(self, state: &mut State)

Sets the format the types that expect bytes decode strings with.

Source

pub fn name(&self) -> &'static str

Returns the name of the format.

This is the name of the encoding or seq.

Source

pub fn is_seq(&self) -> bool

Returns true if bytes are sequences of integers.

Source

pub fn encode(&self, bytes: &[u8]) -> Option<String>

Encodes bytes as string.

Returns None if bytes are sequences of integers.

Source

pub fn decode(&self, s: &str) -> Result<Vec<u8>, Error>

Decodes bytes from a string.

For sequences of integers the string is decoded as base64.

Trait Implementations§

Source§

impl Clone for BytesFormat

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for BytesFormat

Source§

impl Debug for BytesFormat

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for BytesFormat

Source§

fn default() -> BytesFormat

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

impl Eq for BytesFormat

Source§

impl PartialEq for BytesFormat

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.