Struct msgpack_schema::value::Str[][src]

pub struct Str(pub Vec<u8>);
Expand description

String objects of MessagePack are essentially byte arrays type that may contain any bytes.

String type vs Binary type

MessagePack has a complicated history about its distinction between string type and binary type.

While an earlier version of MessagePack had only one type for encoding a binary data, namely the raw type, it was later divided into two distinct types for supporting use cases in dynamically-typed languages. 1 The string type, one of the newly added types, is essentially what was originally called the raw type. Because of this origin, despite its name, string objects of MessagePack can contain not just valid UTF-8 sequences but also any byte sequences. And encoding non-UTF-8 byte sequences as a string object is a perfectly valid and expected usage by the spec authors.

So which to use in encoding my binary data?

When you decide to implement a custom serializer/deserializer for your own binary type, it is recommended to use string type instead of binary type for its encoding scheme for the following reasons.

  • It just saves some memory. If your byte array is less than 32 byte length, using string type instead of byte array saves one byte per object.
  • The disiction only matters when not using a data schema. Because this crate offers a statically-typed data schema, and we know how to decode data into a Rust object at compile time, distinction of these types in the input binary data is almost useless,

Although we strongly recommend you to use string types rather than binary types, this crate does not force you to do so. The functions and trait implementations provided by this crate are all taking a neutral stand.

Implementations

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.