bytes-0.15.2: Sharing code for serialization between binary and cereal

Portabilitytype-families, generalized newtype deriving
Safe HaskellNone



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:


newtype VarInt n Source




unVarInt :: n


Bounded n => Bounded (VarInt n) Source 
Enum n => Enum (VarInt n) Source 
Eq n => Eq (VarInt n) Source 
Integral n => Integral (VarInt n) Source 
Num n => Num (VarInt n) Source 
Ord n => Ord (VarInt n) Source 
Real n => Real (VarInt n) Source 
Show n => Show (VarInt n) Source 
Bits n => Bits (VarInt n) Source 
(Bits n, Integral n, Bits (Unsigned n), Integral (Unsigned n)) => Serial (VarInt n) Source

$setup >>> import Data.Word >>> import Data.Fixed >>> import Data.Bytes.Serial

Integer/Word types serialized to base-128 variable-width ints.

>>> import Data.Monoid (mconcat)
>>> import qualified Data.ByteString.Lazy as BSL
>>> mconcat $ BSL.toChunks $ runPutL $ serialize (97 :: Word64)
>>> mconcat $ BSL.toChunks $ runPutL $ serialize (97 :: VarInt Word64)
type Signed (VarInt n) = VarInt (Signed n) Source 
type Unsigned (VarInt n) = VarInt (Unsigned n) Source