bytes-0.17.2: Sharing code for serialization between binary and cereal
LicenseBSD3
Stabilityexperimental
Portabilitytype-families, generalized newtype deriving
Safe HaskellNone
LanguageHaskell2010

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

newtype VarInt n Source #

Constructors

VarInt 

Fields

Instances

Instances details
Bounded n => Bounded (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

Methods

minBound :: VarInt n #

maxBound :: VarInt n #

Enum n => Enum (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

Methods

succ :: VarInt n -> VarInt n #

pred :: VarInt n -> VarInt n #

toEnum :: Int -> VarInt n #

fromEnum :: VarInt n -> Int #

enumFrom :: VarInt n -> [VarInt n] #

enumFromThen :: VarInt n -> VarInt n -> [VarInt n] #

enumFromTo :: VarInt n -> VarInt n -> [VarInt n] #

enumFromThenTo :: VarInt n -> VarInt n -> VarInt n -> [VarInt n] #

Eq n => Eq (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

Methods

(==) :: VarInt n -> VarInt n -> Bool #

(/=) :: VarInt n -> VarInt n -> Bool #

Integral n => Integral (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

Methods

quot :: VarInt n -> VarInt n -> VarInt n #

rem :: VarInt n -> VarInt n -> VarInt n #

div :: VarInt n -> VarInt n -> VarInt n #

mod :: VarInt n -> VarInt n -> VarInt n #

quotRem :: VarInt n -> VarInt n -> (VarInt n, VarInt n) #

divMod :: VarInt n -> VarInt n -> (VarInt n, VarInt n) #

toInteger :: VarInt n -> Integer #

Num n => Num (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

Methods

(+) :: VarInt n -> VarInt n -> VarInt n #

(-) :: VarInt n -> VarInt n -> VarInt n #

(*) :: VarInt n -> VarInt n -> VarInt n #

negate :: VarInt n -> VarInt n #

abs :: VarInt n -> VarInt n #

signum :: VarInt n -> VarInt n #

fromInteger :: Integer -> VarInt n #

Ord n => Ord (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

Methods

compare :: VarInt n -> VarInt n -> Ordering #

(<) :: VarInt n -> VarInt n -> Bool #

(<=) :: VarInt n -> VarInt n -> Bool #

(>) :: VarInt n -> VarInt n -> Bool #

(>=) :: VarInt n -> VarInt n -> Bool #

max :: VarInt n -> VarInt n -> VarInt n #

min :: VarInt n -> VarInt n -> VarInt n #

Real n => Real (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

Methods

toRational :: VarInt n -> Rational #

Show n => Show (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

Methods

showsPrec :: Int -> VarInt n -> ShowS #

show :: VarInt n -> String #

showList :: [VarInt n] -> ShowS #

Bits n => Bits (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

Methods

(.&.) :: VarInt n -> VarInt n -> VarInt n #

(.|.) :: VarInt n -> VarInt n -> VarInt n #

xor :: VarInt n -> VarInt n -> VarInt n #

complement :: VarInt n -> VarInt n #

shift :: VarInt n -> Int -> VarInt n #

rotate :: VarInt n -> Int -> VarInt n #

zeroBits :: VarInt n #

bit :: Int -> VarInt n #

setBit :: VarInt n -> Int -> VarInt n #

clearBit :: VarInt n -> Int -> VarInt n #

complementBit :: VarInt n -> Int -> VarInt n #

testBit :: VarInt n -> Int -> Bool #

bitSizeMaybe :: VarInt n -> Maybe Int #

bitSize :: VarInt n -> Int #

isSigned :: VarInt n -> Bool #

shiftL :: VarInt n -> Int -> VarInt n #

unsafeShiftL :: VarInt n -> Int -> VarInt n #

shiftR :: VarInt n -> Int -> VarInt n #

unsafeShiftR :: VarInt n -> Int -> VarInt n #

rotateL :: VarInt n -> Int -> VarInt n #

rotateR :: VarInt n -> Int -> VarInt n #

popCount :: VarInt n -> Int #

(Bits n, Integral n, Bits (Unsigned n), Integral (Unsigned n)) => Serial (VarInt n) Source #

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)
"\NUL\NUL\NUL\NUL\NUL\NUL\NULa"
>>> mconcat $ BSL.toChunks $ runPutL $ serialize (97 :: VarInt Word64)
"a"
Instance details

Defined in Data.Bytes.Serial

Methods

serialize :: MonadPut m => VarInt n -> m () Source #

deserialize :: MonadGet m => m (VarInt n) Source #

type Signed (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt

type Signed (VarInt n) = VarInt (Signed n)
type Unsigned (VarInt n) Source # 
Instance details

Defined in Data.Bytes.VarInt