[][src]Struct bytekey_fix::ser::Serializer

pub struct Serializer<W> where
    W: Write
{ /* fields omitted */ }

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

impl<W> Serializer<W> where
    W: Write
[src]

pub fn new(writer: W) -> Serializer<W>[src]

Creates a new ordered bytes encoder whose output will be written to the provided writer.

pub fn serialize_var_u64(&mut self, val: u64) -> Result<()>[src]

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

pub fn serialize_var_i64(&mut self, v: i64) -> Result<()>[src]

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

impl<W: Debug> Debug for Serializer<W> where
    W: Write
[src]

impl<'a, W> SerializeMap for &'a mut Serializer<W> where
    W: Write
[src]

type Ok = ()

Must match the Ok type of our Serializer.

type Error = Error

Must match the Error type of our Serializer.

impl<'a, W> SerializeSeq for &'a mut Serializer<W> where
    W: Write
[src]

type Ok = ()

Must match the Ok type of our Serializer.

type Error = Error

Must match the Error type of our Serializer.

impl<'a, W> SerializeStruct for &'a mut Serializer<W> where
    W: Write
[src]

type Ok = ()

Must match the Ok type of our Serializer.

type Error = Error

Must match the Error type of our Serializer.

impl<'a, W> SerializeStructVariant for &'a mut Serializer<W> where
    W: Write
[src]

type Ok = ()

Must match the Ok type of our Serializer.

type Error = Error

Must match the Error type of our Serializer.

impl<'a, W> SerializeTuple for &'a mut Serializer<W> where
    W: Write
[src]

type Ok = ()

Must match the Ok type of our Serializer.

type Error = Error

Must match the Error type of our Serializer.

impl<'a, W> SerializeTupleStruct for &'a mut Serializer<W> where
    W: Write
[src]

type Ok = ()

Must match the Ok type of our Serializer.

type Error = Error

Must match the Error type of our Serializer.

impl<'a, W> SerializeTupleVariant for &'a mut Serializer<W> where
    W: Write
[src]

type Ok = ()

Must match the Ok type of our Serializer.

type Error = Error

Must match the Error type of our Serializer.

impl<'a, W> Serializer for &'a mut Serializer<W> where
    W: Write
[src]

type Ok = ()

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 = Self

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

type SerializeTuple = Self

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

type SerializeTupleStruct = Self

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

type SerializeTupleVariant = Self

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

type SerializeMap = Self

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

type SerializeStruct = Self

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

type SerializeStructVariant = Self

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

fn serialize_f32(self, v: f32) -> Result<()>[src]

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.

fn serialize_f64(self, v: f64) -> Result<()>[src]

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.

Auto Trait Implementations

impl<W> RefUnwindSafe for Serializer<W> where
    W: RefUnwindSafe

impl<W> Send for Serializer<W> where
    W: Send

impl<W> Sync for Serializer<W> where
    W: Sync

impl<W> Unpin for Serializer<W> where
    W: Unpin

impl<W> UnwindSafe for Serializer<W> where
    W: UnwindSafe

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.