Struct Str

Source
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.

Tuple Fields§

§0: Vec<u8>

Implementations§

Source§

impl Str

Source

pub fn into_bytes(self) -> Vec<u8>

Source

pub fn as_bytes(&self) -> &[u8]

Trait Implementations§

Source§

impl Arbitrary for Str

Source§

type Parameters = <Vec<u8> as Arbitrary>::Parameters

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
Source§

type Strategy = Map<<Vec<u8> as Arbitrary>::Strategy, fn(Vec<u8>) -> Str>

The type of Strategy used to generate values of type Self.
Source§

fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
Source§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
Source§

impl Clone for Str

Source§

fn clone(&self) -> Str

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Str

Source§

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

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

impl From<Str> for Value

Source§

fn from(v: Str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Str

Source§

fn from(x: String) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Str

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Str

Source§

impl StructuralPartialEq for Str

Auto Trait Implementations§

§

impl Freeze for Str

§

impl RefUnwindSafe for Str

§

impl Send for Str

§

impl Sync for Str

§

impl Unpin for Str

§

impl UnwindSafe for Str

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<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 = 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V