Portability | type-families, generalized newtype deriving |
---|---|
Stability | experimental |
Safe Haskell | None |
Data.Bytes.VarInt
Description
This module provides a VarInt
wrapper with a Serial
instance
that generates base-128 variable-width ints. Values are encoded 7
bits at a time, with the most significant being a continuation bit.
Thus, the numbers from 0 to 127 require only a single byte to
encode, those from 128 to 16383 require two bytes, etc.
This format is taken from Google's Protocol Buffers, which provides a bit more verbiage on the encoding: https://developers.google.com/protocol-buffers/docs/encoding#varints.
Documentation
Integer/Word types serialized to base-128 variable-width ints.
>>>
import Data.Monoid (mconcat)
>>>
import Data.ByteString.Lazy (toChunks)
>>>
mconcat $ toChunks $ runPutL $ serialize (97 :: Word64)
"\NUL\NUL\NUL\NUL\NUL\NUL\NULa">>>
mconcat $ toChunks $ runPutL $ serialize (97 :: VarInt Word64)
"a"
Instances
Bounded n => Bounded (VarInt n) | |
Enum n => Enum (VarInt n) | |
Eq n => Eq (VarInt n) | |
Integral n => Integral (VarInt n) | |
Num n => Num (VarInt n) | |
Ord n => Ord (VarInt n) | |
Real n => Real (VarInt n) | |
Show n => Show (VarInt n) | |
Bits n => Bits (VarInt n) | |
(Bits n, Integral n, Bits (Unsigned n), Integral (Unsigned n)) => Serial (VarInt n) |