Skip to main content

Format

Enum Format 

Source
#[non_exhaustive]
pub enum Format { Packed, Storage, Wire, Descriptive, Json, }
Available on crate feature api only.
Expand description

The serialization format used for message bodies.

The format is negotiated per connection, see the [negotiation protocol] for the details of how. Message headers are never affected by this and always use a fixed envelope, which is what makes negotiation possible in the first place.

The variants are ordered from least to most capable. Each capability that is dropped makes the encoding more compact:

reordermissingunknownself
Packed
Storage
Wire
Descriptive
Json
  • reorder determines whether fields may be reordered in the model.
  • missing determines whether decoding tolerates missing fields, which is what allows new optional fields to be added.
  • unknown determines whether decoding can skip fields it does not know about. A format which can do this is fully upgrade safe, since an old peer can talk to a new one.
  • self determines whether the format is self-descriptive, so that the data can be decoded without the model.

§Examples

use musli_web::api::Format;

assert_eq!(Format::default(), Format::Wire);
assert!(Format::Wire.is_upgrade_safe());
assert!(!Format::Storage.is_upgrade_safe());
assert!(Format::Json.is_human_readable());

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Packed

The musli::packed format.

The most compact format, but it requires that both peers use exactly the same model. Suitable when client and server are deployed together.

§

Storage

The musli::storage format.

Tolerates missing fields, but cannot skip fields it does not know about.

§

Wire

The musli::wire format.

Fully upgrade safe, so peers built against different versions of the model can talk to each other. This is the default.

§

Descriptive

The musli::descriptive format.

Fully upgrade safe and self-descriptive, at the cost of a larger payload.

§

Json

The musli::json format.

Human readable, which is useful when the traffic has to be inspected by hand. Encoded using the Text mode so that fields are keyed by name.

Implementations§

Source§

impl Format

Source

pub const DEFAULT: Self = Self::Wire

The default format, which is Format::Wire.

This is used by a client which has not picked a format, and by a server for a connection which has not negotiated one.

§Examples
use musli_web::api::Format;

assert_eq!(Format::DEFAULT, Format::Wire);
Source

pub const ALL: &'static [Format]

Every format in order of increasing capability.

Note that this includes formats which the crate might not have been built with support for, see Format::is_supported.

§Examples
use musli_web::api::Format;

assert!(Format::ALL.contains(&Format::Json));
Source

pub const fn to_u8(self) -> u8

Get the stable identifier used for this format on the wire.

Zero is never used, so it is available to indicate an absent format.

§Examples
use musli_web::api::Format;

assert_eq!(Format::Wire.to_u8(), 3);
assert_eq!(Format::from_u8(3), Some(Format::Wire));
Source

pub const fn from_u8(id: u8) -> Option<Self>

Construct a format from the stable identifier used on the wire.

Returns None if the identifier is not known, which is how a peer built against an older version of this crate reports a format it has never heard of.

§Examples
use musli_web::api::Format;

assert_eq!(Format::from_u8(1), Some(Format::Packed));
assert_eq!(Format::from_u8(0), None);
assert_eq!(Format::from_u8(200), None);
Source

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

The name of the format.

§Examples
use musli_web::api::Format;

assert_eq!(Format::Wire.name(), "wire");
Source

pub const fn is_upgrade_safe(self) -> bool

Test if the format can skip over unknown fields, making it fully upgrade safe.

§Examples
use musli_web::api::Format;

assert!(Format::Wire.is_upgrade_safe());
assert!(!Format::Packed.is_upgrade_safe());
Source

pub const fn is_self_descriptive(self) -> bool

Test if the format is self-descriptive, so that data can be decoded without access to the model.

§Examples
use musli_web::api::Format;

assert!(Format::Descriptive.is_self_descriptive());
assert!(!Format::Wire.is_self_descriptive());
Source

pub const fn is_human_readable(self) -> bool

Test if the format produces output which is meant to be read by humans.

§Examples
use musli_web::api::Format;

assert!(Format::Json.is_human_readable());
assert!(!Format::Wire.is_human_readable());
Source§

impl Format

Source

pub const fn is_supported(self) -> bool

Test if this build of the crate has support for the format.

Formats are gated behind features, so a peer might genuinely be unable to speak a format which the other side asks for. This is what the negotiation protocol uses to decide whether a request can be honored.

§Examples
use musli_web::api::Format;

// The default format is always available.
assert!(Format::DEFAULT.is_supported());
Source

pub fn supported() -> impl Iterator<Item = Format>

Iterate over every format this build of the crate supports.

§Examples
use musli_web::api::Format;

assert!(Format::supported().any(|f| f == Format::DEFAULT));

Trait Implementations§

Source§

impl Clone for Format

Source§

fn clone(&self) -> Format

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 Format

Source§

impl Debug for Format

Source§

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

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

impl Default for Format

Source§

fn default() -> Self

Construct the default format, which is Format::DEFAULT.

§Examples
use musli_web::api::Format;

assert_eq!(Format::default(), Format::Wire);
Source§

impl Display for Format

Source§

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

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

impl Eq for Format

Source§

impl Hash for Format

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Format

Source§

fn cmp(&self, other: &Format) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Format

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Format

Source§

fn partial_cmp(&self, other: &Format) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Format

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<Token, Builder, How> AllPropsFor<Builder, How> for Token
where Builder: Buildable<Token>, <Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,

Source§

impl<Token, Builder, How> AllPropsFor<Builder, How> for Token
where Builder: Buildable<Token>, <Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> HasAllProps<(), T> for T

Source§

impl<T> HasAllProps<(), T> for T

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoPropValue<Option<T>> for T

Source§

fn into_prop_value(self) -> Option<T>

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<Option<T>> for T

Source§

fn into_prop_value(self) -> Option<T>

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<T> for T

Source§

fn into_prop_value(self) -> T

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<T> for T

Source§

fn into_prop_value(self) -> T

Convert self to a value of a Properties struct.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more