Skip to content

module that, when targeted using the serde `with` field attribute, causes integer primitives to be serialized using variable integer encoding

License

Notifications You must be signed in to change notification settings

sirkibsirkib/serde_varint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VarInt for Serde

This crate offers functions that change the way integers are encoded for your serde serializer to that of variable-integer encoding, which is usually more compact. You can choose which fields do and do not use these overriding functions.

// imports go here

#[derive(Serialize, Deserialize)]
struct YourStruct {
    #[serde(with = "serde_varint")]
    x: u64,

    y: u64,
}

Using bincode as an example, YourStruct { x:5, y:5 } is serialized as:

  • x --> [05]
  • y --> [05,00,00,00,00,00,00,00]

The crate exposes two functions: serialize and deserialize which are generic over integer types {u8, i8, u16, i16, ...}.

If you're using serde's derive macros to serialize your types (I know I often do), its very conventient to inject the var-int functions just using a serde attribute tag for the desired fields (as in the example above).

Otherwise, the functions can be called manually.

This crate just adapts the low-level, actual encoding provided by crate integer-encoding for use with the serde data model.

Trade-off

Serializing a number using var-int encoding is slower than just slapping the bytes into the writer. For bincode, it seems to be around 1x slower extra for each byte in the variable-encoded form. Since serializing and deserializing these numbers is reasonably fast, this shouldn't usually be your bottleneck anyway. Just bear it in mind when deciding whether or not to use this option.

Var Int for raw numbers

If you want to serialize just some integer (eg: u64), there are two options:

  1. Create a newtype struct with the integer as its field, apply the serde attribute exactly as before.
  2. Use the integer-encoding crate directly.

About

module that, when targeted using the serde `with` field attribute, causes integer primitives to be serialized using variable integer encoding

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages