    {-# LANGUAGE FlexibleContexts #-}
    {-# LANGUAGE UndecidableInstances #-}
    {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    {-# LANGUAGE TypeFamilies #-}
    --------------------------------------------------------------------
    -- |
    -- License   :  BSD3
    -- Stability :  experimental
    -- Portability: type-families, generalized newtype deriving
    --
    -- 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>.
    --------------------------------------------------------------------

    module Data.Bytes.VarInt
      ( VarInt(..)
      ) where

    import Data.Bits
    import Data.Bytes.Signed

    newtype VarInt n = VarInt { forall n. VarInt n -> n
    unVarInt :: n }
      deriving (VarInt n -> VarInt n -> Bool
    (VarInt n -> VarInt n -> Bool)
    -> (VarInt n -> VarInt n -> Bool) -> Eq (VarInt n)
    forall n. Eq n => VarInt n -> VarInt n -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: forall n. Eq n => VarInt n -> VarInt n -> Bool
    == :: VarInt n -> VarInt n -> Bool
    $c/= :: forall n. Eq n => VarInt n -> VarInt n -> Bool
    /= :: VarInt n -> VarInt n -> Bool
    Eq, Eq (VarInt n)
    Eq (VarInt n) =>
    (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)
    -> (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n -> VarInt n)
    -> Ord (VarInt n)
    VarInt n -> VarInt n -> Bool
    VarInt n -> VarInt n -> Ordering
    VarInt n -> VarInt n -> VarInt n
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    forall n. Ord n => Eq (VarInt n)
    forall n. Ord n => VarInt n -> VarInt n -> Bool
    forall n. Ord n => VarInt n -> VarInt n -> Ordering
    forall n. Ord n => VarInt n -> VarInt n -> VarInt n
    $ccompare :: forall n. Ord n => VarInt n -> VarInt n -> Ordering
    compare :: VarInt n -> VarInt n -> Ordering
    $c< :: forall n. Ord n => VarInt n -> VarInt n -> Bool
    < :: VarInt n -> VarInt n -> Bool
    $c<= :: forall n. Ord n => VarInt n -> VarInt n -> Bool
    <= :: VarInt n -> VarInt n -> Bool
    $c> :: forall n. Ord n => VarInt n -> VarInt n -> Bool
    > :: VarInt n -> VarInt n -> Bool
    $c>= :: forall n. Ord n => VarInt n -> VarInt n -> Bool
    >= :: VarInt n -> VarInt n -> Bool
    $cmax :: forall n. Ord n => VarInt n -> VarInt n -> VarInt n
    max :: VarInt n -> VarInt n -> VarInt n
    $cmin :: forall n. Ord n => VarInt n -> VarInt n -> VarInt n
    min :: VarInt n -> VarInt n -> VarInt n
    Ord, Int -> VarInt n -> ShowS
    [VarInt n] -> ShowS
    VarInt n -> String
    (Int -> VarInt n -> ShowS)
    -> (VarInt n -> String) -> ([VarInt n] -> ShowS) -> Show (VarInt n)
    forall n. Show n => Int -> VarInt n -> ShowS
    forall n. Show n => [VarInt n] -> ShowS
    forall n. Show n => VarInt n -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall n. Show n => Int -> VarInt n -> ShowS
    showsPrec :: Int -> VarInt n -> ShowS
    $cshow :: forall n. Show n => VarInt n -> String
    show :: VarInt n -> String
    $cshowList :: forall n. Show n => [VarInt n] -> ShowS
    showList :: [VarInt n] -> ShowS
    Show, Int -> VarInt n
    VarInt n -> Int
    VarInt n -> [VarInt n]
    VarInt n -> VarInt n
    VarInt n -> VarInt n -> [VarInt n]
    VarInt n -> VarInt n -> VarInt n -> [VarInt n]
    (VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n)
    -> (Int -> VarInt n)
    -> (VarInt n -> Int)
    -> (VarInt n -> [VarInt n])
    -> (VarInt n -> VarInt n -> [VarInt n])
    -> (VarInt n -> VarInt n -> [VarInt n])
    -> (VarInt n -> VarInt n -> VarInt n -> [VarInt n])
    -> Enum (VarInt n)
    forall n. Enum n => Int -> VarInt n
    forall n. Enum n => VarInt n -> Int
    forall n. Enum n => VarInt n -> [VarInt n]
    forall n. Enum n => VarInt n -> VarInt n
    forall n. Enum n => VarInt n -> VarInt n -> [VarInt n]
    forall n. Enum n => VarInt n -> VarInt n -> VarInt n -> [VarInt n]
    forall a.
    (a -> a)
    -> (a -> a)
    -> (Int -> a)
    -> (a -> Int)
    -> (a -> [a])
    -> (a -> a -> [a])
    -> (a -> a -> [a])
    -> (a -> a -> a -> [a])
    -> Enum a
    $csucc :: forall n. Enum n => VarInt n -> VarInt n
    succ :: VarInt n -> VarInt n
    $cpred :: forall n. Enum n => VarInt n -> VarInt n
    pred :: VarInt n -> VarInt n
    $ctoEnum :: forall n. Enum n => Int -> VarInt n
    toEnum :: Int -> VarInt n
    $cfromEnum :: forall n. Enum n => VarInt n -> Int
    fromEnum :: VarInt n -> Int
    $cenumFrom :: forall n. Enum n => VarInt n -> [VarInt n]
    enumFrom :: VarInt n -> [VarInt n]
    $cenumFromThen :: forall n. Enum n => VarInt n -> VarInt n -> [VarInt n]
    enumFromThen :: VarInt n -> VarInt n -> [VarInt n]
    $cenumFromTo :: forall n. Enum n => VarInt n -> VarInt n -> [VarInt n]
    enumFromTo :: VarInt n -> VarInt n -> [VarInt n]
    $cenumFromThenTo :: forall n. Enum n => VarInt n -> VarInt n -> VarInt n -> [VarInt n]
    enumFromThenTo :: VarInt n -> VarInt n -> VarInt n -> [VarInt n]
    Enum, Integer -> VarInt n
    VarInt n -> VarInt n
    VarInt n -> VarInt n -> VarInt n
    (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n)
    -> (Integer -> VarInt n)
    -> Num (VarInt n)
    forall n. Num n => Integer -> VarInt n
    forall n. Num n => VarInt n -> VarInt n
    forall n. Num n => VarInt n -> VarInt n -> VarInt n
    forall a.
    (a -> a -> a)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> (a -> a)
    -> (a -> a)
    -> (a -> a)
    -> (Integer -> a)
    -> Num a
    $c+ :: forall n. Num n => VarInt n -> VarInt n -> VarInt n
    + :: VarInt n -> VarInt n -> VarInt n
    $c- :: forall n. Num n => VarInt n -> VarInt n -> VarInt n
    - :: VarInt n -> VarInt n -> VarInt n
    $c* :: forall n. Num n => VarInt n -> VarInt n -> VarInt n
    * :: VarInt n -> VarInt n -> VarInt n
    $cnegate :: forall n. Num n => VarInt n -> VarInt n
    negate :: VarInt n -> VarInt n
    $cabs :: forall n. Num n => VarInt n -> VarInt n
    abs :: VarInt n -> VarInt n
    $csignum :: forall n. Num n => VarInt n -> VarInt n
    signum :: VarInt n -> VarInt n
    $cfromInteger :: forall n. Num n => Integer -> VarInt n
    fromInteger :: Integer -> VarInt n
    Num, Enum (VarInt n)
    Real (VarInt n)
    (Real (VarInt n), Enum (VarInt n)) =>
    (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n -> (VarInt n, VarInt n))
    -> (VarInt n -> VarInt n -> (VarInt n, VarInt n))
    -> (VarInt n -> Integer)
    -> Integral (VarInt n)
    VarInt n -> Integer
    VarInt n -> VarInt n -> (VarInt n, VarInt n)
    VarInt n -> VarInt n -> VarInt n
    forall n. Integral n => Enum (VarInt n)
    forall n. Integral n => Real (VarInt n)
    forall n. Integral n => VarInt n -> Integer
    forall n.
    Integral n =>
    VarInt n -> VarInt n -> (VarInt n, VarInt n)
    forall n. Integral n => VarInt n -> VarInt n -> VarInt n
    forall a.
    (Real a, Enum a) =>
    (a -> a -> a)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> (a -> a -> (a, a))
    -> (a -> a -> (a, a))
    -> (a -> Integer)
    -> Integral a
    $cquot :: forall n. Integral n => VarInt n -> VarInt n -> VarInt n
    quot :: VarInt n -> VarInt n -> VarInt n
    $crem :: forall n. Integral n => VarInt n -> VarInt n -> VarInt n
    rem :: VarInt n -> VarInt n -> VarInt n
    $cdiv :: forall n. Integral n => VarInt n -> VarInt n -> VarInt n
    div :: VarInt n -> VarInt n -> VarInt n
    $cmod :: forall n. Integral n => VarInt n -> VarInt n -> VarInt n
    mod :: VarInt n -> VarInt n -> VarInt n
    $cquotRem :: forall n.
    Integral n =>
    VarInt n -> VarInt n -> (VarInt n, VarInt n)
    quotRem :: VarInt n -> VarInt n -> (VarInt n, VarInt n)
    $cdivMod :: forall n.
    Integral n =>
    VarInt n -> VarInt n -> (VarInt n, VarInt n)
    divMod :: VarInt n -> VarInt n -> (VarInt n, VarInt n)
    $ctoInteger :: forall n. Integral n => VarInt n -> Integer
    toInteger :: VarInt n -> Integer
    Integral, VarInt n
    VarInt n -> VarInt n -> Bounded (VarInt n)
    forall a. a -> a -> Bounded a
    forall n. Bounded n => VarInt n
    $cminBound :: forall n. Bounded n => VarInt n
    minBound :: VarInt n
    $cmaxBound :: forall n. Bounded n => VarInt n
    maxBound :: VarInt n
    Bounded, Num (VarInt n)
    Ord (VarInt n)
    (Num (VarInt n), Ord (VarInt n)) =>
    (VarInt n -> Rational) -> Real (VarInt n)
    VarInt n -> Rational
    forall a. (Num a, Ord a) => (a -> Rational) -> Real a
    forall n. Real n => Num (VarInt n)
    forall n. Real n => Ord (VarInt n)
    forall n. Real n => VarInt n -> Rational
    $ctoRational :: forall n. Real n => VarInt n -> Rational
    toRational :: VarInt n -> Rational
    Real, Eq (VarInt n)
    VarInt n
    Eq (VarInt n) =>
    (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n -> VarInt n)
    -> (VarInt n -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> VarInt n
    -> (Int -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int -> Bool)
    -> (VarInt n -> Maybe Int)
    -> (VarInt n -> Int)
    -> (VarInt n -> Bool)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int -> VarInt n)
    -> (VarInt n -> Int)
    -> Bits (VarInt n)
    Int -> VarInt n
    VarInt n -> Bool
    VarInt n -> Int
    VarInt n -> Maybe Int
    VarInt n -> VarInt n
    VarInt n -> Int -> Bool
    VarInt n -> Int -> VarInt n
    VarInt n -> VarInt n -> VarInt n
    forall a.
    Eq a =>
    (a -> a -> a)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> (a -> a)
    -> (a -> Int -> a)
    -> (a -> Int -> a)
    -> a
    -> (Int -> a)
    -> (a -> Int -> a)
    -> (a -> Int -> a)
    -> (a -> Int -> a)
    -> (a -> Int -> Bool)
    -> (a -> Maybe Int)
    -> (a -> Int)
    -> (a -> Bool)
    -> (a -> Int -> a)
    -> (a -> Int -> a)
    -> (a -> Int -> a)
    -> (a -> Int -> a)
    -> (a -> Int -> a)
    -> (a -> Int -> a)
    -> (a -> Int)
    -> Bits a
    forall n. Bits n => Eq (VarInt n)
    forall n. Bits n => VarInt n
    forall n. Bits n => Int -> VarInt n
    forall n. Bits n => VarInt n -> Bool
    forall n. Bits n => VarInt n -> Int
    forall n. Bits n => VarInt n -> Maybe Int
    forall n. Bits n => VarInt n -> VarInt n
    forall n. Bits n => VarInt n -> Int -> Bool
    forall n. Bits n => VarInt n -> Int -> VarInt n
    forall n. Bits n => VarInt n -> VarInt n -> VarInt n
    $c.&. :: forall n. Bits n => VarInt n -> VarInt n -> VarInt n
    .&. :: VarInt n -> VarInt n -> VarInt n
    $c.|. :: forall n. Bits n => VarInt n -> VarInt n -> VarInt n
    .|. :: VarInt n -> VarInt n -> VarInt n
    $cxor :: forall n. Bits n => VarInt n -> VarInt n -> VarInt n
    xor :: VarInt n -> VarInt n -> VarInt n
    $ccomplement :: forall n. Bits n => VarInt n -> VarInt n
    complement :: VarInt n -> VarInt n
    $cshift :: forall n. Bits n => VarInt n -> Int -> VarInt n
    shift :: VarInt n -> Int -> VarInt n
    $crotate :: forall n. Bits n => VarInt n -> Int -> VarInt n
    rotate :: VarInt n -> Int -> VarInt n
    $czeroBits :: forall n. Bits n => VarInt n
    zeroBits :: VarInt n
    $cbit :: forall n. Bits n => Int -> VarInt n
    bit :: Int -> VarInt n
    $csetBit :: forall n. Bits n => VarInt n -> Int -> VarInt n
    setBit :: VarInt n -> Int -> VarInt n
    $cclearBit :: forall n. Bits n => VarInt n -> Int -> VarInt n
    clearBit :: VarInt n -> Int -> VarInt n
    $ccomplementBit :: forall n. Bits n => VarInt n -> Int -> VarInt n
    complementBit :: VarInt n -> Int -> VarInt n
    $ctestBit :: forall n. Bits n => VarInt n -> Int -> Bool
    testBit :: VarInt n -> Int -> Bool
    $cbitSizeMaybe :: forall n. Bits n => VarInt n -> Maybe Int
    bitSizeMaybe :: VarInt n -> Maybe Int
    $cbitSize :: forall n. Bits n => VarInt n -> Int
    bitSize :: VarInt n -> Int
    $cisSigned :: forall n. Bits n => VarInt n -> Bool
    isSigned :: VarInt n -> Bool
    $cshiftL :: forall n. Bits n => VarInt n -> Int -> VarInt n
    shiftL :: VarInt n -> Int -> VarInt n
    $cunsafeShiftL :: forall n. Bits n => VarInt n -> Int -> VarInt n
    unsafeShiftL :: VarInt n -> Int -> VarInt n
    $cshiftR :: forall n. Bits n => VarInt n -> Int -> VarInt n
    shiftR :: VarInt n -> Int -> VarInt n
    $cunsafeShiftR :: forall n. Bits n => VarInt n -> Int -> VarInt n
    unsafeShiftR :: VarInt n -> Int -> VarInt n
    $crotateL :: forall n. Bits n => VarInt n -> Int -> VarInt n
    rotateL :: VarInt n -> Int -> VarInt n
    $crotateR :: forall n. Bits n => VarInt n -> Int -> VarInt n
    rotateR :: VarInt n -> Int -> VarInt n
    $cpopCount :: forall n. Bits n => VarInt n -> Int
    popCount :: VarInt n -> Int
    Bits)

    type instance Unsigned (VarInt n) = VarInt (Unsigned n)
    type instance Signed (VarInt n) = VarInt (Signed n)