### 14 releases

new 0.7.1 | Feb 12, 2024 |
---|---|

0.6.0 | Mar 21, 2019 |

0.5.1 | Sep 14, 2018 |

0.3.4 | Mar 30, 2018 |

0.3.2 | Jul 29, 2017 |

#**370** in Encoding

**3,111** downloads per month

Used in **5** crates
(2 directly)

**MIT**license

30KB

860 lines

See crate docs.

###
`lib.rs`

:

Variable length signed and unsigned integer types. Types support up to 128-bit integers, both are encoded to 1-17 bytes.

Encoding rules are based on SQLite 4 Varuint type with modifications for support of 128-bit long integers. Signed integers are encoded using the Protobuf ZigZag approach and reuses unsigned integer as a storage.

Unlike the Protobuf encoding rules

needs the first byte only to find out the length of the
whole value. Microbenchmarks say that it is a lot faster.`Varint`

## How to use

Add dependency to your Cargo.toml:

`[``dependencies``]`
`varuint ``=` `"`0.6`"`

Add imports to your code:

`
``use` `varuint``::``{`Varint`,` Serializable`,` Deserializable`}``;`

Use it:

`use` `std``::`mem`;`
`use` `std``::``io``::`Cursor`;`
`use` `varuint``::``*``;`
`fn` `main``(``)`` ``{`
`let` `mut` cursor `=` `Cursor``::`new`(``vec!``[``]``)``;`
`let` `_` `=` cursor`.``write_varint``(``1``u8``)``.``unwrap``(``)``;`
`let` `_` `=` cursor`.``write_varint``(``-``300``i16``)``.``unwrap``(``)``;`
`let` v `=` Varint`(``-``56_782``i128``)``;`
`let` `_` `=` v`.``serialize``(``&``mut` cursor`)``.``unwrap``(``)``;`
cursor`.``set_position``(``0``)``;`
`assert_eq!``(``1``u8``,` `ReadVarint``::``<``u8``>``::`read_varint`(``&``mut` cursor`)``.``unwrap``(``)``)``;`
`assert_eq!``(``-``300``i16``,` `ReadVarint``::``<``i16``>``::`read_varint`(``&``mut` cursor`)``.``unwrap``(``)``)``;`
`assert_eq!``(`v`,` `Varint``::``<``i128``>``::`deserialize`(``&``mut` cursor`)``.``unwrap``(``)``)``;`
`}`

## Encoding rules

Encoding rules for unsinged integer

are (assuming value is `Varint`

):`V`

- If

then output a single byte`V``<`=240

equal to`A0`

.`V` - If

then output`V``<`=2031

as`A0`

and`(`V`-``240``)``/``256``+``241`

as`A1`

.`(`V`-``240``)``%``256` - If

then output`V``<`=67567

as`A0`

,`248`

as`A1`

, and`(`V`-``2032``)``/``256`

as`A2`

.`(`V`-``2032``)``%``256` - If

then output`V``<`=16_777_215

as`A0`

and`249`

through`A1`

as a little-endian 3-byte integer.`A3` - If

then output`V``<`=4_294_967_295

as`A0`

and`250`

as a little-endian 4-byte integer.`A1``..``A4` - If

then output`V``<`=1_099_511_627_775

as`A0`

and`251`

as a little-endian 5-byte integer.`A1``..``A5` - If

then output`V``<`=281_474_976_710_655

as`A0`

and`252`

as a little-endian 6-byte integer.`A1``..``A6` - If

then output`V``<`=72_057_594_037_927_935

as`A0`

and`253`

as a little-endian 7-byte integer.`A1``..``A7` - If

then output`V``<`=9_223_372_036_854_775_807

as`A0`

and`254`

as a little-endian 8-byte integer.`A1``..``A8` - Otherwise output

as`A0`

and`255`

as a little-endian 16-byte integer.`A1``..``A16`

Signed integer

converted to the unsigned integer `Varint`

in the first place and then encoded as an unsigned integer.
Conversion method makes values closer to 0 to take less space.
See Protobuf docs
for details.`Varint`

#### Dependencies

~175KB