[−][src]Struct kserd::encode::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
fn serialize_bool(self, v: bool) -> Result<Kserd<'static>, Error>
[src]
fn serialize_i8(self, v: i8) -> Result<Kserd<'static>, Error>
[src]
fn serialize_i16(self, v: i16) -> Result<Kserd<'static>, Error>
[src]
fn serialize_i32(self, v: i32) -> Result<Kserd<'static>, Error>
[src]
fn serialize_i64(self, v: i64) -> Result<Kserd<'static>, Error>
[src]
fn serialize_i128(self, v: i128) -> Result<Kserd<'static>, Error>
[src]
fn serialize_u8(self, v: u8) -> Result<Kserd<'static>, Error>
[src]
fn serialize_u16(self, v: u16) -> Result<Kserd<'static>, Error>
[src]
fn serialize_u32(self, v: u32) -> Result<Kserd<'static>, Error>
[src]
fn serialize_u64(self, v: u64) -> Result<Kserd<'static>, Error>
[src]
fn serialize_u128(self, v: u128) -> Result<Kserd<'static>, Error>
[src]
fn serialize_f32(self, v: f32) -> Result<Kserd<'static>, Error>
[src]
fn serialize_f64(self, v: f64) -> Result<Kserd<'static>, Error>
[src]
fn serialize_char(self, v: char) -> Result<Kserd<'static>, Error>
[src]
fn serialize_str(self, v: &str) -> Result<Kserd<'static>, Error>
[src]
fn serialize_bytes(self, v: &[u8]) -> Result<Kserd<'static>, Error>
[src]
fn serialize_none(self) -> Result<Kserd<'static>, Error>
[src]
fn serialize_some<T: ?Sized + Serialize>(
self,
v: &T
) -> Result<Kserd<'static>, Error>
[src]
self,
v: &T
) -> Result<Kserd<'static>, Error>
fn serialize_unit(self) -> Result<Kserd<'static>, Error>
[src]
fn serialize_unit_struct(
self,
name: &'static str
) -> Result<Kserd<'static>, Error>
[src]
self,
name: &'static str
) -> Result<Kserd<'static>, Error>
fn serialize_unit_variant(
self,
_: &'static str,
_: u32,
variant: &'static str
) -> Result<Kserd<'static>, Error>
[src]
self,
_: &'static str,
_: u32,
variant: &'static str
) -> Result<Kserd<'static>, Error>
fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
name: &'static str,
value: &T
) -> Result<Kserd<'static>, Error>
[src]
self,
name: &'static str,
value: &T
) -> Result<Kserd<'static>, Error>
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_: &'static str,
_: u32,
variant: &'static str,
value: &T
) -> Result<Kserd<'static>, Error>
[src]
self,
_: &'static str,
_: u32,
variant: &'static str,
value: &T
) -> Result<Kserd<'static>, Error>
fn serialize_seq(self, len: Option<usize>) -> Result<SeqLike, Error>
[src]
fn serialize_tuple(self, len: usize) -> Result<TupleLike, Error>
[src]
fn serialize_tuple_struct(
self,
name: &'static str,
len: usize
) -> Result<TupleLike, Error>
[src]
self,
name: &'static str,
len: usize
) -> Result<TupleLike, Error>
fn serialize_tuple_variant(
self,
_: &'static str,
_: u32,
variant: &'static str,
len: usize
) -> Result<TupleLike, Error>
[src]
self,
_: &'static str,
_: u32,
variant: &'static str,
len: usize
) -> Result<TupleLike, Error>
fn serialize_map(self, _len: Option<usize>) -> Result<MapLike, Error>
[src]
fn serialize_struct(
self,
name: &'static str,
_len: usize
) -> Result<CntrLike, Error>
[src]
self,
name: &'static str,
_len: usize
) -> Result<CntrLike, Error>
fn serialize_struct_variant(
self,
_: &'static str,
_: u32,
variant: &'static str,
_: usize
) -> Result<CntrLike, Error>
[src]
self,
_: &'static str,
_: u32,
variant: &'static str,
_: usize
) -> Result<CntrLike, Error>
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error> where
T: Display,
[src]
T: Display,
fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error> where
I: IntoIterator,
<I as IntoIterator>::Item: Serialize,
[src]
I: IntoIterator,
<I as IntoIterator>::Item: Serialize,
fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error> where
I: IntoIterator<Item = (K, V)>,
K: Serialize,
V: Serialize,
[src]
I: IntoIterator<Item = (K, V)>,
K: Serialize,
V: Serialize,
fn is_human_readable(&self) -> bool
[src]
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,