[][src]Macro yew::binary_format

macro_rules! binary_format {
    ($type:ident based on $format:ident) => { ... };
    ($type:ident, $into:path, $from:path) => { ... };
}

This macro is used for a format that can be encoded as Binary. It is used in conjunction with a type definition for a tuple struct with one (publicly accessible) element of a generic type. Not all types that can be encoded as Binary can be encoded as Text. As such, this macro should be paired with the text_format macro where such an encoding works (e.g., JSON), or with the text_format_is_an_error macro for binary-only formats (e.g., MsgPack).

Rely on serde's to_vec and from_vec

The simplest form of this macro relegates all the work to serde's to_vec function for serialization and serde's from_vec for deseriaization.

Examples

Binary that is also Text

use yew::{binary_format, text_format};

pub struct Json<T>(pub T);

text_format!(Json based on serde_json);
binary_format!(Json based on serde_json);

Binary only

  use rmp_serde;
  use yew::{binary_format, text_format_is_an_error};

  pub struct MsgPack<T>(pub T);

  binary_format!(MsgPack based on rmp_serde);
  text_format_is_an_error!(MsgPack);

Supply serialization and deserialization functions

In addition to the "based on" form of this macro demonstrated above, you can use the three parameter second form to supply non-standard (i.e., alternatives to serde's to_vec and/or from_slice) helpers as the second and third parameters.

Example

  use bincode;
  use yew::{binary_format, text_format_is_an_error};

  pub struct Bincode<T>(pub T);

  binary_format!(Bincode, bincode::serialize, bincode::deserialize);
  text_format_is_an_error!(Bincode);