pub struct Serializer<W>where
W: Write,{ /* private fields */ }Expand description
A serializer for a byte format that preserves lexicographic sort order.
The byte format is designed with a few goals:
- Order must be preserved
- Serialized representations should be as compact as possible
- Type information is not serialized with values
§Supported Data Types
§Unsigned Integers
u8, u16, u32, and u64 are serialized into 1, 2, 4, and 8 bytes of output, respectively.
Order is preserved by encoding the bytes in big-endian (most-significant bytes first) format.
usize is always serialized as if it were u64.
The Serializer also supports variable-length serialization of unsigned integers via the
serialize_var_u64 method. Smaller magnitude values (closer to 0) will encode into fewer
bytes.
§Signed Integers
i8, i16, i32, and i64 are encoded into 1, 2, 4, and 8 bytes of output, respectively.
Order is preserved by taking the bitwise complement of the value, and encoding the resulting
bytes in big-endian format. isize is always serialized as if it were i64.
The Serializer also supports variable-length serialization of signed integers via the
serialize_var_i64 method. Smaller magnitude values (closer to 0) will encode into fewer
bytes.
§Floating Point Numbers
f32 and f64 are serialized into 4 and 8 bytes of output, respectively. Order is preserved
by encoding the value, or the bitwise complement of the value if negative, into bytes in
big-endian format. NAN values will sort after all other values. In general, it is unwise to
use IEEE 754 floating point values in keys, because rounding errors are pervasive. It is
typically hard or impossible to use an approximate ‘epsilon’ approach when using keys for
lookup.
§Characters
Characters are serialized into between 1 and 4 bytes of output. The resulting length is
equivalent to the result of char::len_utf8.
§Booleans
Booleans are serialized into a single byte of output. false values will sort before true
values.
§Options
An optional wrapper type adds a 1 byte overhead to the wrapped data type. None values will
sort before Some values.
§Structs, Tuples and Fixed-Size Arrays
Structs and tuples are serialized by serializing their consituent fields in order with no prefix, suffix, or padding bytes.
§Enums
Enums are encoded with a u32 variant index tag, plus the consituent fields in the case of an
enum-struct.
§Sequences, Strings and Maps
Sequences are ordered from the most significant to the least. Strings are serialized into their natural UTF8 representation.
The ordering of sequential elements follows the Ord implementation of slice, that is, from
left to write when viewing a Vec printed via the {:?} formatter.
The caveat with these types is that their length must be known before deserialization. This is
because the length is not serialized prior to the elements in order to preserve ordering and
there is no trivial way to tokenise between sequential elements that 1. does not corrupt
ordering and 2. may not confuse tokenisation with following elements of a different type during
tuple or struct deserialization. Thus, when deserializing sequences, strings and maps, the
process will only be considered complete once the inner reader produces an EOF character.
Implementations§
Source§impl<W> Serializer<W>where
W: Write,
impl<W> Serializer<W>where
W: Write,
Sourcepub fn new(writer: W) -> Serializer<W>
pub fn new(writer: W) -> Serializer<W>
Creates a new ordered bytes encoder whose output will be written to the provided writer.
Sourcepub fn serialize_var_u64(&mut self, val: u64) -> Result<()>
pub fn serialize_var_u64(&mut self, val: u64) -> Result<()>
Encode a u64 into a variable number of bytes.
The variable-length encoding scheme uses between 1 and 9 bytes depending on the value.
Smaller magnitude (closer to 0) u64s will encode to fewer bytes.
§Encoding
The encoding uses the first 4 bits to store the number of trailing bytes, between 0 and 8. Subsequent bits are the input value in big-endian format with leading 0 bytes removed.
§Encoded Size
| range | size (bytes) |
|---|---|
| [0, 24) | 1 |
| [24, 212) | 2 |
| [212, 220) | 3 |
| [220, 228) | 4 |
| [228, 236) | 5 |
| [236, 244) | 6 |
| [244, 252) | 7 |
| [252, 260) | 8 |
| [260, 264) | 9 |
Sourcepub fn serialize_var_i64(&mut self, v: i64) -> Result<()>
pub fn serialize_var_i64(&mut self, v: i64) -> Result<()>
Encode an i64 into a variable number of bytes.
The variable-length encoding scheme uses between 1 and 9 bytes depending on the value.
Smaller magnitude (closer to 0) i64s will encode to fewer bytes.
§Encoding
The encoding uses the first bit to encode the sign: 0 for negative values and 1 for
positive values. The following 4 bits store the number of trailing bytes, between 0 and 8.
Subsequent bits are the absolute value of the input value in big-endian format with leading
0 bytes removed. If the original value was negative, than 1 is subtracted from the absolute
value before encoding. Finally, if the value is negative, all bits except the sign bit are
flipped (1s become 0s and 0s become 1s).
§Encoded Size
| negative range | positive range | size (bytes) |
|---|---|---|
| [-23, 0) | [0, 23) | 1 |
| [-211, -23) | [23, 211) | 2 |
| [-219, -211) | [211, 219) | 3 |
| [-227, -219) | [219, 227) | 4 |
| [-235, -227) | [227, 235) | 5 |
| [-243, -235) | [235, 243) | 6 |
| [-251, -243) | [243, 251) | 7 |
| [-259, -251) | [251, 259) | 8 |
| [-263, -259) | [259, 263) | 9 |
Trait Implementations§
Source§impl<W> Debug for Serializer<W>
impl<W> Debug for Serializer<W>
Source§impl<'a, W> SerializeMap for &'a mut Serializer<W>where
W: Write,
impl<'a, W> SerializeMap for &'a mut Serializer<W>where
W: Write,
Source§impl<'a, W> SerializeSeq for &'a mut Serializer<W>where
W: Write,
impl<'a, W> SerializeSeq for &'a mut Serializer<W>where
W: Write,
Source§impl<'a, W> SerializeStruct for &'a mut Serializer<W>where
W: Write,
impl<'a, W> SerializeStruct for &'a mut Serializer<W>where
W: Write,
Source§impl<'a, W> SerializeStructVariant for &'a mut Serializer<W>where
W: Write,
impl<'a, W> SerializeStructVariant for &'a mut Serializer<W>where
W: Write,
Source§impl<'a, W> SerializeTuple for &'a mut Serializer<W>where
W: Write,
impl<'a, W> SerializeTuple for &'a mut Serializer<W>where
W: Write,
Source§impl<'a, W> SerializeTupleStruct for &'a mut Serializer<W>where
W: Write,
impl<'a, W> SerializeTupleStruct for &'a mut Serializer<W>where
W: Write,
Source§impl<'a, W> SerializeTupleVariant for &'a mut Serializer<W>where
W: Write,
impl<'a, W> SerializeTupleVariant for &'a mut Serializer<W>where
W: Write,
Source§impl<'a, W> Serializer for &'a mut Serializer<W>where
W: Write,
impl<'a, W> Serializer for &'a mut Serializer<W>where
W: Write,
Source§fn serialize_f32(self, v: f32) -> Result<()>
fn serialize_f32(self, v: f32) -> Result<()>
Encode an f32 into sortable bytes.
NaNs will sort greater than positive infinity. -0.0 will sort directly before +0.0.
See Hacker’s Delight 2nd Edition Section 17-3.
Source§fn serialize_f64(self, v: f64) -> Result<()>
fn serialize_f64(self, v: f64) -> Result<()>
Encode an f64 into sortable bytes.
NaNs will sort greater than positive infinity. -0.0 will sort directly before +0.0.
See Hacker’s Delight 2nd Edition Section 17-3.
Source§type Ok = ()
type Ok = ()
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.Source§type SerializeSeq = &'a mut Serializer<W>
type SerializeSeq = &'a mut Serializer<W>
serialize_seq for serializing the content of the
sequence.Source§type SerializeTuple = &'a mut Serializer<W>
type SerializeTuple = &'a mut Serializer<W>
serialize_tuple for serializing the content of
the tuple.Source§type SerializeTupleStruct = &'a mut Serializer<W>
type SerializeTupleStruct = &'a mut Serializer<W>
serialize_tuple_struct for serializing the
content of the tuple struct.Source§type SerializeTupleVariant = &'a mut Serializer<W>
type SerializeTupleVariant = &'a mut Serializer<W>
serialize_tuple_variant for serializing the
content of the tuple variant.Source§type SerializeMap = &'a mut Serializer<W>
type SerializeMap = &'a mut Serializer<W>
serialize_map for serializing the content of the
map.Source§type SerializeStruct = &'a mut Serializer<W>
type SerializeStruct = &'a mut Serializer<W>
serialize_struct for serializing the content of
the struct.Source§type SerializeStructVariant = &'a mut Serializer<W>
type SerializeStructVariant = &'a mut Serializer<W>
serialize_struct_variant for serializing the
content of the struct variant.Source§fn serialize_bytes(self, v: &[u8]) -> Result<()>
fn serialize_bytes(self, v: &[u8]) -> Result<()>
Source§fn serialize_unit_variant(
self,
_name: &'static str,
variant_index: u32,
_variant: &'static str,
) -> Result<()>
fn serialize_unit_variant( self, _name: &'static str, variant_index: u32, _variant: &'static str, ) -> Result<()>
Source§fn serialize_newtype_struct<T>(
self,
_name: &'static str,
value: &T,
) -> Result<()>
fn serialize_newtype_struct<T>( self, _name: &'static str, value: &T, ) -> Result<()>
struct Millimeters(u8). Read moreSource§fn serialize_newtype_variant<T>(
self,
_name: &'static str,
variant_index: u32,
_variant: &'static str,
value: &T,
) -> Result<()>
fn serialize_newtype_variant<T>( self, _name: &'static str, variant_index: u32, _variant: &'static str, value: &T, ) -> Result<()>
Source§fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq>
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq>
serialize_element, then a call to
end. Read moreSource§fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple>
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple>
serialize_element,
then a call to end. Read moreSource§fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct>
fn serialize_tuple_struct( self, _name: &'static str, _len: usize, ) -> Result<Self::SerializeTupleStruct>
struct Rgb(u8, u8, u8). This
call must be followed by zero or more calls to serialize_field, then a
call to end. Read moreSource§fn serialize_tuple_variant(
self,
_name: &'static str,
variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant>
fn serialize_tuple_variant( self, _name: &'static str, variant_index: u32, _variant: &'static str, _len: usize, ) -> Result<Self::SerializeTupleVariant>
E::T in enum E { T(u8, u8) }. This call must be followed by zero or more calls to
serialize_field, then a call to end. Read moreSource§fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeStruct>
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeStruct>
serialize_key and serialize_value, then a call to end. Read moreSource§fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct>
fn serialize_struct( self, _name: &'static str, _len: usize, ) -> Result<Self::SerializeStruct>
struct Rgb { r: u8, g: u8, b: u8 }.
This call must be followed by zero or more calls to serialize_field,
then a call to end. Read moreSource§fn serialize_struct_variant(
self,
_name: &'static str,
variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant>
fn serialize_struct_variant( self, _name: &'static str, variant_index: u32, _variant: &'static str, _len: usize, ) -> Result<Self::SerializeStructVariant>
E::S in enum E { S { r: u8, g: u8, b: u8 } }. This call must be followed by zero or more calls to
serialize_field, then a call to end. Read moreSource§fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error>
fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error>
i128 value. Read moreSource§fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error>
fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error>
u128 value. Read moreSource§fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>
fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>
Source§fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
Source§fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
Display. Read moreSource§fn is_human_readable(&self) -> bool
fn is_human_readable(&self) -> bool
Serialize implementations should serialize in
human-readable form. Read more