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

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