[][src]Struct kserd::encode::Encoder

pub struct Encoder;

Encoder to pass to Serialize::serialize to encode a type into a Kserd.

There is no data associated with the Encoder, instead it is used to implement serde's Serializer trait. It can be used to encode a data type that implements Serialize like so:

use kserd::encode::Encoder;
use kserd::encode::Serialize;

let data = ("Hello!", 3.14);
let expected = Kserd::new(
    Value::Tuple(vec![
        Kserd::new_str("Hello!"),
        Kserd::new_num(3.14),
    ])
);

let kserd = data.serialize(Encoder);
assert_eq!(kserd, Ok(expected));

It is important to note that the signature of the Serialize trait does not allow propogation of borrowed data. Specifically, when implementing a Serializer the values are either passed through copied, or as ephemeral borrows as in the case of strings and byte arrays. This limits the Kserd to be an owned copy, with a 'static lifetime parameter. This has implications when decoding as described in the Decoder documentation. An alternate trait ToKserd is defined that consumes the data structure and allows borrowed data to be propogated to the Kserd.

There is also a helper function Kserd::enc that can encode without importing the Encoder.

let expected = Kserd::new(
    Value::Tuple(vec![
        Kserd::new_str("Hello!"),
        Kserd::new_num(3.14),
    ])
);

let kserd = Kserd::enc(&("Hello!", 3.14));
assert_eq!(kserd, Ok(expected));

Trait Implementations

impl Serializer for Encoder[src]

type Ok = Kserd<'static>

The output type produced by this Serializer during successful serialization. Most serializers that produce text or binary output should set Ok = () and serialize into an [io::Write] or buffer contained within the Serializer instance. Serializers that build in-memory data structures may be simplified by using Ok to propagate the data structure around. Read more

type Error = Error

The error type when some error occurs during serialization.

type SerializeSeq = SeqLike

Type returned from [serialize_seq] for serializing the content of the sequence. Read more

type SerializeTuple = TupleLike

Type returned from [serialize_tuple] for serializing the content of the tuple. Read more

type SerializeTupleStruct = TupleLike

Type returned from [serialize_tuple_struct] for serializing the content of the tuple struct. Read more

type SerializeTupleVariant = TupleLike

Type returned from [serialize_tuple_variant] for serializing the content of the tuple variant. Read more

type SerializeMap = MapLike

Type returned from [serialize_map] for serializing the content of the map. Read more

type SerializeStruct = CntrLike

Type returned from [serialize_struct] for serializing the content of the struct. Read more

type SerializeStructVariant = CntrLike

Type returned from [serialize_struct_variant] for serializing the content of the struct variant. Read more

Auto Trait Implementations

impl RefUnwindSafe for Encoder

impl Send for Encoder

impl Sync for Encoder

impl Unpin for Encoder

impl UnwindSafe for Encoder

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.