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

License | BSD3
Stability | experimental
Portability | type-families, generalized newtype deriving
Safe Haskell | None
Language | Haskell2010

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

- unVarInt :: n

#### Instances

Instances details

(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 #
Bits n => Bits (VarInt n) Source # |

Instance details

Defined in Data.Bytes.VarInt

Methods

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

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

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

complement :: VarInt n -> VarInt n Source #

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

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

zeroBits :: VarInt n Source #

bit :: Int -> VarInt n Source #

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

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

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

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

bitSizeMaybe :: VarInt n -> Maybe Int Source #

bitSize :: VarInt n -> Int Source #

isSigned :: VarInt n -> Bool Source #

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

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

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

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

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

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

popCount :: VarInt n -> Int Source #

Bounded n => Bounded (VarInt n) Source # |
Instance details Defined in Data.Bytes.VarInt Methods minBound :: VarInt n Source # maxBound :: VarInt n Source #
Enum n => Enum (VarInt n) Source # |

Instance details

Defined in Data.Bytes.VarInt

Methods

succ :: VarInt n -> VarInt n Source #

pred :: VarInt n -> VarInt n Source #

toEnum :: Int -> VarInt n Source #

fromEnum :: VarInt n -> Int Source #

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

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

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

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

Num n => Num (VarInt n) Source # |
Instance details Defined in Data.Bytes.VarInt Methods (+) :: VarInt n -> VarInt n -> VarInt n Source # (-) :: VarInt n -> VarInt n -> VarInt n Source # (*) :: VarInt n -> VarInt n -> VarInt n Source # negate :: VarInt n -> VarInt n Source # abs :: VarInt n -> VarInt n Source # signum :: VarInt n -> VarInt n Source # fromInteger :: Integer -> VarInt n Source #
Integral n => Integral (VarInt n) Source # |

Instance details

Defined in Data.Bytes.VarInt

Methods

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

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

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

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

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

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

toInteger :: VarInt n -> Integer Source #

Real n => Real (VarInt n) Source # |
Instance details Defined in Data.Bytes.VarInt Methods toRational :: VarInt n -> Rational Source #
Show n => Show (VarInt n) Source # |
Instance details Defined in Data.Bytes.VarInt Methods showsPrec :: Int -> VarInt n -> ShowS Source # show :: VarInt n -> String Source # showList :: [VarInt n] -> ShowS Source #
Eq n => Eq (VarInt n) Source # |
Instance details Defined in Data.Bytes.VarInt Methods (==) :: VarInt n -> VarInt n -> Bool Source # (/=) :: VarInt n -> VarInt n -> Bool Source #
Ord n => Ord (VarInt n) Source # |
Instance details Defined in Data.Bytes.VarInt Methods compare :: VarInt n -> VarInt n -> Ordering Source # (<) :: VarInt n -> VarInt n -> Bool Source # (<=) :: VarInt n -> VarInt n -> Bool Source # (>) :: VarInt n -> VarInt n -> Bool Source # (>=) :: VarInt n -> VarInt n -> Bool Source # max :: VarInt n -> VarInt n -> VarInt n Source # min :: VarInt n -> VarInt n -> 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 type Unsigned (VarInt n) = VarInt (Unsigned n)