{-# LANGUAGE BangPatterns        #-}
{-# LANGUAGE CPP                 #-}
{-# LANGUAGE DefaultSignatures   #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE PolyKinds           #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE TypeOperators       #-}

-- |
-- Module      : Codec.Serialise.Class
-- Copyright   : (c) Duncan Coutts 2015-2017
-- License     : BSD3-style (see LICENSE.txt)
--
-- Maintainer  : duncan@community.haskell.org
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--
-- The @'Serialise'@ class allows you to encode a given type into a
-- CBOR object, or decode a CBOR object into the user-specified type.
--
module Codec.Serialise.Class
 ( -- * The Serialise class
   Serialise(..)
 , GSerialiseEncode(..)
 , GSerialiseDecode(..)
 , GSerialiseProd(..)
 , GSerialiseSum(..)
 , encodeVector
 , decodeVector
 , encodeContainerSkel
 , encodeMapSkel
 , decodeMapSkel
 ) where

import           Control.Applicative

import           Control.Monad
import           Data.Char
import           Data.Hashable
import           Data.Int
import           Data.Monoid
import           Data.Proxy
import           Data.Version
import           Data.Word
import           Data.Complex
import           Data.Fixed
import           Data.Ratio
import           Data.Ord

#if MIN_VERSION_base(4,8,0)
import           Numeric.Natural
import           Data.Functor.Identity
import           Data.Void                           (Void, absurd)
#endif

#if MIN_VERSION_base(4,9,0)
import qualified Data.Semigroup                      as Semigroup
import qualified Data.List.NonEmpty                  as NonEmpty
#endif

import qualified Data.Foldable                       as Foldable
import qualified Data.ByteString                     as BS
import qualified Data.ByteString.Short.Internal      as BSS
import qualified Data.Text                           as Text

-- TODO FIXME: more instances
--import qualified Data.Array                          as Array
--import qualified Data.Array.Unboxed                  as UArray
import qualified Data.ByteString.Lazy                as BS.Lazy
import qualified Data.Map                            as Map
import qualified Data.Sequence                       as Sequence
import qualified Data.Set                            as Set
import qualified Data.Strict                         as Strict
import qualified Data.IntSet                         as IntSet
import qualified Data.IntMap                         as IntMap
import qualified Data.HashSet                        as HashSet
import qualified Data.HashMap.Strict                 as HashMap
import qualified Data.These                          as These
import qualified Data.Tree                           as Tree
import qualified Data.Primitive.ByteArray            as Prim
import qualified Data.Vector                         as Vector
import qualified Data.Vector.Unboxed                 as Vector.Unboxed
import qualified Data.Vector.Storable                as Vector.Storable
import qualified Data.Vector.Primitive               as Vector.Primitive
import qualified Data.Vector.Generic                 as Vector.Generic
import qualified Data.Text.Lazy                      as Text.Lazy
import           Foreign.C.Types
import qualified Numeric.Half                        as Half

import           Data.Time                           (UTCTime (..), addUTCTime)
import           Data.Time.Calendar                  (fromGregorian)
import           Data.Time.Clock.POSIX               (POSIXTime, utcTimeToPOSIXSeconds,
                                                      posixSecondsToUTCTime)
#if MIN_VERSION_time(1,5,0)
import           Data.Time.Format                    (defaultTimeLocale, parseTimeM)
#else
import           Data.Time.Format                    (parseTime)
import           System.Locale                       (defaultTimeLocale)
#endif
import           System.Exit                         (ExitCode(..))

import           Prelude hiding (decodeFloat, encodeFloat, foldr)
import qualified Prelude
#if MIN_VERSION_base(4,16,0)
import           GHC.Exts (Levity(..))
#endif
#if MIN_VERSION_base(4,10,0)
import           Type.Reflection
import           Type.Reflection.Unsafe
import           GHC.Fingerprint
import           GHC.Exts (VecCount(..), VecElem(..), RuntimeRep(..))
import           Data.Kind (Type)
#else
import           Data.Typeable.Internal
#endif
import           GHC.Generics

import           Codec.CBOR.Decoding
import           Codec.CBOR.Encoding
import           Codec.CBOR.Term
import           Codec.Serialise.Internal.GeneralisedUTF8
import qualified Codec.CBOR.ByteArray                as BA
import qualified Codec.CBOR.ByteArray.Sliced         as BAS


--------------------------------------------------------------------------------
-- The Serialise class

-- | Types that are instances of the @'Serialise'@ class allow values
-- to be quickly encoded or decoded directly to a CBOR representation,
-- for object transmission or storage.
--
-- @since 0.2.0.0
class Serialise a where
    -- | Definition for encoding a given type into a binary
    -- representation, using the @'Encoding'@ @'Monoid'@.
    --
    -- @since 0.2.0.0
    encode  :: a -> Encoding
    default encode :: (Generic a, GSerialiseEncode (Rep a)) => a -> Encoding
    encode = forall {k} (f :: k -> *) (a :: k).
GSerialiseEncode f =>
f a -> Encoding
gencode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a x. Generic a => a -> Rep a x
from

    -- | Definition of a given @'Decoder'@ for a type.
    --
    -- @since 0.2.0.0
    decode  :: Decoder s a
    default decode :: (Generic a, GSerialiseDecode (Rep a)) => Decoder s a
    decode = forall a x. Generic a => Rep a x -> a
to forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {k} (f :: k -> *) s (a :: k).
GSerialiseDecode f =>
Decoder s (f a)
gdecode

    -- | Utility to support specialised encoding for some list type -
    -- used for @'Char'@/@'String'@ instances in this package.
    --
    -- @since 0.2.0.0
    encodeList :: [a] -> Encoding
    encodeList = forall a. Serialise a => [a] -> Encoding
defaultEncodeList

    -- | Utility to support specialised decoding for some list type -
    -- used for @'Char'@/@'String'@ instances in this package.
    --
    -- @since 0.2.0.0
    decodeList :: Decoder s [a]
    decodeList = forall a s. Serialise a => Decoder s [a]
defaultDecodeList

-- | @since 0.2.0.0
instance Serialise Term where
  encode :: Term -> Encoding
encode = Term -> Encoding
encodeTerm
  decode :: forall s. Decoder s Term
decode = forall s. Decoder s Term
decodeTerm

--------------------------------------------------------------------------------
-- Special list business

-- | @since 0.2.0.0
instance Serialise a => Serialise [a] where
    encode :: [a] -> Encoding
encode = forall a. Serialise a => [a] -> Encoding
encodeList
    decode :: forall s. Decoder s [a]
decode = forall a s. Serialise a => Decoder s [a]
decodeList

-- | Default @'Encoding'@ for list types.
--
-- @since 0.2.0.0
defaultEncodeList :: Serialise a => [a] -> Encoding
defaultEncodeList :: forall a. Serialise a => [a] -> Encoding
defaultEncodeList [] = Word -> Encoding
encodeListLen Word
0
defaultEncodeList [a]
xs = Encoding
encodeListLenIndef
                    forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr (\a
x Encoding
r -> forall a. Serialise a => a -> Encoding
encode a
x forall a. Semigroup a => a -> a -> a
<> Encoding
r) Encoding
encodeBreak [a]
xs

-- | Default @'Decoder'@ for list types.
--
-- @since 0.2.0.0
defaultDecodeList :: Serialise a => Decoder s [a]
defaultDecodeList :: forall a s. Serialise a => Decoder s [a]
defaultDecodeList = do
    Maybe Int
mn <- forall s. Decoder s (Maybe Int)
decodeListLenOrIndef
    case Maybe Int
mn of
      Maybe Int
Nothing -> forall r a r' s.
(r -> a -> r) -> r -> (r -> r') -> Decoder s a -> Decoder s r'
decodeSequenceLenIndef (forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [] forall a. [a] -> [a]
reverse   forall a s. Serialise a => Decoder s a
decode
      Just Int
n  -> forall r a r' s.
(r -> a -> r)
-> r -> (r -> r') -> Int -> Decoder s a -> Decoder s r'
decodeSequenceLenN     (forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [] forall a. [a] -> [a]
reverse Int
n forall a s. Serialise a => Decoder s a
decode

--------------------------------------------------------------------------------
-- Another case: NonEmpty lists

#if MIN_VERSION_base(4,9,0)
-- | @since 0.2.0.0
instance Serialise a => Serialise (NonEmpty.NonEmpty a) where
  encode :: NonEmpty a -> Encoding
encode = forall a. Serialise a => [a] -> Encoding
defaultEncodeList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NonEmpty a -> [a]
NonEmpty.toList
  decode :: forall s. Decoder s (NonEmpty a)
decode = do
    [a]
l <- forall a s. Serialise a => Decoder s [a]
defaultDecodeList
    case forall a. [a] -> Maybe (NonEmpty a)
NonEmpty.nonEmpty [a]
l of
      Maybe (NonEmpty a)
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected a NonEmpty list, but an empty list was found!"
      Just NonEmpty a
xs -> forall (m :: * -> *) a. Monad m => a -> m a
return NonEmpty a
xs
#endif

--------------------------------------------------------------------------------
-- Primitive and integral instances

#if MIN_VERSION_base(4,8,0)
-- | @since 0.2.4.0
instance Serialise Void where
    encode :: Void -> Encoding
encode = forall a. Void -> a
absurd
    decode :: forall s. Decoder s Void
decode = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"tried to decode void"
#endif

-- | @since 0.2.0.0
instance Serialise () where
    encode :: () -> Encoding
encode = forall a b. a -> b -> a
const Encoding
encodeNull
    decode :: forall s. Decoder s ()
decode = forall s. Decoder s ()
decodeNull

-- | @since 0.2.0.0
instance Serialise Bool where
    encode :: Bool -> Encoding
encode = Bool -> Encoding
encodeBool
    decode :: forall s. Decoder s Bool
decode = forall s. Decoder s Bool
decodeBool

-- | @since 0.2.0.0
instance Serialise Int where
    encode :: Int -> Encoding
encode = Int -> Encoding
encodeInt
    decode :: forall s. Decoder s Int
decode = forall s. Decoder s Int
decodeInt

-- | @since 0.2.0.0
instance Serialise Int8 where
    encode :: Int8 -> Encoding
encode = Int8 -> Encoding
encodeInt8
    decode :: forall s. Decoder s Int8
decode = forall s. Decoder s Int8
decodeInt8

-- | @since 0.2.0.0
instance Serialise Int16 where
    encode :: Int16 -> Encoding
encode = Int16 -> Encoding
encodeInt16
    decode :: forall s. Decoder s Int16
decode = forall s. Decoder s Int16
decodeInt16

-- | @since 0.2.0.0
instance Serialise Int32 where
    encode :: Int32 -> Encoding
encode = Int32 -> Encoding
encodeInt32
    decode :: forall s. Decoder s Int32
decode = forall s. Decoder s Int32
decodeInt32

-- | @since 0.2.0.0
instance Serialise Int64 where
    encode :: Int64 -> Encoding
encode = Int64 -> Encoding
encodeInt64
    decode :: forall s. Decoder s Int64
decode = forall s. Decoder s Int64
decodeInt64

-- | @since 0.2.0.0
instance Serialise Word where
    encode :: Word -> Encoding
encode = Word -> Encoding
encodeWord
    decode :: forall s. Decoder s Word
decode = forall s. Decoder s Word
decodeWord

-- | @since 0.2.0.0
instance Serialise Word8 where
    encode :: Word8 -> Encoding
encode = Word8 -> Encoding
encodeWord8
    decode :: forall s. Decoder s Word8
decode = forall s. Decoder s Word8
decodeWord8

-- | @since 0.2.0.0
instance Serialise Word16 where
    encode :: Word16 -> Encoding
encode = Word16 -> Encoding
encodeWord16
    decode :: forall s. Decoder s Word16
decode = forall s. Decoder s Word16
decodeWord16

-- | @since 0.2.0.0
instance Serialise Word32 where
    encode :: Word32 -> Encoding
encode = Word32 -> Encoding
encodeWord32
    decode :: forall s. Decoder s Word32
decode = forall s. Decoder s Word32
decodeWord32

-- | @since 0.2.0.0
instance Serialise Word64 where
    encode :: Word64 -> Encoding
encode = Word64 -> Encoding
encodeWord64
    decode :: forall s. Decoder s Word64
decode = forall s. Decoder s Word64
decodeWord64

-- | @since 0.2.0.0
instance Serialise Integer where
    encode :: Integer -> Encoding
encode = Integer -> Encoding
encodeInteger
    decode :: forall s. Decoder s Integer
decode = forall s. Decoder s Integer
decodeInteger

#if MIN_VERSION_base(4,8,0)
-- | @since 0.2.0.0
instance Serialise Natural where
    encode :: Natural -> Encoding
encode = Integer -> Encoding
encodeInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger
    decode :: forall s. Decoder s Natural
decode = do
      Integer
n <- forall s. Decoder s Integer
decodeInteger
      if Integer
n forall a. Ord a => a -> a -> Bool
>= Integer
0
        then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Num a => Integer -> a
fromInteger Integer
n)
        else forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected non-negative Natural; but got a negative number"
#endif

-- | @since 0.2.0.0
instance Serialise Float where
    encode :: Float -> Encoding
encode = Float -> Encoding
encodeFloat
    decode :: forall s. Decoder s Float
decode = forall s. Decoder s Float
decodeFloat

-- | @since 0.2.0.0
instance Serialise Double where
    encode :: Double -> Encoding
encode = Double -> Encoding
encodeDouble
    decode :: forall s. Decoder s Double
decode = forall s. Decoder s Double
decodeDouble

-- | @since 0.2.0.0
instance Serialise Half.Half where
    encode :: Half -> Encoding
encode = Float -> Encoding
encodeFloat16 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Half -> Float
Half.fromHalf
    decode :: forall s. Decoder s Half
decode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Float -> Half
Half.toHalf forall s. Decoder s Float
decodeFloat

--------------------------------------------------------------------------------
-- Core types

#if MIN_VERSION_base(4,7,0)
-- | Values are serialised in units of least precision represented as
--   @Integer@.
--
-- @since 0.2.0.0
instance Serialise (Fixed e) where
    encode :: Fixed e -> Encoding
encode (MkFixed Integer
i) = forall a. Serialise a => a -> Encoding
encode Integer
i
    decode :: forall s. Decoder s (Fixed e)
decode = forall k (a :: k). Integer -> Fixed a
MkFixed forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise (Proxy a) where
    encode :: Proxy a -> Encoding
encode Proxy a
_ = Encoding
encodeNull
    decode :: forall s. Decoder s (Proxy a)
decode   = forall {k} (t :: k). Proxy t
Proxy forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall s. Decoder s ()
decodeNull
#endif

-- | @since 0.2.0.0
instance Serialise Char where
    -- Here we've taken great pains to ensure that surrogate characters, which
    -- are not representable in UTF-8 yet still admitted by Char,
    -- round-trip properly. We scan the encoded characters during encoding
    -- looking for surrogates; if we find any we encode the string as a
    -- a list of code-points encoded as words. This is slow, but should be rare.
    encode :: Char -> Encoding
encode Char
c
      | Char -> Bool
isSurrogate Char
c = Word -> Encoding
encodeWord (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Char -> Int
ord Char
c)
      | Bool
otherwise     = Text -> Encoding
encodeString (Char -> Text
Text.singleton Char
c)
    decode :: forall s. Decoder s Char
decode = do TokenType
ty <- forall s. Decoder s TokenType
peekTokenType
                case TokenType
ty of
                  TokenType
TypeUInt -> Int -> Char
chr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Word
decodeWord
                  TokenType
TypeString -> do
                    Text
t <- forall s. Decoder s Text
decodeString
                    if Text -> Int
Text.length Text
t forall a. Eq a => a -> a -> Bool
== Int
1
                      then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! Text -> Char
Text.head Text
t
                      else forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected a single char, found a string"
                  TokenType
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected a word or string"

    -- For [Char]/String we have a special encoding
    encodeList :: String -> Encoding
encodeList String
cs =
        case String -> (SlicedByteArray, UTF8Encoding)
encodeGenUTF8 String
cs of
          (SlicedByteArray
ba, UTF8Encoding
ConformantUTF8)  -> SlicedByteArray -> Encoding
encodeUtf8ByteArray SlicedByteArray
ba
          (SlicedByteArray
ba, UTF8Encoding
GeneralisedUTF8) -> SlicedByteArray -> Encoding
encodeByteArray SlicedByteArray
ba
    decodeList :: forall s. Decoder s String
decodeList    = do
        TokenType
ty <- forall s. Decoder s TokenType
peekTokenType
        case TokenType
ty of
          TokenType
TypeBytes  -> ByteArray -> String
decodeGenUTF8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteArray -> ByteArray
BA.unBA forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s ByteArray
decodeByteArray
          TokenType
TypeString -> do
              Text
txt <- forall s. Decoder s Text
decodeString
              forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> String
Text.unpack Text
txt) -- unpack lazily
          TokenType
_          -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected a list or string"

-- | @since 0.2.0.0
instance Serialise Text.Text where
    encode :: Text -> Encoding
encode = Text -> Encoding
encodeString
    decode :: forall s. Decoder s Text
decode = forall s. Decoder s Text
decodeString

-- | @since 0.2.0.0
instance Serialise BS.ByteString where
    encode :: ByteString -> Encoding
encode = ByteString -> Encoding
encodeBytes
    decode :: forall s. Decoder s ByteString
decode = forall s. Decoder s ByteString
decodeBytes

-- | @since 0.2.0.0
instance Serialise BSS.ShortByteString where
    encode :: ShortByteString -> Encoding
encode sbs :: ShortByteString
sbs@(BSS.SBS ByteArray#
ba) =
        SlicedByteArray -> Encoding
encodeByteArray forall a b. (a -> b) -> a -> b
$ ByteArray -> Int -> Int -> SlicedByteArray
BAS.SBA (ByteArray# -> ByteArray
Prim.ByteArray ByteArray#
ba) Int
0 (ShortByteString -> Int
BSS.length ShortByteString
sbs)
    decode :: forall s. Decoder s ShortByteString
decode = do
        BA.BA (Prim.ByteArray ByteArray#
ba) <- forall s. Decoder s ByteArray
decodeByteArray
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ByteArray# -> ShortByteString
BSS.SBS ByteArray#
ba

encodeChunked :: Serialise c
              => Encoding
              -> ((c -> Encoding -> Encoding) -> Encoding -> a -> Encoding)
              -> a
              -> Encoding
encodeChunked :: forall c a.
Serialise c =>
Encoding
-> ((c -> Encoding -> Encoding) -> Encoding -> a -> Encoding)
-> a
-> Encoding
encodeChunked Encoding
encodeIndef (c -> Encoding -> Encoding) -> Encoding -> a -> Encoding
foldrChunks a
a =
    Encoding
encodeIndef
 forall a. Semigroup a => a -> a -> a
<> (c -> Encoding -> Encoding) -> Encoding -> a -> Encoding
foldrChunks (\c
x Encoding
r -> forall a. Serialise a => a -> Encoding
encode c
x forall a. Semigroup a => a -> a -> a
<> Encoding
r) Encoding
encodeBreak a
a

decodeChunked :: Serialise c => Decoder s () -> ([c] -> a) -> Decoder s a
decodeChunked :: forall c s a.
Serialise c =>
Decoder s () -> ([c] -> a) -> Decoder s a
decodeChunked Decoder s ()
decodeIndef [c] -> a
fromChunks = do
  Decoder s ()
decodeIndef
  forall r a r' s.
(r -> a -> r) -> r -> (r -> r') -> Decoder s a -> Decoder s r'
decodeSequenceLenIndef (forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [] ([c] -> a
fromChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse) forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise Text.Lazy.Text where
    encode :: Text -> Encoding
encode = forall c a.
Serialise c =>
Encoding
-> ((c -> Encoding -> Encoding) -> Encoding -> a -> Encoding)
-> a
-> Encoding
encodeChunked Encoding
encodeStringIndef forall a. (Text -> a -> a) -> a -> Text -> a
Text.Lazy.foldrChunks
    decode :: forall s. Decoder s Text
decode = forall c s a.
Serialise c =>
Decoder s () -> ([c] -> a) -> Decoder s a
decodeChunked forall s. Decoder s ()
decodeStringIndef [Text] -> Text
Text.Lazy.fromChunks

-- | @since 0.2.0.0
instance Serialise BS.Lazy.ByteString where
    encode :: ByteString -> Encoding
encode = forall c a.
Serialise c =>
Encoding
-> ((c -> Encoding -> Encoding) -> Encoding -> a -> Encoding)
-> a
-> Encoding
encodeChunked Encoding
encodeBytesIndef forall a. (ByteString -> a -> a) -> a -> ByteString -> a
BS.Lazy.foldrChunks
    decode :: forall s. Decoder s ByteString
decode = forall c s a.
Serialise c =>
Decoder s () -> ([c] -> a) -> Decoder s a
decodeChunked forall s. Decoder s ()
decodeBytesIndef [ByteString] -> ByteString
BS.Lazy.fromChunks

-- | @since 0.2.0.0
instance Serialise a => Serialise (Const a b) where
    encode :: Const a b -> Encoding
encode (Const a
a) = forall a. Serialise a => a -> Encoding
encode a
a
    decode :: forall s. Decoder s (Const a b)
decode = forall {k} a (b :: k). a -> Const a b
Const forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (ZipList a) where
    encode :: ZipList a -> Encoding
encode (ZipList [a]
xs) = forall a. Serialise a => a -> Encoding
encode [a]
xs
    decode :: forall s. Decoder s (ZipList a)
decode = forall a. [a] -> ZipList a
ZipList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance (Serialise a, Integral a) => Serialise (Ratio a) where
    encode :: Ratio a -> Encoding
encode Ratio a
a = Word -> Encoding
encodeListLen Word
2
            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode (forall a. Ratio a -> a
numerator Ratio a
a)
            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode (forall a. Ratio a -> a
denominator Ratio a
a)
    decode :: forall s. Decoder s (Ratio a)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
2
                !a
a <- forall a s. Serialise a => Decoder s a
decode
                !a
b <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ a
a forall a. Integral a => a -> a -> Ratio a
% a
b

-- | @since 0.2.0.0
instance Serialise a => Serialise (Complex a) where
    encode :: Complex a -> Encoding
encode (a
r :+ a
i) = Word -> Encoding
encodeListLen Word
2
                   forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
r
                   forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
i
    decode :: forall s. Decoder s (Complex a)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
2
                !a
r <- forall a s. Serialise a => Decoder s a
decode
                !a
i <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ a
r forall a. a -> a -> Complex a
:+ a
i

-- | @since 0.2.0.0
instance Serialise Ordering where
    encode :: Ordering -> Encoding
encode Ordering
a = Word -> Encoding
encodeListLen Word
1
            forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord (case Ordering
a of Ordering
LT -> Word
0
                                     Ordering
EQ -> Word
1
                                     Ordering
GT -> Word
2)
    decode :: forall s. Decoder s Ordering
decode = do
      forall s. Int -> Decoder s ()
decodeListLenOf Int
1
      Word
t <- forall s. Decoder s Word
decodeWord
      case Word
t of
        Word
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
LT
        Word
1 -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
EQ
        Word
2 -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
GT
        Word
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unexpected tag"

-- | @since 0.2.0.0
instance Serialise a => Serialise (Down a) where
    encode :: Down a -> Encoding
encode (Down a
a) = forall a. Serialise a => a -> Encoding
encode a
a
    decode :: forall s. Decoder s (Down a)
decode = forall a. a -> Down a
Down forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (Dual a) where
    encode :: Dual a -> Encoding
encode (Dual a
a) = forall a. Serialise a => a -> Encoding
encode a
a
    decode :: forall s. Decoder s (Dual a)
decode = forall a. a -> Dual a
Dual forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise All where
    encode :: All -> Encoding
encode (All Bool
b) = forall a. Serialise a => a -> Encoding
encode Bool
b
    decode :: forall s. Decoder s All
decode = Bool -> All
All forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise Any where
    encode :: Any -> Encoding
encode (Any Bool
b) = forall a. Serialise a => a -> Encoding
encode Bool
b
    decode :: forall s. Decoder s Any
decode = Bool -> Any
Any forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (Sum a) where
    encode :: Sum a -> Encoding
encode (Sum a
b) = forall a. Serialise a => a -> Encoding
encode a
b
    decode :: forall s. Decoder s (Sum a)
decode = forall a. a -> Sum a
Sum forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (Product a) where
    encode :: Product a -> Encoding
encode (Product a
b) = forall a. Serialise a => a -> Encoding
encode a
b
    decode :: forall s. Decoder s (Product a)
decode = forall a. a -> Product a
Product forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (First a) where
    encode :: First a -> Encoding
encode (First Maybe a
b) = forall a. Serialise a => a -> Encoding
encode Maybe a
b
    decode :: forall s. Decoder s (First a)
decode = forall a. Maybe a -> First a
First forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (Last a) where
    encode :: Last a -> Encoding
encode (Last Maybe a
b) = forall a. Serialise a => a -> Encoding
encode Maybe a
b
    decode :: forall s. Decoder s (Last a)
decode = forall a. Maybe a -> Last a
Last forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

#if MIN_VERSION_base(4,8,0)
-- | @since 0.2.0.0
instance Serialise (f a) => Serialise (Alt f a) where
    encode :: Alt f a -> Encoding
encode (Alt f a
b) = forall a. Serialise a => a -> Encoding
encode f a
b
    decode :: forall s. Decoder s (Alt f a)
decode = forall {k} (f :: k -> *) (a :: k). f a -> Alt f a
Alt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (Identity a) where
    encode :: Identity a -> Encoding
encode (Identity a
b) = forall a. Serialise a => a -> Encoding
encode a
b
    decode :: forall s. Decoder s (Identity a)
decode = forall a. a -> Identity a
Identity forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode
#endif

-- | @since 0.2.0.0
instance Serialise ExitCode where
    encode :: ExitCode -> Encoding
encode ExitCode
ExitSuccess     = Word -> Encoding
encodeListLen Word
1
                          forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0
    encode (ExitFailure Int
i) = Word -> Encoding
encodeListLen Word
2
                          forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
1
                          forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode Int
i
    decode :: forall s. Decoder s ExitCode
decode = do
      Int
n <- forall s. Decoder s Int
decodeListLen
      case Int
n of
        Int
1 -> do Word
t <- forall s. Decoder s Word
decodeWord
                case Word
t of
                  Word
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return ExitCode
ExitSuccess
                  Word
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unexpected tag"
        Int
2 -> do Word
t <- forall s. Decoder s Word
decodeWord
                case Word
t of
                  Word
1 -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
                  Word
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unexpected tag"
                !Int
i <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Int -> ExitCode
ExitFailure Int
i
        Int
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Bad list length"

-- Semigroup instances for GHC 8.0+
#if MIN_VERSION_base(4,9,0)
-- | @since 0.2.0.0
instance Serialise a => Serialise (Semigroup.Min a) where
  encode :: Min a -> Encoding
encode = forall a. Serialise a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Min a -> a
Semigroup.getMin
  decode :: forall s. Decoder s (Min a)
decode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Min a
Semigroup.Min forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (Semigroup.Max a) where
  encode :: Max a -> Encoding
encode = forall a. Serialise a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Max a -> a
Semigroup.getMax
  decode :: forall s. Decoder s (Max a)
decode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Max a
Semigroup.Max forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (Semigroup.First a) where
  encode :: First a -> Encoding
encode = forall a. Serialise a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. First a -> a
Semigroup.getFirst
  decode :: forall s. Decoder s (First a)
decode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> First a
Semigroup.First forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise a => Serialise (Semigroup.Last a) where
  encode :: Last a -> Encoding
encode = forall a. Serialise a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Last a -> a
Semigroup.getLast
  decode :: forall s. Decoder s (Last a)
decode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Last a
Semigroup.Last forall a s. Serialise a => Decoder s a
decode

#if !MIN_VERSION_base(4,16,0)
-- | @since 0.2.0.0
instance Serialise a => Serialise (Semigroup.Option a) where
  encode = encode . Semigroup.getOption
  decode = fmap Semigroup.Option decode
#endif

instance Serialise a => Serialise (Semigroup.WrappedMonoid a) where
  encode :: WrappedMonoid a -> Encoding
encode = forall a. Serialise a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. WrappedMonoid m -> m
Semigroup.unwrapMonoid
  decode :: forall s. Decoder s (WrappedMonoid a)
decode = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall m. m -> WrappedMonoid m
Semigroup.WrapMonoid forall a s. Serialise a => Decoder s a
decode
#endif

--------------------------------------------------------------------------------
-- Foreign types

-- | @since 0.2.0.0
instance Serialise CChar where
    encode :: CChar -> Encoding
encode (CChar Int8
x) = forall a. Serialise a => a -> Encoding
encode Int8
x
    decode :: forall s. Decoder s CChar
decode = Int8 -> CChar
CChar forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CSChar where
    encode :: CSChar -> Encoding
encode (CSChar Int8
x) = forall a. Serialise a => a -> Encoding
encode Int8
x
    decode :: forall s. Decoder s CSChar
decode = Int8 -> CSChar
CSChar forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CUChar where
    encode :: CUChar -> Encoding
encode (CUChar Word8
x) = forall a. Serialise a => a -> Encoding
encode Word8
x
    decode :: forall s. Decoder s CUChar
decode = Word8 -> CUChar
CUChar forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CShort where
    encode :: CShort -> Encoding
encode (CShort Int16
x) = forall a. Serialise a => a -> Encoding
encode Int16
x
    decode :: forall s. Decoder s CShort
decode = Int16 -> CShort
CShort forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CUShort where
    encode :: CUShort -> Encoding
encode (CUShort Word16
x) = forall a. Serialise a => a -> Encoding
encode Word16
x
    decode :: forall s. Decoder s CUShort
decode = Word16 -> CUShort
CUShort forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CInt where
    encode :: CInt -> Encoding
encode (CInt Int32
x) = forall a. Serialise a => a -> Encoding
encode Int32
x
    decode :: forall s. Decoder s CInt
decode = Int32 -> CInt
CInt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CUInt where
    encode :: CUInt -> Encoding
encode (CUInt Word32
x) = forall a. Serialise a => a -> Encoding
encode Word32
x
    decode :: forall s. Decoder s CUInt
decode = Word32 -> CUInt
CUInt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CLong where
    encode :: CLong -> Encoding
encode (CLong Int64
x) = forall a. Serialise a => a -> Encoding
encode Int64
x
    decode :: forall s. Decoder s CLong
decode = Int64 -> CLong
CLong forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CULong where
    encode :: CULong -> Encoding
encode (CULong Word64
x) = forall a. Serialise a => a -> Encoding
encode Word64
x
    decode :: forall s. Decoder s CULong
decode = Word64 -> CULong
CULong forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CPtrdiff where
    encode :: CPtrdiff -> Encoding
encode (CPtrdiff Int64
x) = forall a. Serialise a => a -> Encoding
encode Int64
x
    decode :: forall s. Decoder s CPtrdiff
decode = Int64 -> CPtrdiff
CPtrdiff forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CSize where
    encode :: CSize -> Encoding
encode (CSize Word64
x) = forall a. Serialise a => a -> Encoding
encode Word64
x
    decode :: forall s. Decoder s CSize
decode = Word64 -> CSize
CSize forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CWchar where
    encode :: CWchar -> Encoding
encode (CWchar Int32
x) = forall a. Serialise a => a -> Encoding
encode Int32
x
    decode :: forall s. Decoder s CWchar
decode = Int32 -> CWchar
CWchar forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CSigAtomic where
    encode :: CSigAtomic -> Encoding
encode (CSigAtomic Int32
x) = forall a. Serialise a => a -> Encoding
encode Int32
x
    decode :: forall s. Decoder s CSigAtomic
decode = Int32 -> CSigAtomic
CSigAtomic forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CLLong where
    encode :: CLLong -> Encoding
encode (CLLong Int64
x) = forall a. Serialise a => a -> Encoding
encode Int64
x
    decode :: forall s. Decoder s CLLong
decode = Int64 -> CLLong
CLLong forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CULLong where
    encode :: CULLong -> Encoding
encode (CULLong Word64
x) = forall a. Serialise a => a -> Encoding
encode Word64
x
    decode :: forall s. Decoder s CULLong
decode = Word64 -> CULLong
CULLong forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CIntPtr where
    encode :: CIntPtr -> Encoding
encode (CIntPtr Int64
x) = forall a. Serialise a => a -> Encoding
encode Int64
x
    decode :: forall s. Decoder s CIntPtr
decode = Int64 -> CIntPtr
CIntPtr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CUIntPtr where
    encode :: CUIntPtr -> Encoding
encode (CUIntPtr Word64
x) = forall a. Serialise a => a -> Encoding
encode Word64
x
    decode :: forall s. Decoder s CUIntPtr
decode = Word64 -> CUIntPtr
CUIntPtr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CIntMax where
    encode :: CIntMax -> Encoding
encode (CIntMax Int64
x) = forall a. Serialise a => a -> Encoding
encode Int64
x
    decode :: forall s. Decoder s CIntMax
decode = Int64 -> CIntMax
CIntMax forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CUIntMax where
    encode :: CUIntMax -> Encoding
encode (CUIntMax Word64
x) = forall a. Serialise a => a -> Encoding
encode Word64
x
    decode :: forall s. Decoder s CUIntMax
decode = Word64 -> CUIntMax
CUIntMax forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CClock where
    encode :: CClock -> Encoding
encode (CClock Int64
x) = forall a. Serialise a => a -> Encoding
encode Int64
x
    decode :: forall s. Decoder s CClock
decode = Int64 -> CClock
CClock forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CTime where
    encode :: CTime -> Encoding
encode (CTime Int64
x) = forall a. Serialise a => a -> Encoding
encode Int64
x
    decode :: forall s. Decoder s CTime
decode = Int64 -> CTime
CTime forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CUSeconds where
    encode :: CUSeconds -> Encoding
encode (CUSeconds Word32
x) = forall a. Serialise a => a -> Encoding
encode Word32
x
    decode :: forall s. Decoder s CUSeconds
decode = Word32 -> CUSeconds
CUSeconds forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CSUSeconds where
    encode :: CSUSeconds -> Encoding
encode (CSUSeconds Int64
x) = forall a. Serialise a => a -> Encoding
encode Int64
x
    decode :: forall s. Decoder s CSUSeconds
decode = Int64 -> CSUSeconds
CSUSeconds forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CFloat where
    encode :: CFloat -> Encoding
encode (CFloat Float
x) = forall a. Serialise a => a -> Encoding
encode Float
x
    decode :: forall s. Decoder s CFloat
decode = Float -> CFloat
CFloat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance Serialise CDouble where
    encode :: CDouble -> Encoding
encode (CDouble Double
x) = forall a. Serialise a => a -> Encoding
encode Double
x
    decode :: forall s. Decoder s CDouble
decode = Double -> CDouble
CDouble forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

--------------------------------------------------------------------------------
-- Structural instances

-- | @since 0.2.0.0
instance (Serialise a, Serialise b) => Serialise (a,b) where
    encode :: (a, b) -> Encoding
encode (a
a,b
b) = Word -> Encoding
encodeListLen Word
2
                forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
a
                forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
b
    decode :: forall s. Decoder s (a, b)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
2
                !a
x <- forall a s. Serialise a => Decoder s a
decode
                !b
y <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, b
y)

-- | @since 0.2.0.0
instance (Serialise a, Serialise b, Serialise c) => Serialise (a,b,c) where
    encode :: (a, b, c) -> Encoding
encode (a
a,b
b,c
c) = Word -> Encoding
encodeListLen Word
3
                  forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
a
                  forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
b
                  forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode c
c

    decode :: forall s. Decoder s (a, b, c)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
3
                !a
x <- forall a s. Serialise a => Decoder s a
decode
                !b
y <- forall a s. Serialise a => Decoder s a
decode
                !c
z <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, b
y, c
z)

-- | @since 0.2.0.0
instance (Serialise a, Serialise b, Serialise c, Serialise d
         ) => Serialise (a,b,c,d) where
    encode :: (a, b, c, d) -> Encoding
encode (a
a,b
b,c
c,d
d) = Word -> Encoding
encodeListLen Word
4
                    forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
a
                    forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
b
                    forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode c
c
                    forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode d
d

    decode :: forall s. Decoder s (a, b, c, d)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
4
                !a
a <- forall a s. Serialise a => Decoder s a
decode
                !b
b <- forall a s. Serialise a => Decoder s a
decode
                !c
c <- forall a s. Serialise a => Decoder s a
decode
                !d
d <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b, c
c, d
d)

-- | @since 0.2.0.0
instance (Serialise a, Serialise b, Serialise c, Serialise d, Serialise e
         ) => Serialise (a,b,c,d,e) where
    encode :: (a, b, c, d, e) -> Encoding
encode (a
a,b
b,c
c,d
d,e
e) = Word -> Encoding
encodeListLen Word
5
                      forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
a
                      forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
b
                      forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode c
c
                      forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode d
d
                      forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode e
e

    decode :: forall s. Decoder s (a, b, c, d, e)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
5
                !a
a <- forall a s. Serialise a => Decoder s a
decode
                !b
b <- forall a s. Serialise a => Decoder s a
decode
                !c
c <- forall a s. Serialise a => Decoder s a
decode
                !d
d <- forall a s. Serialise a => Decoder s a
decode
                !e
e <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b, c
c, d
d, e
e)

-- | @since 0.2.0.0
instance ( Serialise a, Serialise b, Serialise c, Serialise d, Serialise e
         , Serialise f
         ) => Serialise (a,b,c,d,e,f) where
    encode :: (a, b, c, d, e, f) -> Encoding
encode (a
a,b
b,c
c,d
d,e
e,f
f) = Word -> Encoding
encodeListLen Word
6
                        forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
a
                        forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
b
                        forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode c
c
                        forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode d
d
                        forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode e
e
                        forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode f
f

    decode :: forall s. Decoder s (a, b, c, d, e, f)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
6
                !a
a <- forall a s. Serialise a => Decoder s a
decode
                !b
b <- forall a s. Serialise a => Decoder s a
decode
                !c
c <- forall a s. Serialise a => Decoder s a
decode
                !d
d <- forall a s. Serialise a => Decoder s a
decode
                !e
e <- forall a s. Serialise a => Decoder s a
decode
                !f
f <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b, c
c, d
d, e
e, f
f)

-- | @since 0.2.0.0
instance ( Serialise a, Serialise b, Serialise c, Serialise d, Serialise e
         , Serialise f, Serialise g
         ) => Serialise (a,b,c,d,e,f,g) where
    encode :: (a, b, c, d, e, f, g) -> Encoding
encode (a
a,b
b,c
c,d
d,e
e,f
f,g
g) = Word -> Encoding
encodeListLen Word
7
                          forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
a
                          forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
b
                          forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode c
c
                          forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode d
d
                          forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode e
e
                          forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode f
f
                          forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode g
g

    decode :: forall s. Decoder s (a, b, c, d, e, f, g)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
7
                !a
a <- forall a s. Serialise a => Decoder s a
decode
                !b
b <- forall a s. Serialise a => Decoder s a
decode
                !c
c <- forall a s. Serialise a => Decoder s a
decode
                !d
d <- forall a s. Serialise a => Decoder s a
decode
                !e
e <- forall a s. Serialise a => Decoder s a
decode
                !f
f <- forall a s. Serialise a => Decoder s a
decode
                !g
g <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b, c
c, d
d, e
e, f
f, g
g)

-- | @since 0.2.0.0
instance ( Serialise a, Serialise b, Serialise c, Serialise d, Serialise e
         , Serialise f, Serialise g, Serialise h
         ) => Serialise (a,b,c,d,e,f,g,h) where
    encode :: (a, b, c, d, e, f, g, h) -> Encoding
encode (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h) = Word -> Encoding
encodeListLen Word
8
                            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
a
                            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
b
                            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode c
c
                            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode d
d
                            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode e
e
                            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode f
f
                            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode g
g
                            forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode h
h

    decode :: forall s. Decoder s (a, b, c, d, e, f, g, h)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
8
                !a
a <- forall a s. Serialise a => Decoder s a
decode
                !b
b <- forall a s. Serialise a => Decoder s a
decode
                !c
c <- forall a s. Serialise a => Decoder s a
decode
                !d
d <- forall a s. Serialise a => Decoder s a
decode
                !e
e <- forall a s. Serialise a => Decoder s a
decode
                !f
f <- forall a s. Serialise a => Decoder s a
decode
                !g
g <- forall a s. Serialise a => Decoder s a
decode
                !h
h <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b, c
c, d
d, e
e, f
f, g
g, h
h)

-- | @since 0.2.0.0
instance ( Serialise a, Serialise b, Serialise c, Serialise d, Serialise e
         , Serialise f, Serialise g, Serialise h, Serialise i
         ) => Serialise (a,b,c,d,e,f,g,h,i) where
    encode :: (a, b, c, d, e, f, g, h, i) -> Encoding
encode (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i) = Word -> Encoding
encodeListLen Word
9
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
a
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
b
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode c
c
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode d
d
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode e
e
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode f
f
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode g
g
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode h
h
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode i
i

    decode :: forall s. Decoder s (a, b, c, d, e, f, g, h, i)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
9
                !a
a <- forall a s. Serialise a => Decoder s a
decode
                !b
b <- forall a s. Serialise a => Decoder s a
decode
                !c
c <- forall a s. Serialise a => Decoder s a
decode
                !d
d <- forall a s. Serialise a => Decoder s a
decode
                !e
e <- forall a s. Serialise a => Decoder s a
decode
                !f
f <- forall a s. Serialise a => Decoder s a
decode
                !g
g <- forall a s. Serialise a => Decoder s a
decode
                !h
h <- forall a s. Serialise a => Decoder s a
decode
                !i
i <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b, c
c, d
d, e
e, f
f, g
g, h
h, i
i)

-- | @since 0.2.0.0
instance Serialise a => Serialise (Maybe a) where
    encode :: Maybe a -> Encoding
encode Maybe a
Nothing  = Word -> Encoding
encodeListLen Word
0
    encode (Just a
x) = Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
x

    decode :: forall s. Decoder s (Maybe a)
decode = do Int
n <- forall s. Decoder s Int
decodeListLen
                case Int
n of
                  Int
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
                  Int
1 -> do !a
x <- forall a s. Serialise a => Decoder s a
decode
                          forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
x)
                  Int
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unknown tag"

-- | @since 0.2.0.0
instance (Serialise a, Serialise b) => Serialise (Either a b) where
    encode :: Either a b -> Encoding
encode (Left  a
x) = Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
x
    encode (Right b
x) = Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
1 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
x

    decode :: forall s. Decoder s (Either a b)
decode = do forall s. Int -> Decoder s ()
decodeListLenOf Int
2
                Word
t <- forall s. Decoder s Word
decodeWord
                case Word
t of
                  Word
0 -> do !a
x <- forall a s. Serialise a => Decoder s a
decode
                          forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left a
x)
                  Word
1 -> do !b
x <- forall a s. Serialise a => Decoder s a
decode
                          forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
x)
                  Word
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unknown tag"

-- | @since 0.2.4.0
instance (Serialise a, Serialise b) => Serialise (These.These a b) where
    encode :: These a b -> Encoding
encode (These.This a
x) = Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
x
    encode (These.That b
x) = Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
1 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
x
    encode (These.These a
x b
y) = Word -> Encoding
encodeListLen Word
3 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
2 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
x forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode b
y

    decode :: forall s. Decoder s (These a b)
decode = do Int
n <- forall s. Decoder s Int
decodeListLen
                Word
t <- forall s. Decoder s Word
decodeWord
                case (Word
t, Int
n) of
                  (Word
0, Int
2) -> do !a
x <- forall a s. Serialise a => Decoder s a
decode
                               forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> These a b
These.This a
x)
                  (Word
1, Int
2) -> do !b
x <- forall a s. Serialise a => Decoder s a
decode
                               forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> These a b
These.That b
x)
                  (Word
2, Int
3) -> do !a
x <- forall a s. Serialise a => Decoder s a
decode
                               !b
y <- forall a s. Serialise a => Decoder s a
decode
                               forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> b -> These a b
These.These a
x b
y)
                  (Word, Int)
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unknown tag"

-- | @since 0.2.4.0
instance (Serialise a, Serialise b) => Serialise (Strict.Pair a b) where
    encode :: Pair a b -> Encoding
encode = forall a. Serialise a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall lazy strict. Strict lazy strict => strict -> lazy
Strict.toLazy
    decode :: forall s. Decoder s (Pair a b)
decode = forall lazy strict. Strict lazy strict => lazy -> strict
Strict.toStrict forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.4.0
instance Serialise a => Serialise (Strict.Maybe a) where
    encode :: Maybe a -> Encoding
encode = forall a. Serialise a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall lazy strict. Strict lazy strict => strict -> lazy
Strict.toLazy
    decode :: forall s. Decoder s (Maybe a)
decode = forall lazy strict. Strict lazy strict => lazy -> strict
Strict.toStrict forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.4.0
instance (Serialise a, Serialise b) => Serialise (Strict.Either a b) where
    encode :: Either a b -> Encoding
encode = forall a. Serialise a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall lazy strict. Strict lazy strict => strict -> lazy
Strict.toLazy
    decode :: forall s. Decoder s (Either a b)
decode = forall lazy strict. Strict lazy strict => lazy -> strict
Strict.toStrict forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.4.0
instance (Serialise a, Serialise b) => Serialise (Strict.These a b) where
    encode :: These a b -> Encoding
encode = forall a. Serialise a => a -> Encoding
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall lazy strict. Strict lazy strict => strict -> lazy
Strict.toLazy
    decode :: forall s. Decoder s (These a b)
decode = forall lazy strict. Strict lazy strict => lazy -> strict
Strict.toStrict forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode


--------------------------------------------------------------------------------
-- Container instances

-- | @since 0.2.0.0
instance Serialise a => Serialise (Tree.Tree a) where
  encode :: Tree a -> Encoding
encode (Tree.Node a
r [Tree a]
sub) = Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
r forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode [Tree a]
sub
  decode :: forall s. Decoder s (Tree a)
decode = forall s. Int -> Decoder s ()
decodeListLenOf Int
2 forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (forall a. a -> [Tree a] -> Tree a
Tree.Node forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode)

-- | Patch functions together to obtain an 'Encoding' for a container.
encodeContainerSkel :: (Word -> Encoding) -- ^ encoder of the length
                    -> (container -> Int) -- ^ length
                    -> (accumFunc -> Encoding -> container -> Encoding) -- ^ foldr
                    -> accumFunc
                    -> container
                    -> Encoding
encodeContainerSkel :: forall container accumFunc.
(Word -> Encoding)
-> (container -> Int)
-> (accumFunc -> Encoding -> container -> Encoding)
-> accumFunc
-> container
-> Encoding
encodeContainerSkel Word -> Encoding
encodeLen container -> Int
size accumFunc -> Encoding -> container -> Encoding
foldr accumFunc
f  container
c =
    Word -> Encoding
encodeLen (forall a b. (Integral a, Num b) => a -> b
fromIntegral (container -> Int
size container
c)) forall a. Semigroup a => a -> a -> a
<> accumFunc -> Encoding -> container -> Encoding
foldr accumFunc
f forall a. Monoid a => a
mempty container
c
{-# INLINE encodeContainerSkel #-}

decodeContainerSkelWithReplicate
  :: (Serialise a)
  => Decoder s Int
     -- ^ How to get the size of the container
  -> (Int -> Decoder s a -> Decoder s container)
     -- ^ replicateM for the container
  -> ([container] -> container)
     -- ^ concat for the container
  -> Decoder s container
decodeContainerSkelWithReplicate :: forall a s container.
Serialise a =>
Decoder s Int
-> (Int -> Decoder s a -> Decoder s container)
-> ([container] -> container)
-> Decoder s container
decodeContainerSkelWithReplicate Decoder s Int
decodeLen Int -> Decoder s a -> Decoder s container
replicateFun [container] -> container
fromList = do
    -- Look at how much data we have at the moment and use it as the limit for
    -- the size of a single call to replicateFun. We don't want to use
    -- replicateFun directly on the result of decodeLen since this might lead to
    -- DOS attack (attacker providing a huge value for length). So if it's above
    -- our limit, we'll do manual chunking and then combine the containers into
    -- one.
    Int
size <- Decoder s Int
decodeLen
    Int
limit <- forall s. Decoder s Int
peekAvailable
    if Int
size forall a. Ord a => a -> a -> Bool
<= Int
limit
       then Int -> Decoder s a -> Decoder s container
replicateFun Int
size forall a s. Serialise a => Decoder s a
decode
       else do
           -- Take the max of limit and a fixed chunk size (note: limit can be
           -- 0). This basically means that the attacker can make us allocate a
           -- container of size 128 even though there's no actual input.
           let chunkSize :: Int
chunkSize = forall a. Ord a => a -> a -> a
max Int
limit Int
128
               (Int
d, Int
m) = Int
size forall a. Integral a => a -> a -> (a, a)
`divMod` Int
chunkSize
               buildOne :: Int -> Decoder s container
buildOne Int
s = Int -> Decoder s a -> Decoder s container
replicateFun Int
s forall a s. Serialise a => Decoder s a
decode
           [container]
containers <- forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence forall a b. (a -> b) -> a -> b
$ Int -> Decoder s container
buildOne Int
m forall a. a -> [a] -> [a]
: forall a. Int -> a -> [a]
replicate Int
d (Int -> Decoder s container
buildOne Int
chunkSize)
           forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! [container] -> container
fromList [container]
containers
{-# INLINE decodeContainerSkelWithReplicate #-}

-- | @since 0.2.0.0
instance (Serialise a) => Serialise (Sequence.Seq a) where
  encode :: Seq a -> Encoding
encode = forall container accumFunc.
(Word -> Encoding)
-> (container -> Int)
-> (accumFunc -> Encoding -> container -> Encoding)
-> accumFunc
-> container
-> Encoding
encodeContainerSkel
             Word -> Encoding
encodeListLen
             forall a. Seq a -> Int
Sequence.length
             forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr
             (\a
a Encoding
b -> forall a. Serialise a => a -> Encoding
encode a
a forall a. Semigroup a => a -> a -> a
<> Encoding
b)
  decode :: forall s. Decoder s (Seq a)
decode = forall a s container.
Serialise a =>
Decoder s Int
-> (Int -> Decoder s a -> Decoder s container)
-> ([container] -> container)
-> Decoder s container
decodeContainerSkelWithReplicate
             forall s. Decoder s Int
decodeListLen
             forall (m :: * -> *) a. Applicative m => Int -> m a -> m (Seq a)
Sequence.replicateM
             forall a. Monoid a => [a] -> a
mconcat

-- | Generic encoder for vectors. Its intended use is to allow easy
-- definition of 'Serialise' instances for custom vector
--
-- @since 0.2.0.0
encodeVector :: (Serialise a, Vector.Generic.Vector v a)
             => v a -> Encoding
encodeVector :: forall a (v :: * -> *).
(Serialise a, Vector v a) =>
v a -> Encoding
encodeVector = forall container accumFunc.
(Word -> Encoding)
-> (container -> Int)
-> (accumFunc -> Encoding -> container -> Encoding)
-> accumFunc
-> container
-> Encoding
encodeContainerSkel
    Word -> Encoding
encodeListLen
    forall (v :: * -> *) a. Vector v a => v a -> Int
Vector.Generic.length
    forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
Vector.Generic.foldr
    (\a
a Encoding
b -> forall a. Serialise a => a -> Encoding
encode a
a forall a. Semigroup a => a -> a -> a
<> Encoding
b)
{-# INLINE encodeVector #-}

-- | Generic decoder for vectors. Its intended use is to allow easy
-- definition of 'Serialise' instances for custom vector
--
-- @since 0.2.0.0
decodeVector :: (Serialise a, Vector.Generic.Vector v a)
             => Decoder s (v a)
decodeVector :: forall a (v :: * -> *) s.
(Serialise a, Vector v a) =>
Decoder s (v a)
decodeVector = forall a s container.
Serialise a =>
Decoder s Int
-> (Int -> Decoder s a -> Decoder s container)
-> ([container] -> container)
-> Decoder s container
decodeContainerSkelWithReplicate
    forall s. Decoder s Int
decodeListLen
    forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
Int -> m a -> m (v a)
Vector.Generic.replicateM
    forall (v :: * -> *) a. Vector v a => [v a] -> v a
Vector.Generic.concat
{-# INLINE decodeVector #-}

-- | @since 0.2.0.0
instance (Serialise a) => Serialise (Vector.Vector a) where
  encode :: Vector a -> Encoding
encode = forall a (v :: * -> *).
(Serialise a, Vector v a) =>
v a -> Encoding
encodeVector
  {-# INLINE encode #-}
  decode :: forall s. Decoder s (Vector a)
decode = forall a (v :: * -> *) s.
(Serialise a, Vector v a) =>
Decoder s (v a)
decodeVector
  {-# INLINE decode #-}

-- | @since 0.2.0.0
instance (Serialise a, Vector.Unboxed.Unbox a) =>
         Serialise (Vector.Unboxed.Vector a) where
  encode :: Vector a -> Encoding
encode = forall a (v :: * -> *).
(Serialise a, Vector v a) =>
v a -> Encoding
encodeVector
  {-# INLINE encode #-}
  decode :: forall s. Decoder s (Vector a)
decode = forall a (v :: * -> *) s.
(Serialise a, Vector v a) =>
Decoder s (v a)
decodeVector
  {-# INLINE decode #-}

-- | @since 0.2.0.0
instance (Serialise a, Vector.Storable.Storable a) => Serialise (Vector.Storable.Vector a) where
  encode :: Vector a -> Encoding
encode = forall a (v :: * -> *).
(Serialise a, Vector v a) =>
v a -> Encoding
encodeVector
  {-# INLINE encode #-}
  decode :: forall s. Decoder s (Vector a)
decode = forall a (v :: * -> *) s.
(Serialise a, Vector v a) =>
Decoder s (v a)
decodeVector
  {-# INLINE decode #-}

-- | @since 0.2.0.0
instance (Serialise a, Vector.Primitive.Prim a) => Serialise (Vector.Primitive.Vector a) where
  encode :: Vector a -> Encoding
encode = forall a (v :: * -> *).
(Serialise a, Vector v a) =>
v a -> Encoding
encodeVector
  {-# INLINE encode #-}
  decode :: forall s. Decoder s (Vector a)
decode = forall a (v :: * -> *) s.
(Serialise a, Vector v a) =>
Decoder s (v a)
decodeVector
  {-# INLINE decode #-}



encodeSetSkel :: Serialise a
              => (s -> Int)
              -> ((a -> Encoding -> Encoding) -> Encoding -> s -> Encoding)
              -> s
              -> Encoding
encodeSetSkel :: forall a s.
Serialise a =>
(s -> Int)
-> ((a -> Encoding -> Encoding) -> Encoding -> s -> Encoding)
-> s
-> Encoding
encodeSetSkel s -> Int
size (a -> Encoding -> Encoding) -> Encoding -> s -> Encoding
foldr =
    forall container accumFunc.
(Word -> Encoding)
-> (container -> Int)
-> (accumFunc -> Encoding -> container -> Encoding)
-> accumFunc
-> container
-> Encoding
encodeContainerSkel Word -> Encoding
encodeListLen s -> Int
size (a -> Encoding -> Encoding) -> Encoding -> s -> Encoding
foldr (\a
a Encoding
b -> forall a. Serialise a => a -> Encoding
encode a
a forall a. Semigroup a => a -> a -> a
<> Encoding
b)
{-# INLINE encodeSetSkel #-}

decodeSetSkel :: Serialise a
              => ([a] -> c) -> Decoder s c
decodeSetSkel :: forall a c s. Serialise a => ([a] -> c) -> Decoder s c
decodeSetSkel [a] -> c
fromList = do
  Int
n <- forall s. Decoder s Int
decodeListLen
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> c
fromList (forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n forall a s. Serialise a => Decoder s a
decode)
{-# INLINE decodeSetSkel #-}

-- | @since 0.2.0.0
instance (Ord a, Serialise a) => Serialise (Set.Set a) where
  encode :: Set a -> Encoding
encode = forall a s.
Serialise a =>
(s -> Int)
-> ((a -> Encoding -> Encoding) -> Encoding -> s -> Encoding)
-> s
-> Encoding
encodeSetSkel forall a. Set a -> Int
Set.size forall a b. (a -> b -> b) -> b -> Set a -> b
Set.foldr
  decode :: forall s. Decoder s (Set a)
decode = forall a c s. Serialise a => ([a] -> c) -> Decoder s c
decodeSetSkel forall a. Ord a => [a] -> Set a
Set.fromList

-- | @since 0.2.0.0
instance Serialise IntSet.IntSet where
  encode :: IntSet -> Encoding
encode = forall a s.
Serialise a =>
(s -> Int)
-> ((a -> Encoding -> Encoding) -> Encoding -> s -> Encoding)
-> s
-> Encoding
encodeSetSkel IntSet -> Int
IntSet.size forall b. (Int -> b -> b) -> b -> IntSet -> b
IntSet.foldr
  decode :: forall s. Decoder s IntSet
decode = forall a c s. Serialise a => ([a] -> c) -> Decoder s c
decodeSetSkel [Int] -> IntSet
IntSet.fromList

-- | @since 0.2.0.0
instance (Serialise a, Hashable a, Eq a) => Serialise (HashSet.HashSet a) where
  encode :: HashSet a -> Encoding
encode = forall a s.
Serialise a =>
(s -> Int)
-> ((a -> Encoding -> Encoding) -> Encoding -> s -> Encoding)
-> s
-> Encoding
encodeSetSkel forall a. HashSet a -> Int
HashSet.size forall b a. (b -> a -> a) -> a -> HashSet b -> a
HashSet.foldr
  decode :: forall s. Decoder s (HashSet a)
decode = forall a c s. Serialise a => ([a] -> c) -> Decoder s c
decodeSetSkel forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList

-- | A helper function for encoding maps.
encodeMapSkel :: (Serialise k, Serialise v)
              => (m -> Int) -- ^ obtain the length
              -> ((k -> v -> Encoding -> Encoding) -> Encoding -> m -> Encoding)
              -> m
              -> Encoding
encodeMapSkel :: forall k v m.
(Serialise k, Serialise v) =>
(m -> Int)
-> ((k -> v -> Encoding -> Encoding) -> Encoding -> m -> Encoding)
-> m
-> Encoding
encodeMapSkel m -> Int
size (k -> v -> Encoding -> Encoding) -> Encoding -> m -> Encoding
foldrWithKey =
  forall container accumFunc.
(Word -> Encoding)
-> (container -> Int)
-> (accumFunc -> Encoding -> container -> Encoding)
-> accumFunc
-> container
-> Encoding
encodeContainerSkel
    Word -> Encoding
encodeMapLen
    m -> Int
size
    (k -> v -> Encoding -> Encoding) -> Encoding -> m -> Encoding
foldrWithKey
    (\k
k v
v Encoding
b -> forall a. Serialise a => a -> Encoding
encode k
k forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode v
v forall a. Semigroup a => a -> a -> a
<> Encoding
b)
{-# INLINE encodeMapSkel #-}

-- | A utility function to construct a 'Decoder' for maps.
decodeMapSkel :: (Serialise k, Serialise v)
              => ([(k,v)] -> m) -- ^ fromList
              -> Decoder s m
decodeMapSkel :: forall k v m s.
(Serialise k, Serialise v) =>
([(k, v)] -> m) -> Decoder s m
decodeMapSkel [(k, v)] -> m
fromList = do
  Int
n <- forall s. Decoder s Int
decodeMapLen
  let decodeEntry :: Decoder s (k, v)
decodeEntry = do
        !k
k <- forall a s. Serialise a => Decoder s a
decode
        !v
v <- forall a s. Serialise a => Decoder s a
decode
        forall (m :: * -> *) a. Monad m => a -> m a
return (k
k, v
v)
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(k, v)] -> m
fromList (forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n forall {s}. Decoder s (k, v)
decodeEntry)
{-# INLINE decodeMapSkel #-}

-- | @since 0.2.0.0
instance (Ord k, Serialise k, Serialise v) => Serialise (Map.Map k v) where
  encode :: Map k v -> Encoding
encode = forall k v m.
(Serialise k, Serialise v) =>
(m -> Int)
-> ((k -> v -> Encoding -> Encoding) -> Encoding -> m -> Encoding)
-> m
-> Encoding
encodeMapSkel forall k a. Map k a -> Int
Map.size forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey
  decode :: forall s. Decoder s (Map k v)
decode = forall k v m s.
(Serialise k, Serialise v) =>
([(k, v)] -> m) -> Decoder s m
decodeMapSkel forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList

-- | @since 0.2.0.0
instance (Serialise a) => Serialise (IntMap.IntMap a) where
  encode :: IntMap a -> Encoding
encode = forall k v m.
(Serialise k, Serialise v) =>
(m -> Int)
-> ((k -> v -> Encoding -> Encoding) -> Encoding -> m -> Encoding)
-> m
-> Encoding
encodeMapSkel forall a. IntMap a -> Int
IntMap.size forall a b. (Int -> a -> b -> b) -> b -> IntMap a -> b
IntMap.foldrWithKey
  decode :: forall s. Decoder s (IntMap a)
decode = forall k v m s.
(Serialise k, Serialise v) =>
([(k, v)] -> m) -> Decoder s m
decodeMapSkel forall a. [(Int, a)] -> IntMap a
IntMap.fromList

-- | @since 0.2.0.0
instance (Serialise k, Hashable k, Eq k, Serialise v) =>
  Serialise (HashMap.HashMap k v) where
  encode :: HashMap k v -> Encoding
encode = forall k v m.
(Serialise k, Serialise v) =>
(m -> Int)
-> ((k -> v -> Encoding -> Encoding) -> Encoding -> m -> Encoding)
-> m
-> Encoding
encodeMapSkel forall k v. HashMap k v -> Int
HashMap.size forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
HashMap.foldrWithKey
  decode :: forall s. Decoder s (HashMap k v)
decode = forall k v m s.
(Serialise k, Serialise v) =>
([(k, v)] -> m) -> Decoder s m
decodeMapSkel forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList


--------------------------------------------------------------------------------
-- Misc base package instances

-- | @since 0.2.0.0
instance Serialise Version where
    encode :: Version -> Encoding
encode (Version [Int]
ns [String]
ts) = Word -> Encoding
encodeListLen Word
3
                          forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode [Int]
ns forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode [String]
ts
    decode :: forall s. Decoder s Version
decode = do
      Int
len <- forall s. Decoder s Int
decodeListLen
      Word
tag <- forall s. Decoder s Word
decodeWord
      case Word
tag of
        Word
0 | Int
len forall a. Eq a => a -> a -> Bool
== Int
3
          -> do ![Int]
x <- forall a s. Serialise a => Decoder s a
decode
                ![String]
y <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return ([Int] -> [String] -> Version
Version [Int]
x [String]
y)
        Word
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unexpected tag"

-- | @since 0.2.0.0
instance Serialise Fingerprint where
    encode :: Fingerprint -> Encoding
encode (Fingerprint Word64
w1 Word64
w2) = Word -> Encoding
encodeListLen Word
3
                              forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode Word64
w1
                              forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode Word64
w2
    decode :: forall s. Decoder s Fingerprint
decode = do
      forall s. Int -> Decoder s ()
decodeListLenOf Int
3
      Word
tag <- forall s. Decoder s Word
decodeWord
      case Word
tag of
        Word
0 -> do !Word64
w1 <- forall a s. Serialise a => Decoder s a
decode
                !Word64
w2 <- forall a s. Serialise a => Decoder s a
decode
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! Word64 -> Word64 -> Fingerprint
Fingerprint Word64
w1 Word64
w2
        Word
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unexpected tag"

-- | @since 0.2.0.0
instance Serialise TyCon where
#if MIN_VERSION_base(4,10,0)
  encode :: TyCon -> Encoding
encode TyCon
tc
    = Word -> Encoding
encodeListLen Word
6
   forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0
   forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode (TyCon -> String
tyConPackage TyCon
tc)
   forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode (TyCon -> String
tyConModule TyCon
tc)
   forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode (TyCon -> String
tyConName TyCon
tc)
   forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode (TyCon -> Int
tyConKindArgs TyCon
tc)
   forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode (TyCon -> KindRep
tyConKindRep TyCon
tc)
  decode :: forall s. Decoder s TyCon
decode = do
    forall s. Int -> Decoder s ()
decodeListLenOf Int
6
    Word
tag <- forall s. Decoder s Word
decodeWord
    case Word
tag of
      Word
0 -> String -> String -> String -> Int -> KindRep -> TyCon
mkTyCon forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode
      Word
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unexpected tag"
#elif MIN_VERSION_base(4,9,0)
  encode tycon
    = encodeListLen 4
   <> encodeWord 0
   <> encode (tyConPackage tycon)
   <> encode (tyConModule  tycon)
   <> encode (tyConName    tycon)
#else
  encode (TyCon _ pkg modname name)
    = encodeListLen 4
   <> encodeWord 0
   <> encode pkg
   <> encode modname
   <> encode name
#endif

#if !MIN_VERSION_base(4,10,0)
  decode = do
    decodeListLenOf 4
    tag <- decodeWord
    case tag of
      0 -> do !pkg     <- decode
              !modname <- decode
              !name    <- decode
              return $! mkTyCon3 pkg modname name
      _ -> fail "unexpected tag"
#endif

#if MIN_VERSION_base(4,10,0)
-- | @since 0.2.0.0
instance Serialise VecCount where
  encode :: VecCount -> Encoding
encode VecCount
c = Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum VecCount
c)
  decode :: forall s. Decoder s VecCount
decode = do
    forall s. Int -> Decoder s ()
decodeListLenOf Int
1
    forall a. Enum a => Int -> a
toEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Word
decodeWord

-- | @since 0.2.0.0
instance Serialise VecElem where
  encode :: VecElem -> Encoding
encode VecElem
e = Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum VecElem
e)
  decode :: forall s. Decoder s VecElem
decode = do
    forall s. Int -> Decoder s ()
decodeListLenOf Int
1
    forall a. Enum a => Int -> a
toEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Word
decodeWord

#if MIN_VERSION_base(4,16,0)
-- | @since 0.2.6.0
instance Serialise Levity where
  encode :: Levity -> Encoding
encode Levity
lev = Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum Levity
lev)
  decode :: forall s. Decoder s Levity
decode = do
    forall s. Int -> Decoder s ()
decodeListLenOf Int
1
    forall a. Enum a => Int -> a
toEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Word
decodeWord
#endif

-- | @since 0.2.0.0
instance Serialise RuntimeRep where
  encode :: RuntimeRep -> Encoding
encode RuntimeRep
rr =
    case RuntimeRep
rr of
      VecRep VecCount
a VecElem
b    -> Word -> Encoding
encodeListLen Word
3 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode VecCount
a forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode VecElem
b
      TupleRep [RuntimeRep]
reps -> Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
1 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode [RuntimeRep]
reps
      SumRep [RuntimeRep]
reps   -> Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
2 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode [RuntimeRep]
reps
#if MIN_VERSION_base(4,16,0)
      BoxedRep Levity
lev  -> Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
3 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode Levity
lev
#else
      LiftedRep     -> encodeListLen 1 <> encodeWord 3
      UnliftedRep   -> encodeListLen 1 <> encodeWord 4
#endif
      RuntimeRep
IntRep        -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
5
      RuntimeRep
WordRep       -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
6
      RuntimeRep
Int64Rep      -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
7
      RuntimeRep
Word64Rep     -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
8
      RuntimeRep
AddrRep       -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
9
      RuntimeRep
FloatRep      -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
10
      RuntimeRep
DoubleRep     -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
11
#if MIN_VERSION_base(4,13,0)
      RuntimeRep
Int8Rep       -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
12
      RuntimeRep
Int16Rep      -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
13
      RuntimeRep
Word8Rep      -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
14
      RuntimeRep
Word16Rep     -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
15
#endif
#if MIN_VERSION_base(4,14,0)
      RuntimeRep
Int32Rep      -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
16
      RuntimeRep
Word32Rep     -> Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
17
#endif

  decode :: forall s. Decoder s RuntimeRep
decode = do
    Int
len <- forall s. Decoder s Int
decodeListLen
    Word
tag <- forall s. Decoder s Word
decodeWord
    case Word
tag of
      Word
0  | Int
len forall a. Eq a => a -> a -> Bool
== Int
3 -> VecCount -> VecElem -> RuntimeRep
VecRep forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode
      Word
1  | Int
len forall a. Eq a => a -> a -> Bool
== Int
2 -> [RuntimeRep] -> RuntimeRep
TupleRep forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode
      Word
2  | Int
len forall a. Eq a => a -> a -> Bool
== Int
2 -> [RuntimeRep] -> RuntimeRep
SumRep forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode
#if MIN_VERSION_base(4,16,0)
      Word
3  | Int
len forall a. Eq a => a -> a -> Bool
== Int
2 -> Levity -> RuntimeRep
BoxedRep forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode
#else
      3  | len == 1 -> pure LiftedRep
      4  | len == 1 -> pure UnliftedRep
#endif
      Word
5  | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
IntRep
      Word
6  | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
WordRep
      Word
7  | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
Int64Rep
      Word
8  | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
Word64Rep
      Word
9  | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
AddrRep
      Word
10 | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
FloatRep
      Word
11 | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
DoubleRep
#if MIN_VERSION_base(4,13,0)
      Word
12 | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
Int8Rep
      Word
13 | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
Int16Rep
      Word
14 | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
Word8Rep
      Word
15 | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
Word16Rep
#endif
#if MIN_VERSION_base(4,14,0)
      Word
16 | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
Int32Rep
      Word
17 | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure RuntimeRep
Word32Rep
#endif
      Word
_             -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Data.Serialise.Binary.CBOR.getRuntimeRep: invalid tag"

-- | @since 0.2.0.0
instance Serialise KindRep where
  encode :: KindRep -> Encoding
encode KindRep
rep =
    case KindRep
rep of
      KindRepTyConApp TyCon
tc [KindRep]
k  -> Word -> Encoding
encodeListLen Word
3 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode TyCon
tc forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode [KindRep]
k
      KindRepVar Int
bndr       -> Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
1 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode Int
bndr
      KindRepApp KindRep
a KindRep
b        -> Word -> Encoding
encodeListLen Word
3 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
2 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode KindRep
a forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode KindRep
b
      KindRepFun KindRep
a KindRep
b        -> Word -> Encoding
encodeListLen Word
3 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
3 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode KindRep
a forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode KindRep
b
      KindRepTYPE RuntimeRep
r         -> Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
4 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode RuntimeRep
r
      KindRepTypeLit TypeLitSort
sort String
r -> Word -> Encoding
encodeListLen Word
3 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
5 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode TypeLitSort
sort forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode String
r

  decode :: forall s. Decoder s KindRep
decode = do
    Int
len <- forall s. Decoder s Int
decodeListLen
    Word
tag <- forall s. Decoder s Word
decodeWord
    case Word
tag of
      Word
0 | Int
len forall a. Eq a => a -> a -> Bool
== Int
3 -> TyCon -> [KindRep] -> KindRep
KindRepTyConApp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode
      Word
1 | Int
len forall a. Eq a => a -> a -> Bool
== Int
2 -> Int -> KindRep
KindRepVar forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode
      Word
2 | Int
len forall a. Eq a => a -> a -> Bool
== Int
3 -> KindRep -> KindRep -> KindRep
KindRepApp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode
      Word
3 | Int
len forall a. Eq a => a -> a -> Bool
== Int
3 -> KindRep -> KindRep -> KindRep
KindRepFun forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode
      Word
4 | Int
len forall a. Eq a => a -> a -> Bool
== Int
2 -> RuntimeRep -> KindRep
KindRepTYPE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode
      Word
5 | Int
len forall a. Eq a => a -> a -> Bool
== Int
3 -> TypeLitSort -> String -> KindRep
KindRepTypeLit forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. Serialise a => Decoder s a
decode
      Word
_            -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Data.Serialise.Binary.CBOR.getKindRep: invalid tag"

-- | @since 0.2.0.0
instance Serialise TypeLitSort where
  encode :: TypeLitSort -> Encoding
encode TypeLitSort
n
    = Word -> Encoding
encodeListLen Word
1
   forall a. Semigroup a => a -> a -> a
<> case TypeLitSort
n of
        TypeLitSort
TypeLitSymbol -> Word -> Encoding
encodeWord Word
0
        TypeLitSort
TypeLitNat    -> Word -> Encoding
encodeWord Word
1
#if MIN_VERSION_base(4,16,0)
        TypeLitSort
TypeLitChar   -> Word -> Encoding
encodeWord Word
2
#endif
  decode :: forall s. Decoder s TypeLitSort
decode = do
    forall s. Int -> Decoder s ()
decodeListLenOf Int
1
    Word
tag <- forall s. Decoder s Word
decodeWord
    case Word
tag of
      Word
0 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure TypeLitSort
TypeLitSymbol
      Word
1 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure TypeLitSort
TypeLitNat
#if MIN_VERSION_base(4,16,0)
      Word
2 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure TypeLitSort
TypeLitChar
#endif
      Word
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Data.Serialise.Binary.CBOR.putTypeLitSort: invalid tag"

decodeSomeTypeRep :: Decoder s SomeTypeRep
decodeSomeTypeRep :: forall s. Decoder s SomeTypeRep
decodeSomeTypeRep = do
    Int
len <- forall s. Decoder s Int
decodeListLen
    Word
tag <- forall s. Decoder s Word
decodeWord
    case Word
tag of
      Word
0 | Int
len forall a. Eq a => a -> a -> Bool
== Int
1 ->
              forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall k (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall {k} (a :: k). Typeable a => TypeRep a
typeRep :: TypeRep Type)
      Word
1 | Int
len forall a. Eq a => a -> a -> Bool
== Int
3 -> do
              !TyCon
con <- forall a s. Serialise a => Decoder s a
decode
              ![SomeTypeRep]
ks <- forall a s. Serialise a => Decoder s a
decode
              forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall k (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep forall a b. (a -> b) -> a -> b
$ forall k (a :: k). TyCon -> [SomeTypeRep] -> TypeRep a
mkTrCon TyCon
con [SomeTypeRep]
ks
      Word
2 | Int
len forall a. Eq a => a -> a -> Bool
== Int
3 -> do
              SomeTypeRep TypeRep a
f <- forall s. Decoder s SomeTypeRep
decodeSomeTypeRep
              SomeTypeRep TypeRep a
x <- forall s. Decoder s SomeTypeRep
decodeSomeTypeRep
              case forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
f of
                Fun TypeRep arg
arg TypeRep res
res ->
                    case TypeRep arg
arg forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
x of
                      Just arg :~~: k
HRefl -> do
                          case forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep res
res forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` (forall {k} (a :: k). Typeable a => TypeRep a
typeRep :: TypeRep Type) of
                            Just TYPE r2 :~~: *
HRefl -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall k (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep (forall k1 k2 (a :: k1 -> k2) (b :: k1).
TypeRep a -> TypeRep b -> TypeRep (a b)
mkTrApp TypeRep a
f TypeRep a
x)
                            Maybe (TYPE r2 :~~: *)
_          -> forall {m :: * -> *} {a}. MonadFail m => String -> [String] -> m a
failure String
"Kind mismatch" []
                      Maybe (arg :~~: k)
_ -> forall {m :: * -> *} {a}. MonadFail m => String -> [String] -> m a
failure String
"Kind mismatch"
                           [ String
"Found argument of kind:      " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
x)
                           , String
"Where the constructor:       " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show TypeRep a
f
                           , String
"Expects an argument of kind: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show TypeRep arg
arg
                           ]
                TypeRep k
_ -> forall {m :: * -> *} {a}. MonadFail m => String -> [String] -> m a
failure String
"Applied non-arrow type"
                     [ String
"Applied type: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show TypeRep a
f
                     , String
"To argument:  " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show TypeRep a
x
                     ]
      Word
3 | Int
len forall a. Eq a => a -> a -> Bool
== Int
3 -> do
              SomeTypeRep TypeRep a
arg <- forall s. Decoder s SomeTypeRep
decodeSomeTypeRep
              SomeTypeRep TypeRep a
res <- forall s. Decoder s SomeTypeRep
decodeSomeTypeRep
              case forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
arg forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` (forall {k} (a :: k). Typeable a => TypeRep a
typeRep :: TypeRep Type) of
                Just k :~~: *
HRefl ->
                    case  forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
res forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` (forall {k} (a :: k). Typeable a => TypeRep a
typeRep :: TypeRep Type) of
                      Just k :~~: *
HRefl -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall k (a :: k). TypeRep a -> SomeTypeRep
SomeTypeRep forall a b. (a -> b) -> a -> b
$ forall k (fun :: k) arg res.
(k ~ *, fun ~~ (arg -> res)) =>
TypeRep arg -> TypeRep res -> TypeRep fun
Fun TypeRep a
arg TypeRep a
res
                      Maybe (k :~~: *)
Nothing -> forall {m :: * -> *} {a}. MonadFail m => String -> [String] -> m a
failure String
"Kind mismatch" []
                Maybe (k :~~: *)
Nothing -> forall {m :: * -> *} {a}. MonadFail m => String -> [String] -> m a
failure String
"Kind mismatch" []
      Word
_ -> forall {m :: * -> *} {a}. MonadFail m => String -> [String] -> m a
failure String
"unexpected tag"
           [ String
"Tag: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Word
tag
           , String
"Len: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
len ]
  where
    failure :: String -> [String] -> m a
failure String
description [String]
info =
        forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines forall a b. (a -> b) -> a -> b
$ [ String
"Codec.CBOR.Class.decodeSomeTypeRep: "forall a. [a] -> [a] -> [a]
++String
description ]
                         forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (String
"    "forall a. [a] -> [a] -> [a]
++) [String]
info

encodeTypeRep :: TypeRep a -> Encoding
encodeTypeRep :: forall {k} (a :: k). TypeRep a -> Encoding
encodeTypeRep TypeRep a
rep  -- Handle Type specially since it's so common
  | Just a :~~: *
HRefl <- TypeRep a
rep forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` (forall {k} (a :: k). Typeable a => TypeRep a
typeRep :: TypeRep Type)
  = Word -> Encoding
encodeListLen Word
1
 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0
encodeTypeRep (Con' TyCon
con [SomeTypeRep]
ks)
  = Word -> Encoding
encodeListLen Word
3
 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
1
 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode TyCon
con
 forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode [SomeTypeRep]
ks
encodeTypeRep (App TypeRep a
f TypeRep b
x)
  = Word -> Encoding
encodeListLen Word
3
 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
2
 forall a. Semigroup a => a -> a -> a
<> forall {k} (a :: k). TypeRep a -> Encoding
encodeTypeRep TypeRep a
f
 forall a. Semigroup a => a -> a -> a
<> forall {k} (a :: k). TypeRep a -> Encoding
encodeTypeRep TypeRep b
x
encodeTypeRep (Fun TypeRep arg
arg TypeRep res
res)
  = Word -> Encoding
encodeListLen Word
3
 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
3
 forall a. Semigroup a => a -> a -> a
<> forall {k} (a :: k). TypeRep a -> Encoding
encodeTypeRep TypeRep arg
arg
 forall a. Semigroup a => a -> a -> a
<> forall {k} (a :: k). TypeRep a -> Encoding
encodeTypeRep TypeRep res
res

-- | @since 0.2.0.0
instance Typeable a => Serialise (TypeRep (a :: k)) where
  encode :: TypeRep a -> Encoding
encode = forall {k} (a :: k). TypeRep a -> Encoding
encodeTypeRep
  decode :: forall s. Decoder s (TypeRep a)
decode = do
      SomeTypeRep TypeRep a
rep <- forall s. Decoder s SomeTypeRep
decodeSomeTypeRep
      case TypeRep a
rep forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` TypeRep a
expected of
        Just a :~~: a
HRefl -> forall (f :: * -> *) a. Applicative f => a -> f a
pure TypeRep a
rep
        Maybe (a :~~: a)
Nothing    -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
                      [ String
"Codec.CBOR.Class.decode(TypeRep): Type mismatch"
                      , String
"    Deserialised type: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show TypeRep a
rep
                      , String
"    Expected type:     " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show TypeRep a
expected
                      ]
    where expected :: TypeRep a
expected = forall {k} (a :: k). Typeable a => TypeRep a
typeRep :: TypeRep a

-- | @since 0.2.0.0
instance Serialise SomeTypeRep where
  encode :: SomeTypeRep -> Encoding
encode (SomeTypeRep TypeRep a
rep) = forall {k} (a :: k). TypeRep a -> Encoding
encodeTypeRep TypeRep a
rep
  decode :: forall s. Decoder s SomeTypeRep
decode = forall s. Decoder s SomeTypeRep
decodeSomeTypeRep

#else

-- | @since 0.2.0.0
instance Serialise TypeRep where
#if MIN_VERSION_base(4,8,0)
  encode (TypeRep fp tycon kirep tyrep)
    = encodeListLen 5
   <> encodeWord 0
   <> encode fp
   <> encode tycon
   <> encode kirep
   <> encode tyrep

  decode = do
    decodeListLenOf 5
    tag <- decodeWord
    case tag of
      0 -> do !fp    <- decode
              !tycon <- decode
              !kirep <- decode
              !tyrep <- decode
              return $! TypeRep fp tycon kirep tyrep
      _ -> fail "unexpected tag"
#else
  encode (TypeRep fp tycon tyrep)
    = encodeListLen 4
   <> encodeWord 0
   <> encode fp
   <> encode tycon
   <> encode tyrep

  decode = do
    decodeListLenOf 4
    tag <- decodeWord
    case tag of
      0 -> do !fp    <- decode
              !tycon <- decode
              !tyrep <- decode
              return $! TypeRep fp tycon tyrep
      _ -> fail "unexpected tag"
#endif

#endif /* !MIN_VERBOSE_base(4,10,0) */

--------------------------------------------------------------------------------
-- Time instances
--
-- CBOR has some special encodings for times/timestamps

-- | 'UTCTime' is encoded using the extended time format which is currently in
-- Internet Draft state,
-- https://tools.ietf.org/html/draft-bormann-cbor-time-tag-00.
--
-- @since 0.2.0.0
instance Serialise UTCTime where
    encode :: UTCTime -> Encoding
encode UTCTime
t =
        Word -> Encoding
encodeTag Word
1000
        forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeMapLen Word
2
        forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
1 forall a. Semigroup a => a -> a -> a
<> Int64 -> Encoding
encodeInt64 Int64
secs
        forall a. Semigroup a => a -> a -> a
<> Int -> Encoding
encodeInt (-Int
12) forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
encodeWord64 Word64
psecs
      where
        (Int64
secs, POSIXTime
frac) = case forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction forall a b. (a -> b) -> a -> b
$ UTCTime -> POSIXTime
utcTimeToPOSIXSeconds UTCTime
t of
                         -- fractional part must be positive
                         (Int64
secs', POSIXTime
frac')
                           | POSIXTime
frac' forall a. Ord a => a -> a -> Bool
< POSIXTime
0  -> (Int64
secs' forall a. Num a => a -> a -> a
- Int64
1, POSIXTime
frac' forall a. Num a => a -> a -> a
+ POSIXTime
1)
                           | Bool
otherwise -> (Int64
secs', POSIXTime
frac')
        psecs :: Word64
psecs = forall a b. (RealFrac a, Integral b) => a -> b
round forall a b. (a -> b) -> a -> b
$ POSIXTime
frac forall a. Num a => a -> a -> a
* POSIXTime
1000000000000

    decode :: forall s. Decoder s UTCTime
decode = do
      Word
tag <- forall s. Decoder s Word
decodeTag
      case Word
tag of
        Word
0 -> do Text
str <- forall s. Decoder s Text
decodeString
                case String -> Maybe UTCTime
parseUTCrfc3339 (Text -> String
Text.unpack Text
str) of
                  Just UTCTime
t  -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! UTCTime -> UTCTime
forceUTCTime UTCTime
t
                  Maybe UTCTime
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Could not parse RFC3339 date"

        Word
1 -> do
          TokenType
tt <- forall s. Decoder s TokenType
peekTokenType
          case TokenType
tt of
            TokenType
TypeUInt    -> forall a. Integral a => a -> UTCTime
utcFromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Word
decodeWord
            TokenType
TypeUInt64  -> forall a. Integral a => a -> UTCTime
utcFromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Word64
decodeWord64
            TokenType
TypeNInt    -> forall a. Integral a => a -> UTCTime
utcFromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Int
decodeInt
            TokenType
TypeNInt64  -> forall a. Integral a => a -> UTCTime
utcFromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Int64
decodeInt64
            TokenType
TypeInteger -> forall a. Integral a => a -> UTCTime
utcFromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Integer
decodeInteger
            TokenType
TypeFloat16 -> forall a. Real a => a -> UTCTime
utcFromReal forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Float
decodeFloat
            TokenType
TypeFloat32 -> forall a. Real a => a -> UTCTime
utcFromReal forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Float
decodeFloat
            TokenType
TypeFloat64 -> forall a. Real a => a -> UTCTime
utcFromReal forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Decoder s Double
decodeDouble
            TokenType
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected numeric type following tag 1 (epoch offset)"

        -- Extended time
        Word
1000 -> do
          Int
len <- forall s. Decoder s Int
decodeMapLen
          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len forall a. Eq a => a -> a -> Bool
/= Int
2) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected list of length two (UTCTime)"

          Int
k0 <- forall s. Decoder s Int
decodeInt
          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
k0 forall a. Eq a => a -> a -> Bool
/= Int
1) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected key 1 in extended time"
          Int64
v0 <- forall s. Decoder s Int64
decodeInt64

          Int
k1 <- forall s. Decoder s Int
decodeInt
          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
k1 forall a. Eq a => a -> a -> Bool
/= (-Int
12)) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected key -12 in extended time"
          Word64
v1 <- forall s. Decoder s Word64
decodeWord64
          let psecs :: Pico
              psecs :: Pico
psecs = forall a b. (Real a, Fractional b) => a -> b
realToFrac Word64
v1 forall a. Fractional a => a -> a -> a
/ Pico
1000000000000

              dt :: POSIXTime
              dt :: POSIXTime
dt = forall a b. (Real a, Fractional b) => a -> b
realToFrac Int64
v0 forall a. Num a => a -> a -> a
+ forall a b. (Real a, Fractional b) => a -> b
realToFrac Pico
psecs
          forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! UTCTime -> UTCTime
forceUTCTime (POSIXTime -> UTCTime
posixSecondsToUTCTime POSIXTime
dt)

        Word
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected timestamp (tag 0, 1, or 40)"

epoch :: UTCTime
epoch :: UTCTime
epoch = Day -> DiffTime -> UTCTime
UTCTime (Integer -> Int -> Int -> Day
fromGregorian Integer
1970 Int
1 Int
1) DiffTime
0

{-# INLINE utcFromIntegral #-}
utcFromIntegral :: Integral a => a -> UTCTime
utcFromIntegral :: forall a. Integral a => a -> UTCTime
utcFromIntegral a
i = POSIXTime -> UTCTime -> UTCTime
addUTCTime (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
i) UTCTime
epoch

{-# INLINE utcFromReal #-}
utcFromReal :: Real a => a -> UTCTime
utcFromReal :: forall a. Real a => a -> UTCTime
utcFromReal a
f = POSIXTime -> UTCTime -> UTCTime
addUTCTime (forall a. Fractional a => Rational -> a
fromRational (forall a. Real a => a -> Rational
toRational a
f)) UTCTime
epoch


-- | @'UTCTime'@ parsing, from a regular @'String'@.
parseUTCrfc3339  :: String -> Maybe UTCTime
#if MIN_VERSION_time(1,5,0)
parseUTCrfc3339 :: String -> Maybe UTCTime
parseUTCrfc3339  = forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
False TimeLocale
defaultTimeLocale String
"%Y-%m-%dT%H:%M:%S%Q%Z"
#else
parseUTCrfc3339  = parseTime        defaultTimeLocale "%Y-%m-%dT%H:%M:%S%Q%Z"
#endif

-- | Force the unnecessarily lazy @'UTCTime'@ representation.
forceUTCTime :: UTCTime -> UTCTime
forceUTCTime :: UTCTime -> UTCTime
forceUTCTime t :: UTCTime
t@(UTCTime !Day
_day !DiffTime
_daytime) = UTCTime
t

--------------------------------------------------------------------------------
-- Generic instances

-- Factored into two classes because this makes GHC optimize the
-- instances faster. This doesn't matter for builds of binary, but it
-- matters a lot for end-users who write 'instance Binary T'. See
-- also: https://ghc.haskell.org/trac/ghc/ticket/9630

-- | @since 0.2.0.0
class GSerialiseEncode f where
    -- | @since 0.2.0.0
    gencode  :: f a -> Encoding

-- | @since 0.2.0.0
class GSerialiseDecode f where
    -- | @since 0.2.0.0
    gdecode  :: Decoder s (f a)

-- | @since 0.2.0.0
instance GSerialiseEncode V1 where
    -- Data types without constructors are still serialised as null value
    gencode :: forall (a :: k). V1 a -> Encoding
gencode V1 a
_ = Encoding
encodeNull

-- | @since 0.2.0.0
instance GSerialiseDecode V1 where
    gdecode :: forall s (a :: k). Decoder s (V1 a)
gdecode   = forall a. HasCallStack => String -> a
error String
"V1 don't have contructors" forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall s. Decoder s ()
decodeNull

-- | @since 0.2.0.0
instance GSerialiseEncode U1 where
    -- Constructors without fields are serialised as null value
    gencode :: forall (a :: k). U1 a -> Encoding
gencode U1 a
_ = Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0

-- | @since 0.2.0.0
instance GSerialiseDecode U1 where
    gdecode :: forall s (a :: k). Decoder s (U1 a)
gdecode   = do
      Int
n <- forall s. Decoder s Int
decodeListLen
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n forall a. Eq a => a -> a -> Bool
/= Int
1) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expect list of length 1"
      Word
tag <- forall s. Decoder s Word
decodeWord
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word
tag forall a. Eq a => a -> a -> Bool
/= Word
0) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unexpected tag. Expect 0"
      forall (m :: * -> *) a. Monad m => a -> m a
return forall k (p :: k). U1 p
U1

-- | @since 0.2.0.0
instance GSerialiseEncode a => GSerialiseEncode (M1 i c a) where
    -- Metadata (constructor name, etc) is skipped
    gencode :: forall (a :: k). M1 i c a a -> Encoding
gencode = forall {k} (f :: k -> *) (a :: k).
GSerialiseEncode f =>
f a -> Encoding
gencode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1

-- | @since 0.2.0.0
instance GSerialiseDecode a => GSerialiseDecode (M1 i c a) where
    gdecode :: forall s (a :: k). Decoder s (M1 i c a a)
gdecode = forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {k} (f :: k -> *) s (a :: k).
GSerialiseDecode f =>
Decoder s (f a)
gdecode

-- | @since 0.2.0.0
instance Serialise a => GSerialiseEncode (K1 i a) where
    -- Constructor field (Could only appear in one-field & one-constructor
    -- data types). In all other cases we go through GSerialise{Sum,Prod}
    gencode :: forall (a :: k). K1 i a a -> Encoding
gencode (K1 a
a) = Word -> Encoding
encodeListLen Word
2
                  forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0
                  forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode a
a

-- | @since 0.2.0.0
instance Serialise a => GSerialiseDecode (K1 i a) where
    gdecode :: forall s (a :: k). Decoder s (K1 i a a)
gdecode = do
      Int
n <- forall s. Decoder s Int
decodeListLen
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n forall a. Eq a => a -> a -> Bool
/= Int
2) forall a b. (a -> b) -> a -> b
$
        forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expect list of length 2"
      Word
tag <- forall s. Decoder s Word
decodeWord
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word
tag forall a. Eq a => a -> a -> Bool
/= Word
0) forall a b. (a -> b) -> a -> b
$
        forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unexpected tag. Expects 0"
      forall k i c (p :: k). c -> K1 i c p
K1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance (GSerialiseProd f, GSerialiseProd g) => GSerialiseEncode (f :*: g) where
    -- Products are serialised as N-tuples with 0 constructor tag
    gencode :: forall (a :: k). (:*:) f g a -> Encoding
gencode (f a
f :*: g a
g)
        = Word -> Encoding
encodeListLen (forall {k} (f :: k -> *). GSerialiseProd f => Proxy f -> Word
nFields (forall {k} (t :: k). Proxy t
Proxy :: Proxy (f :*: g)) forall a. Num a => a -> a -> a
+ Word
1)
       forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
encodeWord Word
0
       forall a. Semigroup a => a -> a -> a
<> forall {k} (f :: k -> *) (a :: k).
GSerialiseProd f =>
f a -> Encoding
encodeSeq f a
f
       forall a. Semigroup a => a -> a -> a
<> forall {k} (f :: k -> *) (a :: k).
GSerialiseProd f =>
f a -> Encoding
encodeSeq g a
g

-- | @since 0.2.0.0
instance (GSerialiseProd f, GSerialiseProd g) => GSerialiseDecode (f :*: g) where
    gdecode :: forall s (a :: k). Decoder s ((:*:) f g a)
gdecode = do
      let nF :: Word
nF = forall {k} (f :: k -> *). GSerialiseProd f => Proxy f -> Word
nFields (forall {k} (t :: k). Proxy t
Proxy :: Proxy (f :*: g))
      Int
n <- forall s. Decoder s Int
decodeListLen
      -- TODO FIXME: signedness of list length
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n forall a. Eq a => a -> a -> Bool
/= Word
nF forall a. Num a => a -> a -> a
+ Word
1) forall a b. (a -> b) -> a -> b
$
        forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"Wrong number of fields: expected="forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show (Word
nFforall a. Num a => a -> a -> a
+Word
1)forall a. [a] -> [a] -> [a]
++String
" got="forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show Int
n
      Word
tag <- forall s. Decoder s Word
decodeWord
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word
tag forall a. Eq a => a -> a -> Bool
/= Word
0) forall a b. (a -> b) -> a -> b
$
        forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"unexpect tag (expect 0)"
      !f a
f <- forall {k} (f :: k -> *) s (a :: k).
GSerialiseProd f =>
Decoder s (f a)
gdecodeSeq
      !g a
g <- forall {k} (f :: k -> *) s (a :: k).
GSerialiseProd f =>
Decoder s (f a)
gdecodeSeq
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ f a
f forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g a
g

-- | @since 0.2.0.0
instance (GSerialiseSum f, GSerialiseSum g) => GSerialiseEncode (f :+: g) where
    -- Sum types are serialised as N-tuples and first element is
    -- constructor tag
    gencode :: forall (a :: k). (:+:) f g a -> Encoding
gencode (:+:) f g a
a = Word -> Encoding
encodeListLen (forall {k} (f :: k -> *) (a :: k). GSerialiseSum f => f a -> Word
numOfFields (:+:) f g a
a forall a. Num a => a -> a -> a
+ Word
1)
             forall a. Semigroup a => a -> a -> a
<> forall a. Serialise a => a -> Encoding
encode (forall {k} (f :: k -> *) (a :: k). GSerialiseSum f => f a -> Word
conNumber (:+:) f g a
a)
             forall a. Semigroup a => a -> a -> a
<> forall {k} (f :: k -> *) (a :: k).
GSerialiseSum f =>
f a -> Encoding
encodeSum (:+:) f g a
a

-- | @since 0.2.0.0
instance (GSerialiseSum f, GSerialiseSum g) => GSerialiseDecode (f :+: g) where
    gdecode :: forall s (a :: k). Decoder s ((:+:) f g a)
gdecode = do
        Int
n <- forall s. Decoder s Int
decodeListLen
        -- TODO FIXME: Again signedness
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n forall a. Eq a => a -> a -> Bool
== Int
0) forall a b. (a -> b) -> a -> b
$
          forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Empty list encountered for sum type"
        Word
nCon  <- forall s. Decoder s Word
decodeWord
        Word
trueN <- forall {k} (f :: k -> *) s.
GSerialiseSum f =>
Proxy f -> Word -> Decoder s Word
fieldsForCon (forall {k} (t :: k). Proxy t
Proxy :: Proxy (f :+: g)) Word
nCon
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
nforall a. Num a => a -> a -> a
-Int
1 forall a. Eq a => a -> a -> Bool
/= forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
trueN ) forall a b. (a -> b) -> a -> b
$
          forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"Number of fields mismatch: expected="forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show Word
trueNforall a. [a] -> [a] -> [a]
++String
" got="forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show Int
n
        forall {k} (f :: k -> *) s (a :: k).
GSerialiseSum f =>
Word -> Decoder s (f a)
decodeSum Word
nCon


-- | Serialization of product types
class GSerialiseProd f where
    -- | Number of fields in product type
    nFields   :: Proxy f -> Word
    -- | Encode fields sequentially without writing header
    encodeSeq :: f a -> Encoding
    -- | Decode fields sequentially without reading header
    gdecodeSeq :: Decoder s (f a)

-- | @since 0.2.0.0
instance (GSerialiseProd f, GSerialiseProd g) => GSerialiseProd (f :*: g) where
    nFields :: Proxy (f :*: g) -> Word
nFields Proxy (f :*: g)
_ = forall {k} (f :: k -> *). GSerialiseProd f => Proxy f -> Word
nFields (forall {k} (t :: k). Proxy t
Proxy :: Proxy f) forall a. Num a => a -> a -> a
+ forall {k} (f :: k -> *). GSerialiseProd f => Proxy f -> Word
nFields (forall {k} (t :: k). Proxy t
Proxy :: Proxy g)
    encodeSeq :: forall (a :: k). (:*:) f g a -> Encoding
encodeSeq (f a
f :*: g a
g) = forall {k} (f :: k -> *) (a :: k).
GSerialiseProd f =>
f a -> Encoding
encodeSeq f a
f forall a. Semigroup a => a -> a -> a
<> forall {k} (f :: k -> *) (a :: k).
GSerialiseProd f =>
f a -> Encoding
encodeSeq g a
g
    gdecodeSeq :: forall s (a :: k). Decoder s ((:*:) f g a)
gdecodeSeq = do !f a
f <- forall {k} (f :: k -> *) s (a :: k).
GSerialiseProd f =>
Decoder s (f a)
gdecodeSeq
                    !g a
g <- forall {k} (f :: k -> *) s (a :: k).
GSerialiseProd f =>
Decoder s (f a)
gdecodeSeq
                    forall (m :: * -> *) a. Monad m => a -> m a
return (f a
f forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g a
g)

-- | @since 0.2.0.0
instance GSerialiseProd U1 where
    -- N.B. Could only be reached when one of constructors in sum type
    --      don't have parameters
    nFields :: Proxy U1 -> Word
nFields   Proxy U1
_ = Word
0
    encodeSeq :: forall (a :: k). U1 a -> Encoding
encodeSeq U1 a
_ = forall a. Monoid a => a
mempty
    gdecodeSeq :: forall s (a :: k). Decoder s (U1 a)
gdecodeSeq  = forall (m :: * -> *) a. Monad m => a -> m a
return forall k (p :: k). U1 p
U1

-- | @since 0.2.0.0
instance (Serialise a) => GSerialiseProd (K1 i a) where
    -- Ordinary field
    nFields :: Proxy (K1 i a) -> Word
nFields    Proxy (K1 i a)
_     = Word
1
    encodeSeq :: forall (a :: k). K1 i a a -> Encoding
encodeSeq (K1 a
f) = forall a. Serialise a => a -> Encoding
encode a
f
    gdecodeSeq :: forall s (a :: k). Decoder s (K1 i a a)
gdecodeSeq       = forall k i c (p :: k). c -> K1 i c p
K1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. Serialise a => Decoder s a
decode

-- | @since 0.2.0.0
instance (i ~ S, GSerialiseProd f) => GSerialiseProd (M1 i c f) where
    -- We skip metadata
    nFields :: Proxy (M1 i c f) -> Word
nFields     Proxy (M1 i c f)
_     = Word
1
    encodeSeq :: forall (a :: k). M1 i c f a -> Encoding
encodeSeq  (M1 f a
f) = forall {k} (f :: k -> *) (a :: k).
GSerialiseProd f =>
f a -> Encoding
encodeSeq f a
f
    gdecodeSeq :: forall s (a :: k). Decoder s (M1 i c f a)
gdecodeSeq        = forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {k} (f :: k -> *) s (a :: k).
GSerialiseProd f =>
Decoder s (f a)
gdecodeSeq

-- | Serialization of sum types
--
-- @since 0.2.0.0
class GSerialiseSum f where
    -- | Number of constructor of given value
    conNumber   :: f a -> Word
    -- | Number of fields of given value
    numOfFields :: f a -> Word
    -- | Encode field
    encodeSum   :: f a  -> Encoding

    -- | Decode field
    decodeSum     :: Word -> Decoder s (f a)
    -- | Number of constructors
    nConstructors :: Proxy f -> Word
    -- | Number of fields for given constructor number
    fieldsForCon  :: Proxy f -> Word -> Decoder s Word

-- | @since 0.2.0.0
instance (GSerialiseSum f, GSerialiseSum g) => GSerialiseSum (f :+: g) where
    conNumber :: forall (a :: k). (:+:) f g a -> Word
conNumber (:+:) f g a
x = case (:+:) f g a
x of
      L1 f a
f -> forall {k} (f :: k -> *) (a :: k). GSerialiseSum f => f a -> Word
conNumber f a
f
      R1 g a
g -> forall {k} (f :: k -> *) (a :: k). GSerialiseSum f => f a -> Word
conNumber g a
g forall a. Num a => a -> a -> a
+ forall {k} (f :: k -> *). GSerialiseSum f => Proxy f -> Word
nConstructors (forall {k} (t :: k). Proxy t
Proxy :: Proxy f)
    numOfFields :: forall (a :: k). (:+:) f g a -> Word
numOfFields (:+:) f g a
x = case (:+:) f g a
x of
      L1 f a
f -> forall {k} (f :: k -> *) (a :: k). GSerialiseSum f => f a -> Word
numOfFields f a
f
      R1 g a
g -> forall {k} (f :: k -> *) (a :: k). GSerialiseSum f => f a -> Word
numOfFields g a
g
    encodeSum :: forall (a :: k). (:+:) f g a -> Encoding
encodeSum (:+:) f g a
x = case (:+:) f g a
x of
      L1 f a
f -> forall {k} (f :: k -> *) (a :: k).
GSerialiseSum f =>
f a -> Encoding
encodeSum f a
f
      R1 g a
g -> forall {k} (f :: k -> *) (a :: k).
GSerialiseSum f =>
f a -> Encoding
encodeSum g a
g

    nConstructors :: Proxy (f :+: g) -> Word
nConstructors Proxy (f :+: g)
_ = forall {k} (f :: k -> *). GSerialiseSum f => Proxy f -> Word
nConstructors (forall {k} (t :: k). Proxy t
Proxy :: Proxy f)
                    forall a. Num a => a -> a -> a
+ forall {k} (f :: k -> *). GSerialiseSum f => Proxy f -> Word
nConstructors (forall {k} (t :: k). Proxy t
Proxy :: Proxy g)

    fieldsForCon :: forall s. Proxy (f :+: g) -> Word -> Decoder s Word
fieldsForCon Proxy (f :+: g)
_ Word
n | Word
n forall a. Ord a => a -> a -> Bool
< Word
nL    = forall {k} (f :: k -> *) s.
GSerialiseSum f =>
Proxy f -> Word -> Decoder s Word
fieldsForCon (forall {k} (t :: k). Proxy t
Proxy :: Proxy f) Word
n
                     | Bool
otherwise = forall {k} (f :: k -> *) s.
GSerialiseSum f =>
Proxy f -> Word -> Decoder s Word
fieldsForCon (forall {k} (t :: k). Proxy t
Proxy :: Proxy g) (Word
n forall a. Num a => a -> a -> a
- Word
nL)
      where
        nL :: Word
nL = forall {k} (f :: k -> *). GSerialiseSum f => Proxy f -> Word
nConstructors (forall {k} (t :: k). Proxy t
Proxy :: Proxy f)

    decodeSum :: forall s (a :: k). Word -> Decoder s ((:+:) f g a)
decodeSum Word
nCon | Word
nCon forall a. Ord a => a -> a -> Bool
< Word
nL = forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {k} (f :: k -> *) s (a :: k).
GSerialiseSum f =>
Word -> Decoder s (f a)
decodeSum Word
nCon
                   | Bool
otherwise = forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {k} (f :: k -> *) s (a :: k).
GSerialiseSum f =>
Word -> Decoder s (f a)
decodeSum (Word
nCon forall a. Num a => a -> a -> a
- Word
nL)
      where
        nL :: Word
nL = forall {k} (f :: k -> *). GSerialiseSum f => Proxy f -> Word
nConstructors (forall {k} (t :: k). Proxy t
Proxy :: Proxy f)

-- | @since 0.2.0.0
instance (i ~ C, GSerialiseProd f) => GSerialiseSum (M1 i c f) where
    conNumber :: forall (a :: k). M1 i c f a -> Word
conNumber    M1 i c f a
_     = Word
0
    numOfFields :: forall (a :: k). M1 i c f a -> Word
numOfFields  M1 i c f a
_     = forall {k} (f :: k -> *). GSerialiseProd f => Proxy f -> Word
nFields (forall {k} (t :: k). Proxy t
Proxy :: Proxy f)
    encodeSum :: forall (a :: k). M1 i c f a -> Encoding
encodeSum   (M1 f a
f) = forall {k} (f :: k -> *) (a :: k).
GSerialiseProd f =>
f a -> Encoding
encodeSeq f a
f

    nConstructors :: Proxy (M1 i c f) -> Word
nConstructors  Proxy (M1 i c f)
_ = Word
1
    fieldsForCon :: forall s. Proxy (M1 i c f) -> Word -> Decoder s Word
fieldsForCon Proxy (M1 i c f)
_ Word
0 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall {k} (f :: k -> *). GSerialiseProd f => Proxy f -> Word
nFields (forall {k} (t :: k). Proxy t
Proxy :: Proxy f)
    fieldsForCon Proxy (M1 i c f)
_ Word
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Bad constructor number"
    decodeSum :: forall s (a :: k). Word -> Decoder s (M1 i c f a)
decodeSum      Word
0 = forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {k} (f :: k -> *) s (a :: k).
GSerialiseProd f =>
Decoder s (f a)
gdecodeSeq
    decodeSum      Word
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"bad constructor number"