    {-# LANGUAGE BangPatterns #-}
    {-# LANGUAGE CPP #-}
    {-# LANGUAGE DeriveDataTypeable #-}
    {-# LANGUAGE FlexibleContexts #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE StandaloneDeriving #-}

    module Data.ProtocolBuffers.Wire
      ( Enumeration(..)
      , WireField(..)
      , Tag
      , EncodeWire(..)
      , DecodeWire(..)
      , wireFieldTag
      , getWireField
      , getVarInt
      , getVarintPrefixedBS
      , putVarSInt
      , putVarUInt
      , putVarintPrefixedBS
      , zzEncode32
      , zzEncode64
      , zzDecode32
      , zzDecode64
      ) where

    import Control.Applicative
    import Data.Bits
    import Data.ByteString (ByteString)
    import qualified Data.ByteString as B
    import Data.Foldable
    import Data.Int
    import Data.Monoid
    import Data.Serialize.Get
    import Data.Serialize.IEEE754
    import Data.Serialize.Put
    import qualified Data.Text as T
    import qualified Data.Text.Encoding as T
    import Data.Typeable
    import Data.Word
    import Data.Binary.IEEE754 (wordToDouble, wordToFloat)

    import Data.ProtocolBuffers.Types

    -- |
    -- Field identifiers
    type Tag = Word32

    -- |
    -- A representation of the wire format as described in
    -- <https://developers.google.com/protocol-buffers/docs/encoding#structure>
    data WireField
      = VarintField    {-# UNPACK #-} !Tag {-# UNPACK #-} !Word64 -- ^ For: int32, int64, uint32, uint64, sint32, sint64, bool, enum
      | Fixed64Field   {-# UNPACK #-} !Tag {-# UNPACK #-} !Word64 -- ^ For: fixed64, sfixed64, double
      | DelimitedField {-# UNPACK #-} !Tag !ByteString -- ^ For: string, bytes, embedded messages, packed repeated fields
      | StartField     {-# UNPACK #-} !Tag -- ^ For: groups (deprecated)
      | EndField       {-# UNPACK #-} !Tag -- ^ For: groups (deprecated)
      | Fixed32Field   {-# UNPACK #-} !Tag {-# UNPACK #-} !Word32 -- ^ For: fixed32, sfixed32, float
        deriving (WireField -> WireField -> Bool
    (WireField -> WireField -> Bool)
    -> (WireField -> WireField -> Bool) -> Eq WireField
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: WireField -> WireField -> Bool
    == :: WireField -> WireField -> Bool
    $c/= :: WireField -> WireField -> Bool
    /= :: WireField -> WireField -> Bool
    Eq, Eq WireField
    Eq WireField =>
    (WireField -> WireField -> Ordering)
    -> (WireField -> WireField -> Bool)
    -> (WireField -> WireField -> Bool)
    -> (WireField -> WireField -> Bool)
    -> (WireField -> WireField -> Bool)
    -> (WireField -> WireField -> WireField)
    -> (WireField -> WireField -> WireField)
    -> Ord WireField
    WireField -> WireField -> Bool
    WireField -> WireField -> Ordering
    WireField -> WireField -> WireField
    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
    $ccompare :: WireField -> WireField -> Ordering
    compare :: WireField -> WireField -> Ordering
    $c< :: WireField -> WireField -> Bool
    < :: WireField -> WireField -> Bool
    $c<= :: WireField -> WireField -> Bool
    <= :: WireField -> WireField -> Bool
    $c> :: WireField -> WireField -> Bool
    > :: WireField -> WireField -> Bool
    $c>= :: WireField -> WireField -> Bool
    >= :: WireField -> WireField -> Bool
    $cmax :: WireField -> WireField -> WireField
    max :: WireField -> WireField -> WireField
    $cmin :: WireField -> WireField -> WireField
    min :: WireField -> WireField -> WireField
    Ord, Int -> WireField -> ShowS
    [WireField] -> ShowS
    WireField -> String
    (Int -> WireField -> ShowS)
    -> (WireField -> String)
    -> ([WireField] -> ShowS)
    -> Show WireField
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> WireField -> ShowS
    showsPrec :: Int -> WireField -> ShowS
    $cshow :: WireField -> String
    show :: WireField -> String
    $cshowList :: [WireField] -> ShowS
    showList :: [WireField] -> ShowS
    Show, Typeable)

    getVarintPrefixedBS :: Get ByteString
    getVarintPrefixedBS :: Get ByteString
    getVarintPrefixedBS = Int -> Get ByteString
    getBytes (Int -> Get ByteString) -> Get Int -> Get ByteString
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
    =<< Get Int
    forall a. (Integral a, Bits a) => Get a
    getVarInt

    putVarintPrefixedBS :: ByteString -> Put
    putVarintPrefixedBS :: ByteString -> Put
    putVarintPrefixedBS ByteString
    bs = Int -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt (ByteString -> Int
    B.length ByteString
    bs) Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> ByteString -> Put
    putByteString ByteString
    bs

    getWireField :: Get WireField
    getWireField :: Get WireField
    getWireField = do
      wireTag <- Get Word32
    forall a. (Integral a, Bits a) => Get a
    getVarInt
      let tag = Word32
    wireTag Word32 -> Int -> Word32
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    3
      case wireTag .&. 7 of
        Word32
    0 -> Word32 -> Word64 -> WireField
    VarintField    Word32
    tag (Word64 -> WireField) -> Get Word64 -> Get WireField
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Get Word64
    forall a. (Integral a, Bits a) => Get a
    getVarInt
        Word32
    1 -> Word32 -> Word64 -> WireField
    Fixed64Field   Word32
    tag (Word64 -> WireField) -> Get Word64 -> Get WireField
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Get Word64
    getWord64le
        Word32
    2 -> Word32 -> ByteString -> WireField
    DelimitedField Word32
    tag (ByteString -> WireField) -> Get ByteString -> Get WireField
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Get ByteString
    getVarintPrefixedBS
        Word32
    3 -> WireField -> Get WireField
    forall a. a -> Get a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (WireField -> Get WireField) -> WireField -> Get WireField
    forall a b. (a -> b) -> a -> b
    $! Word32 -> WireField
    StartField Word32
    tag
        Word32
    4 -> WireField -> Get WireField
    forall a. a -> Get a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (WireField -> Get WireField) -> WireField -> Get WireField
    forall a b. (a -> b) -> a -> b
    $! Word32 -> WireField
    EndField   Word32
    tag
        Word32
    5 -> Word32 -> Word32 -> WireField
    Fixed32Field   Word32
    tag (Word32 -> WireField) -> Get Word32 -> Get WireField
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Get Word32
    getWord32le
        Word32
    x -> String -> Get WireField
    forall a. String -> Get a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String -> Get WireField) -> String -> Get WireField
    forall a b. (a -> b) -> a -> b
    $ String
    "Wire type out of range: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Word32 -> String
    forall a. Show a => a -> String
    show Word32
    x

    putWireField :: WireField -> Put
    putWireField :: WireField -> Put
    putWireField (VarintField    Word32
    t Word64
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    0 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word64 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt Word64
    val
    putWireField (Fixed64Field   Word32
    t Word64
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    1 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word64 -> Put
    putWord64le Word64
    val
    putWireField (DelimitedField Word32
    t ByteString
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    2 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> ByteString -> Put
    putVarintPrefixedBS ByteString
    val
    putWireField (StartField     Word32
    t    ) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    3
    putWireField (EndField       Word32
    t    ) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    4
    putWireField (Fixed32Field   Word32
    t Word32
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    5 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word32 -> Put
    putWord32le Word32
    val

    putWireTag :: Tag -> Word32 -> Put
    putWireTag :: Word32 -> Word32 -> Put
    putWireTag Word32
    tag Word32
    typ
      | Word32
    tag Word32 -> Word32 -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word32
    0x1FFFFFFF, Word32
    typ Word32 -> Word32 -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word32
    7 = Word32 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt (Word32 -> Put) -> Word32 -> Put
    forall a b. (a -> b) -> a -> b
    $ Word32
    tag Word32 -> Int -> Word32
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    3 Word32 -> Word32 -> Word32
    forall a. Bits a => a -> a -> a
    .|. (Word32
    typ Word32 -> Word32 -> Word32
    forall a. Bits a => a -> a -> a
    .&. Word32
    7)
      | Word32
    tag  Word32 -> Word32 -> Bool
    forall a. Ord a => a -> a -> Bool
    > Word32
    0x1FFFFFFF = String -> Put
    forall (m :: * -> *) a. Monad m => String -> m a
    legacyFail (String -> Put) -> String -> Put
    forall a b. (a -> b) -> a -> b
    $ String
    "Wire tag out of range: "  String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Word32 -> String
    forall a. Show a => a -> String
    show Word32
    tag
      | Bool
    otherwise         = String -> Put
    forall (m :: * -> *) a. Monad m => String -> m a
    legacyFail (String -> Put) -> String -> Put
    forall a b. (a -> b) -> a -> b
    $ String
    "Wire type out of range: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Word32 -> String
    forall a. Show a => a -> String
    show Word32
    typ

    getVarInt :: (Integral a, Bits a) => Get a
    getVarInt :: forall a. (Integral a, Bits a) => Get a
    getVarInt = Int -> a -> Get a
    forall {b}. (Bits b, Num b) => Int -> b -> Get b
    go Int
    0 a
    0 where
      go :: Int -> b -> Get b
    go Int
    n !b
    val = do
        b <- Get Word8
    getWord8
        if testBit b 7
          then go (n+7) (val .|. (fromIntegral (b .&. 0x7F) `shiftL` n))
          else return $! val .|. (fromIntegral b `shiftL` n)

    -- | This can be used on any Integral type and is needed for signed types; unsigned can use putVarUInt below.
    -- This has been changed to handle only up to 64 bit integral values (to match documentation).
    {-# INLINE putVarSInt #-}
    putVarSInt :: (Integral a, Bits a) => a -> Put
    putVarSInt :: forall a. (Integral a, Bits a) => a -> Put
    putVarSInt a
    bIn =
      case a -> a -> Ordering
    forall a. Ord a => a -> a -> Ordering
    compare a
    bIn a
    0 of
        Ordering
    LT -> let -- upcast to 64 bit to match documentation of 10 bytes for all negative values
                  b :: Int64
    b = a -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    bIn
                  len :: Int
    len = Int
    10                                -- (pred 10)*7 < 64 <= 10*7
                  last'Mask :: Int64
    last'Mask = Int64
    1                           -- pred (1 `shiftL` 1)
                  go :: Int64 -> Int -> Put
                  go :: Int64 -> Int -> Put
    go !Int64
    i Int
    1 = Putter Word8
    putWord8 (Int64 -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int64
    i Int64 -> Int64 -> Int64
    forall a. Bits a => a -> a -> a
    .&. Int64
    last'Mask))
                  go !Int64
    i Int
    n = Putter Word8
    putWord8 (Int64 -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int64
    i Int64 -> Int64 -> Int64
    forall a. Bits a => a -> a -> a
    .&. Int64
    0x7F) Word8 -> Word8 -> Word8
    forall a. Bits a => a -> a -> a
    .|. Word8
    0x80) Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Int64 -> Int -> Put
    go (Int64
    i Int64 -> Int -> Int64
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    7) (Int -> Int
    forall a. Enum a => a -> a
    pred Int
    n)
              in Int64 -> Int -> Put
    go Int64
    b Int
    len
        Ordering
    EQ -> Putter Word8
    putWord8 Word8
    0
        Ordering
    GT -> a -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt a
    bIn

    -- | This should be used on unsigned Integral types only (not checked)
    {-# INLINE putVarUInt #-}
    putVarUInt :: (Integral a, Bits a) => a -> Put
    putVarUInt :: forall a. (Integral a, Bits a) => a -> Put
    putVarUInt a
    i
      | a
    i a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    < a
    0x80  = Putter Word8
    putWord8 (a -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    i)
      | Bool
    otherwise = Putter Word8
    putWord8 (a -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a
    i a -> a -> a
    forall a. Bits a => a -> a -> a
    .&. a
    0x7F) Word8 -> Word8 -> Word8
    forall a. Bits a => a -> a -> a
    .|. Word8
    0x80) Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> a -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt (a
    i a -> Int -> a
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    7)

    wireFieldTag :: WireField -> Tag
    wireFieldTag :: WireField -> Word32
    wireFieldTag WireField
    f = case WireField
    f of
      VarintField    Word32
    t Word64
    _ -> Word32
    t
      Fixed64Field   Word32
    t Word64
    _ -> Word32
    t
      DelimitedField Word32
    t ByteString
    _ -> Word32
    t
      StartField     Word32
    t   -> Word32
    t
      EndField       Word32
    t   -> Word32
    t
      Fixed32Field   Word32
    t Word32
    _ -> Word32
    t

    class EncodeWire a where
      encodeWire :: Tag -> a -> Put

    class DecodeWire a where
      decodeWire :: WireField -> Get a

    deriving instance EncodeWire a => EncodeWire (Always (Value a))
    deriving instance EncodeWire a => EncodeWire (Last (Value a))

    deriving instance DecodeWire a => DecodeWire (Always (Value a))
    deriving instance DecodeWire a => DecodeWire (Last (Value a))

    instance EncodeWire a => EncodeWire [Value a] where
      encodeWire :: Word32 -> [Value a] -> Put
    encodeWire Word32
    t = (Value a -> Put) -> [Value a] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Word32 -> Value a -> Put
    forall a. EncodeWire a => Word32 -> a -> Put
    encodeWire Word32
    t)

    instance EncodeWire WireField where
      encodeWire :: Word32 -> WireField -> Put
    encodeWire Word32
    t WireField
    f
        | Word32
    t Word32 -> Word32 -> Bool
    forall a. Eq a => a -> a -> Bool
    == WireField -> Word32
    wireFieldTag WireField
    f = WireField -> Put
    putWireField WireField
    f
        | Bool
    otherwise           = String -> Put
    forall (m :: * -> *) a. Monad m => String -> m a
    legacyFail String
    "Specified tag and field tag do not match"

    instance DecodeWire WireField where
      decodeWire :: WireField -> Get WireField
    decodeWire = WireField -> Get WireField
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure

    instance EncodeWire a => EncodeWire (Value a) where
      encodeWire :: Word32 -> Value a -> Put
    encodeWire Word32
    t = (a -> Put) -> Value a -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Word32 -> a -> Put
    forall a. EncodeWire a => Word32 -> a -> Put
    encodeWire Word32
    t)

    instance DecodeWire a => DecodeWire (Value a) where
      decodeWire :: WireField -> Get (Value a)
    decodeWire = (a -> Value a) -> Get a -> Get (Value a)
    forall a b. (a -> b) -> Get a -> Get b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap a -> Value a
    forall a. a -> Value a
    Value (Get a -> Get (Value a))
    -> (WireField -> Get a) -> WireField -> Get (Value a)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . WireField -> Get a
    forall a. DecodeWire a => WireField -> Get a
    decodeWire

    instance EncodeWire a => EncodeWire (Maybe (Value a)) where
      encodeWire :: Word32 -> Maybe (Value a) -> Put
    encodeWire Word32
    t = (Value a -> Put) -> Maybe (Value a) -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Word32 -> Value a -> Put
    forall a. EncodeWire a => Word32 -> a -> Put
    encodeWire Word32
    t)

    instance DecodeWire a => DecodeWire (Maybe (Value a)) where
      decodeWire :: WireField -> Get (Maybe (Value a))
    decodeWire = (a -> Maybe (Value a)) -> Get a -> Get (Maybe (Value a))
    forall a b. (a -> b) -> Get a -> Get b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (Value a -> Maybe (Value a)
    forall a. a -> Maybe a
    Just (Value a -> Maybe (Value a))
    -> (a -> Value a) -> a -> Maybe (Value a)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Value a
    forall a. a -> Value a
    Value) (Get a -> Get (Maybe (Value a)))
    -> (WireField -> Get a) -> WireField -> Get (Maybe (Value a))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . WireField -> Get a
    forall a. DecodeWire a => WireField -> Get a
    decodeWire

    instance EncodeWire Int32 where
      encodeWire :: Word32 -> Int32 -> Put
    encodeWire Word32
    t Int32
    val = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    0 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Int32 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarSInt Int32
    val

    instance DecodeWire Int32 where
      decodeWire :: WireField -> Get Int32
    decodeWire (VarintField  Word32
    _ Word64
    val) = Int32 -> Get Int32
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Int32 -> Get Int32) -> Int32 -> Get Int32
    forall a b. (a -> b) -> a -> b
    $ Word64 -> Int32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    val
      decodeWire WireField
    _ = Get Int32
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire Int64 where
      encodeWire :: Word32 -> Int64 -> Put
    encodeWire Word32
    t Int64
    val = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    0 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Int64 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarSInt Int64
    val

    instance DecodeWire Int64 where
      decodeWire :: WireField -> Get Int64
    decodeWire (VarintField  Word32
    _ Word64
    val) = Int64 -> Get Int64
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Int64 -> Get Int64) -> Int64 -> Get Int64
    forall a b. (a -> b) -> a -> b
    $ Word64 -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    val
      decodeWire WireField
    _ = Get Int64
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire Word32 where
      encodeWire :: Word32 -> Word32 -> Put
    encodeWire Word32
    t Word32
    val = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    0 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word32 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt Word32
    val

    instance DecodeWire Word32 where
      decodeWire :: WireField -> Get Word32
    decodeWire (VarintField  Word32
    _ Word64
    val) = Word32 -> Get Word32
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Word32 -> Get Word32) -> Word32 -> Get Word32
    forall a b. (a -> b) -> a -> b
    $ Word64 -> Word32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    val
      decodeWire WireField
    _ = Get Word32
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire Word64 where
      encodeWire :: Word32 -> Word64 -> Put
    encodeWire Word32
    t Word64
    val = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    0 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word64 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt Word64
    val

    instance DecodeWire Word64 where
      decodeWire :: WireField -> Get Word64
    decodeWire (VarintField  Word32
    _ Word64
    val) = Word64 -> Get Word64
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Word64
    val
      decodeWire WireField
    _ = Get Word64
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire (Signed Int32) where
      encodeWire :: Word32 -> Signed Int32 -> Put
    encodeWire Word32
    t (Signed Int32
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    0 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word32 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarSInt (Int32 -> Word32
    zzEncode32 Int32
    val)

    instance DecodeWire (Signed Int32) where
      decodeWire :: WireField -> Get (Signed Int32)
    decodeWire (VarintField  Word32
    _ Word64
    val) = Signed Int32 -> Get (Signed Int32)
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Signed Int32 -> Get (Signed Int32))
    -> (Word32 -> Signed Int32) -> Word32 -> Get (Signed Int32)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int32 -> Signed Int32
    forall a. a -> Signed a
    Signed (Int32 -> Signed Int32)
    -> (Word32 -> Int32) -> Word32 -> Signed Int32
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word32 -> Int32
    zzDecode32 (Word32 -> Get (Signed Int32)) -> Word32 -> Get (Signed Int32)
    forall a b. (a -> b) -> a -> b
    $ Word64 -> Word32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    val
      decodeWire WireField
    _ = Get (Signed Int32)
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire (Signed Int64) where
      encodeWire :: Word32 -> Signed Int64 -> Put
    encodeWire Word32
    t (Signed Int64
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    0 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word64 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarSInt (Int64 -> Word64
    zzEncode64 Int64
    val)

    instance DecodeWire (Signed Int64) where
      decodeWire :: WireField -> Get (Signed Int64)
    decodeWire (VarintField  Word32
    _ Word64
    val) = Signed Int64 -> Get (Signed Int64)
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Signed Int64 -> Get (Signed Int64))
    -> (Word64 -> Signed Int64) -> Word64 -> Get (Signed Int64)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int64 -> Signed Int64
    forall a. a -> Signed a
    Signed (Int64 -> Signed Int64)
    -> (Word64 -> Int64) -> Word64 -> Signed Int64
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word64 -> Int64
    zzDecode64 (Word64 -> Get (Signed Int64)) -> Word64 -> Get (Signed Int64)
    forall a b. (a -> b) -> a -> b
    $ Word64 -> Word64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    val
      decodeWire WireField
    _ = Get (Signed Int64)
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire (Fixed Int32) where
      encodeWire :: Word32 -> Fixed Int32 -> Put
    encodeWire Word32
    t (Fixed Int32
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    5 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word32 -> Put
    putWord32le (Int32 -> Word32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int32
    val)

    instance DecodeWire (Fixed Int32) where
      decodeWire :: WireField -> Get (Fixed Int32)
    decodeWire (Fixed32Field Word32
    _ Word32
    val) = Fixed Int32 -> Get (Fixed Int32)
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Fixed Int32 -> Get (Fixed Int32))
    -> (Int32 -> Fixed Int32) -> Int32 -> Get (Fixed Int32)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int32 -> Fixed Int32
    forall a. a -> Fixed a
    Fixed (Int32 -> Get (Fixed Int32)) -> Int32 -> Get (Fixed Int32)
    forall a b. (a -> b) -> a -> b
    $ Word32 -> Int32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word32
    val
      decodeWire WireField
    _ = Get (Fixed Int32)
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire (Fixed Int64) where
      encodeWire :: Word32 -> Fixed Int64 -> Put
    encodeWire Word32
    t (Fixed Int64
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    1 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word64 -> Put
    putWord64le (Int64 -> Word64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int64
    val)

    instance DecodeWire (Fixed Int64) where
      decodeWire :: WireField -> Get (Fixed Int64)
    decodeWire (Fixed64Field Word32
    _ Word64
    val) = Fixed Int64 -> Get (Fixed Int64)
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Fixed Int64 -> Get (Fixed Int64))
    -> (Int64 -> Fixed Int64) -> Int64 -> Get (Fixed Int64)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int64 -> Fixed Int64
    forall a. a -> Fixed a
    Fixed (Int64 -> Get (Fixed Int64)) -> Int64 -> Get (Fixed Int64)
    forall a b. (a -> b) -> a -> b
    $ Word64 -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    val
      decodeWire WireField
    _ = Get (Fixed Int64)
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire (Fixed Word32) where
      encodeWire :: Word32 -> Fixed Word32 -> Put
    encodeWire Word32
    t (Fixed Word32
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    5 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word32 -> Put
    putWord32le Word32
    val

    instance DecodeWire (Fixed Word32) where
      decodeWire :: WireField -> Get (Fixed Word32)
    decodeWire (Fixed32Field Word32
    _ Word32
    val) = Fixed Word32 -> Get (Fixed Word32)
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Fixed Word32 -> Get (Fixed Word32))
    -> Fixed Word32 -> Get (Fixed Word32)
    forall a b. (a -> b) -> a -> b
    $ Word32 -> Fixed Word32
    forall a. a -> Fixed a
    Fixed Word32
    val
      decodeWire WireField
    _ = Get (Fixed Word32)
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire (Fixed Word64) where
      encodeWire :: Word32 -> Fixed Word64 -> Put
    encodeWire Word32
    t (Fixed Word64
    val) = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    1 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Word64 -> Put
    putWord64le Word64
    val

    instance DecodeWire (Fixed Word64) where
      decodeWire :: WireField -> Get (Fixed Word64)
    decodeWire (Fixed64Field Word32
    _ Word64
    val) = Fixed Word64 -> Get (Fixed Word64)
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Fixed Word64 -> Get (Fixed Word64))
    -> Fixed Word64 -> Get (Fixed Word64)
    forall a b. (a -> b) -> a -> b
    $ Word64 -> Fixed Word64
    forall a. a -> Fixed a
    Fixed Word64
    val
      decodeWire WireField
    _ = Get (Fixed Word64)
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire Bool where
      encodeWire :: Word32 -> Bool -> Put
    encodeWire Word32
    t Bool
    val = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    0 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Int32 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt (if Bool
    val then Int32
    1 else (Int32
    0 :: Int32))

    instance DecodeWire Bool where
      decodeWire :: WireField -> Get Bool
    decodeWire (VarintField Word32
    _ Word64
    val) = Bool -> Get Bool
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Bool -> Get Bool) -> Bool -> Get Bool
    forall a b. (a -> b) -> a -> b
    $ Word64
    val Word64 -> Word64 -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Word64
    0
      decodeWire WireField
    _ = Get Bool
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire Float where
      encodeWire :: Word32 -> Float -> Put
    encodeWire Word32
    t Float
    val = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    5 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Float -> Put
    putFloat32le Float
    val

    instance DecodeWire Float where
      decodeWire :: WireField -> Get Float
    decodeWire (Fixed32Field Word32
    _ Word32
    val) = Float -> Get Float
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Float -> Get Float) -> Float -> Get Float
    forall a b. (a -> b) -> a -> b
    $ Word32 -> Float
    wordToFloat Word32
    val
      decodeWire WireField
    _ = Get Float
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire Double where
      encodeWire :: Word32 -> Double -> Put
    encodeWire Word32
    t Double
    val = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    1 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Double -> Put
    putFloat64le Double
    val

    instance DecodeWire Double where
      decodeWire :: WireField -> Get Double
    decodeWire (Fixed64Field Word32
    _ Word64
    val) = Double -> Get Double
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Double -> Get Double) -> Double -> Get Double
    forall a b. (a -> b) -> a -> b
    $ Word64 -> Double
    wordToDouble Word64
    val
      decodeWire WireField
    _ = Get Double
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire ByteString where
      encodeWire :: Word32 -> ByteString -> Put
    encodeWire Word32
    t ByteString
    val = Word32 -> Word32 -> Put
    putWireTag Word32
    t Word32
    2 Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Int -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt (ByteString -> Int
    B.length ByteString
    val) Put -> Put -> Put
    forall a b. PutM a -> PutM b -> PutM b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> ByteString -> Put
    putByteString ByteString
    val

    instance DecodeWire ByteString where
      decodeWire :: WireField -> Get ByteString
    decodeWire (DelimitedField Word32
    _ ByteString
    bs) = ByteString -> Get ByteString
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ByteString
    bs
      decodeWire WireField
    _ = Get ByteString
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    instance EncodeWire String where
      encodeWire :: Word32 -> String -> Put
    encodeWire Word32
    t = Word32 -> Text -> Put
    forall a. EncodeWire a => Word32 -> a -> Put
    encodeWire Word32
    t (Text -> Put) -> (String -> Text) -> String -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Text
    T.pack

    instance DecodeWire String where
      decodeWire :: WireField -> Get String
    decodeWire = (Text -> String) -> Get Text -> Get String
    forall a b. (a -> b) -> Get a -> Get b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Text -> String
    T.unpack (Get Text -> Get String)
    -> (WireField -> Get Text) -> WireField -> Get String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . WireField -> Get Text
    forall a. DecodeWire a => WireField -> Get a
    decodeWire

    instance EncodeWire T.Text where
      encodeWire :: Word32 -> Text -> Put
    encodeWire Word32
    t = Word32 -> ByteString -> Put
    forall a. EncodeWire a => Word32 -> a -> Put
    encodeWire Word32
    t (ByteString -> Put) -> (Text -> ByteString) -> Text -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> ByteString
    T.encodeUtf8

    instance DecodeWire T.Text where
      decodeWire :: WireField -> Get Text
    decodeWire (DelimitedField Word32
    _ ByteString
    bs) =
        case ByteString -> Either UnicodeException Text
    T.decodeUtf8' ByteString
    bs of
          Right Text
    val -> Text -> Get Text
    forall a. a -> Get a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Text
    val
          Left UnicodeException
    err  -> String -> Get Text
    forall a. String -> Get a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String -> Get Text) -> String -> Get Text
    forall a b. (a -> b) -> a -> b
    $ String
    "Decoding failed: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ UnicodeException -> String
    forall a. Show a => a -> String
    show UnicodeException
    err
      decodeWire WireField
    _ = Get Text
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    decodePackedList :: Get a -> WireField -> Get [a]
    {-# INLINE decodePackedList #-}
    decodePackedList :: forall a. Get a -> WireField -> Get [a]
    decodePackedList Get a
    g (DelimitedField Word32
    _ ByteString
    bs) =
      case Get [a] -> ByteString -> Either String [a]
    forall a. Get a -> ByteString -> Either String a
    runGet (Get a -> Get [a]
    forall a. Get a -> Get [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many Get a
    g) ByteString
    bs of
        Right [a]
    val -> [a] -> Get [a]
    forall a. a -> Get a
    forall (m :: * -> *) a. Monad m => a -> m a
    return [a]
    val
        Left String
    err  -> String -> Get [a]
    forall a. String -> Get a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail String
    err
    decodePackedList Get a
    _ WireField
    _ = Get [a]
    forall a. Get a
    forall (f :: * -> *) a. Alternative f => f a
    empty

    -- |
    -- Empty lists are not written out
    encodePackedList :: Tag -> Put -> Put
    {-# INLINE encodePackedList #-}
    encodePackedList :: Word32 -> Put -> Put
    encodePackedList Word32
    t Put
    p
      | ByteString
    bs <- Put -> ByteString
    runPut Put
    p
      , Bool -> Bool
    not (ByteString -> Bool
    B.null ByteString
    bs) = Word32 -> ByteString -> Put
    forall a. EncodeWire a => Word32 -> a -> Put
    encodeWire Word32
    t ByteString
    bs
      | Bool
    otherwise = () -> Put
    forall a. a -> PutM a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ()

    instance EncodeWire (PackedList (Value Int32)) where
      encodeWire :: Word32 -> PackedList (Value Int32) -> Put
    encodeWire Word32
    t (PackedList [Value Int32]
    xs) =
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value Int32 -> Put) -> [Value Int32] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Int32 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarSInt (Int32 -> Put) -> (Value Int32 -> Int32) -> Value Int32 -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value Int32 -> Int32
    forall a. Value a -> a
    runValue) [Value Int32]
    xs

    instance DecodeWire (PackedList (Value Int32)) where
      decodeWire :: WireField -> Get (PackedList (Value Int32))
    decodeWire WireField
    x = do
        xs <- Get Int32 -> WireField -> Get [Int32]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Int32
    forall a. (Integral a, Bits a) => Get a
    getVarInt WireField
    x
        return . PackedList $ Value <$> xs

    instance EncodeWire (PackedList (Value Int64)) where
      encodeWire :: Word32 -> PackedList (Value Int64) -> Put
    encodeWire Word32
    t (PackedList [Value Int64]
    xs) =
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value Int64 -> Put) -> [Value Int64] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Int64 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarSInt (Int64 -> Put) -> (Value Int64 -> Int64) -> Value Int64 -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value Int64 -> Int64
    forall a. Value a -> a
    runValue) [Value Int64]
    xs

    instance DecodeWire (PackedList (Value Int64)) where
      decodeWire :: WireField -> Get (PackedList (Value Int64))
    decodeWire WireField
    x = do
        xs <- Get Int64 -> WireField -> Get [Int64]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Int64
    forall a. (Integral a, Bits a) => Get a
    getVarInt WireField
    x
        return . PackedList $ Value <$> xs

    instance EncodeWire (PackedList (Value Word32)) where
      encodeWire :: Word32 -> PackedList (Value Word32) -> Put
    encodeWire Word32
    t (PackedList [Value Word32]
    xs) =
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value Word32 -> Put) -> [Value Word32] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Word32 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt (Word32 -> Put) -> (Value Word32 -> Word32) -> Value Word32 -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value Word32 -> Word32
    forall a. Value a -> a
    runValue) [Value Word32]
    xs

    instance DecodeWire (PackedList (Value Word32)) where
      decodeWire :: WireField -> Get (PackedList (Value Word32))
    decodeWire WireField
    x = do
        xs <- Get Word32 -> WireField -> Get [Word32]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Word32
    forall a. (Integral a, Bits a) => Get a
    getVarInt WireField
    x
        return . PackedList $ Value <$> xs

    instance EncodeWire (PackedList (Value Word64)) where
      encodeWire :: Word32 -> PackedList (Value Word64) -> Put
    encodeWire Word32
    t (PackedList [Value Word64]
    xs) =
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value Word64 -> Put) -> [Value Word64] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Word64 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt (Word64 -> Put) -> (Value Word64 -> Word64) -> Value Word64 -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value Word64 -> Word64
    forall a. Value a -> a
    runValue) [Value Word64]
    xs

    instance DecodeWire (PackedList (Value Word64)) where
      decodeWire :: WireField -> Get (PackedList (Value Word64))
    decodeWire WireField
    x = do
        xs <- Get Word64 -> WireField -> Get [Word64]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Word64
    forall a. (Integral a, Bits a) => Get a
    getVarInt WireField
    x
        return . PackedList $ Value <$> xs

    instance EncodeWire (PackedList (Value (Signed Int32))) where
      encodeWire :: Word32 -> PackedList (Value (Signed Int32)) -> Put
    encodeWire Word32
    t (PackedList [Value (Signed Int32)]
    xs) = do
        let c :: Signed Int32 -> Put
    c (Signed Int32
    x) = Word32 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarSInt (Word32 -> Put) -> Word32 -> Put
    forall a b. (a -> b) -> a -> b
    $ Int32 -> Word32
    zzEncode32 Int32
    x
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value (Signed Int32) -> Put) -> [Value (Signed Int32)] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Signed Int32 -> Put
    c (Signed Int32 -> Put)
    -> (Value (Signed Int32) -> Signed Int32)
    -> Value (Signed Int32)
    -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value (Signed Int32) -> Signed Int32
    forall a. Value a -> a
    runValue) [Value (Signed Int32)]
    xs

    instance DecodeWire (PackedList (Value (Signed Int32))) where
      decodeWire :: WireField -> Get (PackedList (Value (Signed Int32)))
    decodeWire WireField
    x = do
        xs <- Get Word32 -> WireField -> Get [Word32]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Word32
    forall a. (Integral a, Bits a) => Get a
    getVarInt WireField
    x
        return . PackedList $ Value . Signed . zzDecode32 <$> xs

    instance EncodeWire (PackedList (Value (Signed Int64))) where
      encodeWire :: Word32 -> PackedList (Value (Signed Int64)) -> Put
    encodeWire Word32
    t (PackedList [Value (Signed Int64)]
    xs) = do
        let c :: Signed Int64 -> Put
    c (Signed Int64
    x) = Word64 -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarSInt (Word64 -> Put) -> Word64 -> Put
    forall a b. (a -> b) -> a -> b
    $ Int64 -> Word64
    zzEncode64 Int64
    x
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value (Signed Int64) -> Put) -> [Value (Signed Int64)] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Signed Int64 -> Put
    c (Signed Int64 -> Put)
    -> (Value (Signed Int64) -> Signed Int64)
    -> Value (Signed Int64)
    -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value (Signed Int64) -> Signed Int64
    forall a. Value a -> a
    runValue) [Value (Signed Int64)]
    xs

    instance DecodeWire (PackedList (Value (Signed Int64))) where
      decodeWire :: WireField -> Get (PackedList (Value (Signed Int64)))
    decodeWire WireField
    x = do
        xs <- Get Word64 -> WireField -> Get [Word64]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Word64
    forall a. (Integral a, Bits a) => Get a
    getVarInt WireField
    x
        return . PackedList $ Value . Signed . zzDecode64 <$> xs

    instance EncodeWire (PackedList (Value (Fixed Word32))) where
      encodeWire :: Word32 -> PackedList (Value (Fixed Word32)) -> Put
    encodeWire Word32
    t (PackedList [Value (Fixed Word32)]
    xs) = do
        let c :: Fixed Word32 -> Put
    c (Fixed Word32
    x) = Word32 -> Put
    putWord32le Word32
    x
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value (Fixed Word32) -> Put) -> [Value (Fixed Word32)] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Fixed Word32 -> Put
    c (Fixed Word32 -> Put)
    -> (Value (Fixed Word32) -> Fixed Word32)
    -> Value (Fixed Word32)
    -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value (Fixed Word32) -> Fixed Word32
    forall a. Value a -> a
    runValue) [Value (Fixed Word32)]
    xs

    instance DecodeWire (PackedList (Value (Fixed Word32))) where
      decodeWire :: WireField -> Get (PackedList (Value (Fixed Word32)))
    decodeWire WireField
    x = do
        xs <- Get Word32 -> WireField -> Get [Word32]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Word32
    getWord32le WireField
    x
        return . PackedList $ Value . Fixed <$> xs

    instance EncodeWire (PackedList (Value (Fixed Word64))) where
      encodeWire :: Word32 -> PackedList (Value (Fixed Word64)) -> Put
    encodeWire Word32
    t (PackedList [Value (Fixed Word64)]
    xs) = do
        let c :: Fixed Word64 -> Put
    c (Fixed Word64
    x) = Word64 -> Put
    putWord64le Word64
    x
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value (Fixed Word64) -> Put) -> [Value (Fixed Word64)] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Fixed Word64 -> Put
    c (Fixed Word64 -> Put)
    -> (Value (Fixed Word64) -> Fixed Word64)
    -> Value (Fixed Word64)
    -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value (Fixed Word64) -> Fixed Word64
    forall a. Value a -> a
    runValue) [Value (Fixed Word64)]
    xs

    instance DecodeWire (PackedList (Value (Fixed Word64))) where
      decodeWire :: WireField -> Get (PackedList (Value (Fixed Word64)))
    decodeWire WireField
    x = do
        xs <- Get Word64 -> WireField -> Get [Word64]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Word64
    getWord64le WireField
    x
        return . PackedList $ Value . Fixed <$> xs

    instance EncodeWire (PackedList (Value (Fixed Int32))) where
      encodeWire :: Word32 -> PackedList (Value (Fixed Int32)) -> Put
    encodeWire Word32
    t (PackedList [Value (Fixed Int32)]
    xs) = do
        let c :: Fixed a -> Put
    c (Fixed a
    x) = Word32 -> Put
    putWord32le (Word32 -> Put) -> Word32 -> Put
    forall a b. (a -> b) -> a -> b
    $ a -> Word32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    x
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value (Fixed Int32) -> Put) -> [Value (Fixed Int32)] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Fixed Int32 -> Put
    forall {a}. Integral a => Fixed a -> Put
    c (Fixed Int32 -> Put)
    -> (Value (Fixed Int32) -> Fixed Int32)
    -> Value (Fixed Int32)
    -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value (Fixed Int32) -> Fixed Int32
    forall a. Value a -> a
    runValue) [Value (Fixed Int32)]
    xs

    instance DecodeWire (PackedList (Value (Fixed Int32))) where
      decodeWire :: WireField -> Get (PackedList (Value (Fixed Int32)))
    decodeWire WireField
    x = do
        xs <- Get Word32 -> WireField -> Get [Word32]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Word32
    getWord32le WireField
    x
        return . PackedList $ Value . Fixed . fromIntegral <$> xs

    instance EncodeWire (PackedList (Value (Fixed Int64))) where
      encodeWire :: Word32 -> PackedList (Value (Fixed Int64)) -> Put
    encodeWire Word32
    t (PackedList [Value (Fixed Int64)]
    xs) = do
        let c :: Fixed a -> Put
    c (Fixed a
    x) = Word64 -> Put
    putWord64le (Word64 -> Put) -> Word64 -> Put
    forall a b. (a -> b) -> a -> b
    $ a -> Word64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    x
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value (Fixed Int64) -> Put) -> [Value (Fixed Int64)] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Fixed Int64 -> Put
    forall {a}. Integral a => Fixed a -> Put
    c (Fixed Int64 -> Put)
    -> (Value (Fixed Int64) -> Fixed Int64)
    -> Value (Fixed Int64)
    -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value (Fixed Int64) -> Fixed Int64
    forall a. Value a -> a
    runValue) [Value (Fixed Int64)]
    xs

    instance DecodeWire (PackedList (Value (Fixed Int64))) where
      decodeWire :: WireField -> Get (PackedList (Value (Fixed Int64)))
    decodeWire WireField
    x = do
        xs <- Get Word64 -> WireField -> Get [Word64]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Word64
    getWord64le WireField
    x
        return . PackedList $ Value . Fixed . fromIntegral <$> xs

    instance EncodeWire (PackedList (Value Float)) where
      encodeWire :: Word32 -> PackedList (Value Float) -> Put
    encodeWire Word32
    t (PackedList [Value Float]
    xs) =
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value Float -> Put) -> [Value Float] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Float -> Put
    putFloat32le (Float -> Put) -> (Value Float -> Float) -> Value Float -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value Float -> Float
    forall a. Value a -> a
    runValue) [Value Float]
    xs

    instance DecodeWire (PackedList (Value Float)) where
      decodeWire :: WireField -> Get (PackedList (Value Float))
    decodeWire WireField
    x = do
        xs <- Get Float -> WireField -> Get [Float]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Float
    getFloat32le WireField
    x
        return . PackedList $ Value <$> xs

    instance EncodeWire (PackedList (Value Double)) where
      encodeWire :: Word32 -> PackedList (Value Double) -> Put
    encodeWire Word32
    t (PackedList [Value Double]
    xs) =
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value Double -> Put) -> [Value Double] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Double -> Put
    putFloat64le (Double -> Put) -> (Value Double -> Double) -> Value Double -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value Double -> Double
    forall a. Value a -> a
    runValue) [Value Double]
    xs

    instance DecodeWire (PackedList (Value Double)) where
      decodeWire :: WireField -> Get (PackedList (Value Double))
    decodeWire WireField
    x = do
        xs <- Get Double -> WireField -> Get [Double]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Double
    getFloat64le WireField
    x
        return . PackedList $ Value <$> xs

    instance EncodeWire (PackedList (Value Bool)) where
      encodeWire :: Word32 -> PackedList (Value Bool) -> Put
    encodeWire Word32
    t (PackedList [Value Bool]
    xs) =
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Value Bool -> Put) -> [Value Bool] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Int -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt (Int -> Put) -> (Value Bool -> Int) -> Value Bool -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Value Bool -> Int
    forall a. Enum a => a -> Int
    fromEnum) [Value Bool]
    xs

    instance DecodeWire (PackedList (Value Bool)) where
      decodeWire :: WireField -> Get (PackedList (Value Bool))
    decodeWire WireField
    x = do
        xs <- Get Int -> WireField -> Get [Int]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Int
    forall a. (Integral a, Bits a) => Get a
    getVarInt WireField
    x
        return . PackedList $ toEnum <$> xs

    instance Enum a => EncodeWire (PackedList (Enumeration a)) where
      encodeWire :: Word32 -> PackedList (Enumeration a) -> Put
    encodeWire Word32
    t (PackedList [Enumeration a]
    xs) =
        Word32 -> Put -> Put
    encodePackedList Word32
    t (Put -> Put) -> Put -> Put
    forall a b. (a -> b) -> a -> b
    $ (Enumeration a -> Put) -> [Enumeration a] -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Int -> Put
    forall a. (Integral a, Bits a) => a -> Put
    putVarUInt (Int -> Put) -> (Enumeration a -> Int) -> Enumeration a -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Enumeration a -> Int
    forall a. Enum a => a -> Int
    fromEnum) [Enumeration a]
    xs

    instance Enum a => DecodeWire (PackedList (Enumeration a)) where
      decodeWire :: WireField -> Get (PackedList (Enumeration a))
    decodeWire WireField
    x = do
        xs <- Get Int -> WireField -> Get [Int]
    forall a. Get a -> WireField -> Get [a]
    decodePackedList Get Int
    forall a. (Integral a, Bits a) => Get a
    getVarInt WireField
    x
        return . PackedList $ toEnum <$> xs

    instance (Foldable f, Enum a) => EncodeWire (f (Enumeration a)) where
      encodeWire :: Word32 -> f (Enumeration a) -> Put
    encodeWire Word32
    t = (Enumeration a -> Put) -> f (Enumeration a) -> Put
    forall (t :: * -> *) (f :: * -> *) a b.
    (Foldable t, Applicative f) =>
    (a -> f b) -> t a -> f ()
    traverse_ (Word32 -> Int32 -> Put
    forall a. EncodeWire a => Word32 -> a -> Put
    encodeWire Word32
    t (Int32 -> Put) -> (Enumeration a -> Int32) -> Enumeration a -> Put
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Int32
    c (a -> Int32) -> (Enumeration a -> a) -> Enumeration a -> Int32
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Enumeration a -> a
    forall a. Enumeration a -> a
    runEnumeration) where
        c :: a -> Int32
        c :: a -> Int32
    c = Int -> Int32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Int32) -> (a -> Int) -> a -> Int32
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Int
    forall a. Enum a => a -> Int
    fromEnum

    instance Enum a => DecodeWire (Enumeration a) where
      decodeWire :: WireField -> Get (Enumeration a)
    decodeWire WireField
    f = Int32 -> Enumeration a
    c (Int32 -> Enumeration a) -> Get Int32 -> Get (Enumeration a)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> WireField -> Get Int32
    forall a. DecodeWire a => WireField -> Get a
    decodeWire WireField
    f where
        c :: Int32 -> Enumeration a
        c :: Int32 -> Enumeration a
    c = a -> Enumeration a
    forall a. a -> Enumeration a
    Enumeration (a -> Enumeration a) -> (Int32 -> a) -> Int32 -> Enumeration a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> a
    forall a. Enum a => Int -> a
    toEnum (Int -> a) -> (Int32 -> Int) -> Int32 -> a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int32 -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral

    instance Enum a => DecodeWire (Maybe (Enumeration a)) where
      decodeWire :: WireField -> Get (Maybe (Enumeration a))
    decodeWire WireField
    f = Int32 -> Maybe (Enumeration a)
    c (Int32 -> Maybe (Enumeration a))
    -> Get Int32 -> Get (Maybe (Enumeration a))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> WireField -> Get Int32
    forall a. DecodeWire a => WireField -> Get a
    decodeWire WireField
    f where
        c :: Int32 -> Maybe (Enumeration a)
        c :: Int32 -> Maybe (Enumeration a)
    c = Enumeration a -> Maybe (Enumeration a)
    forall a. a -> Maybe a
    Just (Enumeration a -> Maybe (Enumeration a))
    -> (Int32 -> Enumeration a) -> Int32 -> Maybe (Enumeration a)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Enumeration a
    forall a. a -> Enumeration a
    Enumeration (a -> Enumeration a) -> (Int32 -> a) -> Int32 -> Enumeration a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> a
    forall a. Enum a => Int -> a
    toEnum (Int -> a) -> (Int32 -> Int) -> Int32 -> a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int32 -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral

    instance Enum a => DecodeWire (Always (Enumeration a)) where
      decodeWire :: WireField -> Get (Always (Enumeration a))
    decodeWire WireField
    f = Int32 -> Always (Enumeration a)
    c (Int32 -> Always (Enumeration a))
    -> Get Int32 -> Get (Always (Enumeration a))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> WireField -> Get Int32
    forall a. DecodeWire a => WireField -> Get a
    decodeWire WireField
    f where
        c :: Int32 -> Always (Enumeration a)
        c :: Int32 -> Always (Enumeration a)
    c = Enumeration a -> Always (Enumeration a)
    forall a. a -> Always a
    Always (Enumeration a -> Always (Enumeration a))
    -> (Int32 -> Enumeration a) -> Int32 -> Always (Enumeration a)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Enumeration a
    forall a. a -> Enumeration a
    Enumeration (a -> Enumeration a) -> (Int32 -> a) -> Int32 -> Enumeration a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> a
    forall a. Enum a => Int -> a
    toEnum (Int -> a) -> (Int32 -> Int) -> Int32 -> a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int32 -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral

    -- Taken from google's code, but I had to explcitly add fromIntegral in the right places:
    zzEncode32 :: Int32 -> Word32
    zzEncode32 :: Int32 -> Word32
    zzEncode32 Int32
    x = Int32 -> Word32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral ((Int32
    x Int32 -> Int -> Int32
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    1) Int32 -> Int32 -> Int32
    forall a. Bits a => a -> a -> a
    `xor` Int32
    x Int32 -> Int -> Int32
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    31)
    zzEncode64 :: Int64 -> Word64
    zzEncode64 :: Int64 -> Word64
    zzEncode64 Int64
    x = Int64 -> Word64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral ((Int64
    x Int64 -> Int -> Int64
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    1) Int64 -> Int64 -> Int64
    forall a. Bits a => a -> a -> a
    `xor` Int64
    x Int64 -> Int -> Int64
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    63)
    zzDecode32 :: Word32 -> Int32
    zzDecode32 :: Word32 -> Int32
    zzDecode32 Word32
    w = Word32 -> Int32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Word32
    w Word32 -> Int -> Word32
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    1) Int32 -> Int32 -> Int32
    forall a. Bits a => a -> a -> a
    `xor` Int32 -> Int32
    forall a. Num a => a -> a
    negate (Word32 -> Int32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Word32
    w Word32 -> Word32 -> Word32
    forall a. Bits a => a -> a -> a
    .&. Word32
    1))
    zzDecode64 :: Word64 -> Int64
    zzDecode64 :: Word64 -> Int64
    zzDecode64 Word64
    w = Word64 -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Word64
    w Word64 -> Int -> Word64
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    1) Int64 -> Int64 -> Int64
    forall a. Bits a => a -> a -> a
    `xor` Int64 -> Int64
    forall a. Num a => a -> a
    negate (Word64 -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Word64
    w Word64 -> Word64 -> Word64
    forall a. Bits a => a -> a -> a
    .&. Word64
    1))

    legacyFail :: Monad m => String -> m a
    #if __GLASGOW_HASKELL__ <= 710
    legacyFail = fail
    #else
    legacyFail :: forall (m :: * -> *) a. Monad m => String -> m a
    legacyFail = String -> m a
    forall a. String -> a
    errorWithoutStackTrace
    #endif