pub struct BytesFormat(/* private fields */);Expand description
How bytes are represented in formats without native bytes.
It’s an extension value, usually configured in the
Context (or in the State, see
set). It’s used in three places:
- The serializers of formats without native bytes (like JSON and TOML)
write all bytes that do not request a format in it. The default is
BytesFormat::BASE64. - Bytes can carry a format as fallback (see
Bytes::fallback) which takes precedence over the format of the context. TheBytesFallbackadapter does this. - The types that expect bytes decode strings with it. Without a format
lenient base64 is used. Strings are decoded as base64 for
BytesFormat::SEQ.
use deser::adapters::Base64Url;
use deser::{BytesFormat, Context};
// writes and reads URL-safe base64 in formats without native bytes
let context = Context::with(BytesFormat::encoded::<Base64Url>());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
impl BytesFormat
Sourcepub const BASE64: BytesFormat
pub const BASE64: BytesFormat
Bytes are strings in base64 with the standard alphabet and padding.
This is the default.
Sourcepub const SEQ: BytesFormat
pub const SEQ: BytesFormat
Bytes are sequences of integers.
This is how serde_json represents bytes.
Sourcepub const fn encoded<E: BytesEncoding>() -> BytesFormat
pub const fn encoded<E: BytesEncoding>() -> BytesFormat
Bytes are strings in the given encoding.
Sourcepub fn of(state: &State) -> BytesFormat
pub fn of(state: &State) -> BytesFormat
Returns the format of a serialization or deserialization.
This is BytesFormat::BASE64 unless the state or the context has a
different one.
Sourcepub fn set(self, state: &mut State)
pub fn set(self, state: &mut State)
Sets the format the types that expect bytes decode strings with.
Sourcepub fn name(&self) -> &'static str
pub fn name(&self) -> &'static str
Returns the name of the format.
This is the name of the encoding or seq.
Trait Implementations§
Source§impl Clone for BytesFormat
impl Clone for BytesFormat
impl Copy for BytesFormat
Source§impl Debug for BytesFormat
impl Debug for BytesFormat
Source§impl Default for BytesFormat
impl Default for BytesFormat
Source§fn default() -> BytesFormat
fn default() -> BytesFormat
Returns the “default value” for a type. Read more
impl Eq for BytesFormat
Auto Trait Implementations§
impl Freeze for BytesFormat
impl RefUnwindSafe for BytesFormat
impl Send for BytesFormat
impl Sync for BytesFormat
impl Unpin for BytesFormat
impl UnsafeUnpin for BytesFormat
impl UnwindSafe for BytesFormat
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.