[][src]Struct kserd::encode::Decoder

pub struct Decoder<'de>(pub Kserd<'de>);

Decoder to pass to Deserialize::deserialize to decode a Kserd into a type.

Decoder consumes the Kserd structure. It is important to understand how this interacts with the Deserialize implementor. Because Kserd tries to reduce copying, using clone-on-write pointers for strings and byte arrays, there are extra cases to be handled to achieve the minimal amount of copying data. The Decoder was designed to leverage movement of data as much as possible, however this presents a unique case when decoding back to a type that contains borrowed data.

The table below captures different memory operations using Decoder:

Kserd ownershipRust typeData transfer
OwnedOwnedmove
BorrowedOwnedcopy
BorrowedBorrowedmove (ptr)
OwnedBorrowedunique

As the Kserd is consumed, owned data can be directly moved, whilst borrowed data can be cloned if needing to be come owned, or the pointer can be moved. The only case where this breaks is if the Kserd is owned and an attempt is made to decode it back into borrowed data. The failing occurs because the owned data will be dropped after the deserialize call.

If borrowed data is required, the solution is to decode a Kserd which itself is a reference to the owned Kserd. The method .mk_brw() does this. There is small overhead as nested structures have to be reallocated, but strings and byte arrays are made to point to the original Kserd structure. The 'borrowed' Kserd does not really differ that much, expect that it is gauranteed to not contain owned data. This makes the fourth case not possible and the decoding to succeed.

It is worth noting that it may increase the amount of copying occurring, as previously owned data is now borrowed. It is therefore recommended to not borrow the Kserd unless necessary.

Kserd::decode can be used for conveniance.

Examples

Kserds containing borrowed data can decode into borrowed or owned types.

use kserd::encode::Decoder;
use serde::Deserialize;

let string = String::from("Hello, world!");

// decode into borrowed type
let kserd = Kserd::new_str(&string);
let decoder = Decoder(kserd);
let s = <&str>::deserialize(decoder).unwrap();
assert_eq!(s, "Hello, world!");

// decode into owned type
let kserd = Kserd::new_str(&string);
let decoder = Decoder(kserd);
let s = <String>::deserialize(decoder).unwrap();
assert_eq!(s, string);

Kserds which own their data can decode into owned types, but error on borrowed types.

use kserd::encode::Decoder;
use serde::Deserialize;

// decode into owned type works
let kserd = Kserd::new_string(String::from("Hello, world!"));
let decoder = Decoder(kserd);
let s = <String>::deserialize(decoder).unwrap();
assert_eq!(s, "Hello, world!".to_string());

// decode into borrowed type fails
let kserd = Kserd::new_string(String::from("Hello, world!"));
let decoder = Decoder(kserd);
let r = <&str>::deserialize(decoder);
assert_eq!(r.is_err(), true);

Borrowing the Kserd beforehand extends the lifetime.

use kserd::encode::Decoder;
use serde::Deserialize;

let kserd = Kserd::new_string(String::from("Hello, world!"));
let decoder = Decoder(kserd.mk_brw());
let s = <&str>::deserialize(decoder).unwrap();
assert_eq!(s, "Hello, world!");

Trait Implementations

impl<'de> Deserializer<'de> for Decoder<'de>[src]

type Error = Error

The error type that can be returned if some error occurs during deserialization. Read more

impl<'de> EnumAccess<'de> for Decoder<'de>[src]

type Error = Error

The error type that can be returned if some error occurs during deserialization. Read more

type Variant = Decoder<'de>

The Visitor that will be used to deserialize the content of the enum variant. Read more

impl<'de> VariantAccess<'de> for Decoder<'de>[src]

type Error = Error

The error type that can be returned if some error occurs during deserialization. Must match the error type of our EnumAccess. Read more

Auto Trait Implementations

impl<'de> RefUnwindSafe for Decoder<'de>

impl<'de> Send for Decoder<'de>

impl<'de> Sync for Decoder<'de>

impl<'de> Unpin for Decoder<'de>

impl<'de> UnwindSafe for Decoder<'de>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.