{-# LANGUAGE CPP #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}
#if defined(__GLASGOW_HASKELL__)
# if __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE Trustworthy #-}
# endif
# if __GLASGOW_HASKELL__ >= 708
{-# LANGUAGE EmptyCase #-}
# endif
#endif
#ifndef MIN_VERSION_base
#define MIN_VERSION_base(x,y,z) 1
#endif
--------------------------------------------------------------------
-- |
-- Copyright :  (c) Edward Kmett 2013-2015
-- License   :  BSD3
-- Maintainer:  Edward Kmett <ekmett@gmail.com>
-- Stability :  experimental
-- Portability: non-portable
--
-- This module contains two main classes, each providing methods to
-- serialize and deserialize types. 'Serial' is the primary class,
-- to be used for the canonical way to serialize a specific
-- type. 'SerialEndian' is used to provide endian-specific methods
-- for serializing a type.
--------------------------------------------------------------------
module Data.Bytes.Serial
  (
  -- * Serialization
    Serial(..)
  -- * Specifying endianness
  , SerialEndian(..)
  -- * Higher-order
  -- $higher
  , Serial1(..)
  , serialize1, deserialize1
  , Serial2(..)
  , serialize2, deserialize2
  -- * Storable
  , store, restore
  -- * Generics
  -- $generics
  , GSerial(..)
  , GSerialEndian(..)
  , GSerial1(..)
  ) where

import Control.Applicative
import Control.Monad
import qualified Control.Monad.Fail as MonadFail
import qualified Data.Foldable as F
import Data.Bytes.Get
import Data.Bytes.Put
import Data.Bytes.Signed
import Data.Bytes.VarInt
import Data.ByteString.Internal
import Data.ByteString.Lazy as Lazy
import Data.ByteString as Strict
import Data.Int
import Data.Bits
import Data.Monoid as Monoid
import Data.Functor.Identity as Functor
import Data.Functor.Constant as Functor
import Data.Functor.Product  as Functor
import Data.Functor.Reverse  as Functor
import Data.Hashable (Hashable)
import qualified Data.HashMap.Lazy as HMap
import qualified Data.HashSet      as HSet
import qualified Data.List.NonEmpty as NEL
import Data.Time
import Data.Time.Clock.TAI
import qualified Data.IntMap as IMap
import qualified Data.IntSet as ISet
import qualified Data.Map as Map
import qualified Data.Scientific as Sci
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import Data.Text as SText
import Data.Text.Encoding as SText
import Data.Text.Lazy as LText
import Data.Text.Lazy.Encoding as LText
import Data.Version (Version(..))
import Data.Void
import Data.Word
import Data.Fixed
import Data.Ratio
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Storable
import GHC.Exts (Down(..))
import GHC.Generics
import System.IO.Unsafe
import Numeric.Natural

foreign import ccall floatToWord32 :: Float -> Word32
foreign import ccall word32ToFloat :: Word32 -> Float
foreign import ccall doubleToWord64 :: Double -> Word64
foreign import ccall word64ToDouble :: Word64 -> Double

-- $setup
-- >>> import Data.Bytes.Get
-- >>> import Data.Bytes.Put
-- >>> import Data.Bytes.VarInt
-- >>> import Data.Fixed
-- >>> import Data.Ratio (Ratio, (%))
-- >>> import Data.Time
-- >>> import Data.Time.Clock
-- >>> import Data.Time.Clock.TAI
-- >>> import Data.Word
-- >>> import Numeric.Natural

------------------------------------------------------------------------------
-- Endianness-Dependant Serialization
------------------------------------------------------------------------------

{-| Methods to serialize and deserialize type 'a' to a big and little endian
binary representations. Methods suffixed with "host" are automatically defined
to use equal the methods corresponding to the current machine's native
endianness, but they can be overridden.
-}
class SerialEndian a where
  serializeBE :: MonadPut m => a -> m ()
#ifndef HLINT
  default serializeBE :: (MonadPut m, GSerialEndian (Rep a), Generic a) => a -> m ()
  serializeBE = forall (f :: * -> *) (m :: * -> *) a.
(GSerialEndian f, MonadPut m) =>
f a -> m ()
gserializeBE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a x. Generic a => a -> Rep a x
from
#endif

  deserializeBE :: MonadGet m => m a
#ifndef HLINT
  default deserializeBE :: (MonadGet m, GSerialEndian (Rep a), Generic a) => m a
  deserializeBE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a x. Generic a => Rep a x -> a
to forall (f :: * -> *) (m :: * -> *) a.
(GSerialEndian f, MonadGet m) =>
m (f a)
gdeserializeBE
#endif

  serializeLE :: MonadPut m => a -> m ()
#ifndef HLINT
  default serializeLE :: (MonadPut m, GSerialEndian (Rep a), Generic a) => a -> m ()
  serializeLE = forall (f :: * -> *) (m :: * -> *) a.
(GSerialEndian f, MonadPut m) =>
f a -> m ()
gserializeLE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a x. Generic a => a -> Rep a x
from
#endif

  deserializeLE :: MonadGet m => m a
#ifndef HLINT
  default deserializeLE :: (MonadGet m, GSerialEndian (Rep a), Generic a) => m a
  deserializeLE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a x. Generic a => Rep a x -> a
to forall (f :: * -> *) (m :: * -> *) a.
(GSerialEndian f, MonadGet m) =>
m (f a)
gdeserializeLE
#endif

  serializeHost :: MonadPut m => a -> m ()
  deserializeHost :: MonadGet m => m a
#ifdef WORDS_BIGENDIAN
  serializeHost = serializeBE
  deserializeHost = deserializeBE
#else
  serializeHost = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeLE
  deserializeHost = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeLE
#endif

instance SerialEndian Double where
  serializeBE :: forall (m :: * -> *). MonadPut m => Double -> m ()
serializeBE = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Word64
doubleToWord64
  deserializeBE :: forall (m :: * -> *). MonadGet m => m Double
deserializeBE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Word64 -> Double
word64ToDouble forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

  serializeLE :: forall (m :: * -> *). MonadPut m => Double -> m ()
serializeLE = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeLE forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Word64
doubleToWord64
  deserializeLE :: forall (m :: * -> *). MonadGet m => m Double
deserializeLE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Word64 -> Double
word64ToDouble forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeLE

instance SerialEndian Float where
  serializeBE :: forall (m :: * -> *). MonadPut m => Float -> m ()
serializeBE = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Word32
floatToWord32
  deserializeBE :: forall (m :: * -> *). MonadGet m => m Float
deserializeBE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Word32 -> Float
word32ToFloat forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

  serializeLE :: forall (m :: * -> *). MonadPut m => Float -> m ()
serializeLE = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeLE forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Word32
floatToWord32
  deserializeLE :: forall (m :: * -> *). MonadGet m => m Float
deserializeLE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Word32 -> Float
word32ToFloat forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeLE

instance SerialEndian Char where
  serializeBE :: forall (m :: * -> *). MonadPut m => Char -> m ()
serializeBE = forall (m :: * -> *). MonadPut m => Word32 -> m ()
putWord32be forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum
  deserializeBE :: forall (m :: * -> *). MonadGet m => m Char
deserializeBE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (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 (m :: * -> *). MonadGet m => m Word32
getWord32be

  serializeLE :: forall (m :: * -> *). MonadPut m => Char -> m ()
serializeLE = forall (m :: * -> *). MonadPut m => Word32 -> m ()
putWord32le forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum
  deserializeLE :: forall (m :: * -> *). MonadGet m => m Char
deserializeLE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (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 (m :: * -> *). MonadGet m => m Word32
getWord32le

instance SerialEndian Word64 where
  serializeBE :: forall (m :: * -> *). MonadPut m => Word64 -> m ()
serializeBE = forall (m :: * -> *). MonadPut m => Word64 -> m ()
putWord64be
  deserializeBE :: forall (m :: * -> *). MonadGet m => m Word64
deserializeBE = forall (m :: * -> *). MonadGet m => m Word64
getWord64be

  serializeLE :: forall (m :: * -> *). MonadPut m => Word64 -> m ()
serializeLE = forall (m :: * -> *). MonadPut m => Word64 -> m ()
putWord64le
  deserializeLE :: forall (m :: * -> *). MonadGet m => m Word64
deserializeLE = forall (m :: * -> *). MonadGet m => m Word64
getWord64le

instance SerialEndian Word32 where
  serializeBE :: forall (m :: * -> *). MonadPut m => Word32 -> m ()
serializeBE = forall (m :: * -> *). MonadPut m => Word32 -> m ()
putWord32be
  deserializeBE :: forall (m :: * -> *). MonadGet m => m Word32
deserializeBE = forall (m :: * -> *). MonadGet m => m Word32
getWord32be

  serializeLE :: forall (m :: * -> *). MonadPut m => Word32 -> m ()
serializeLE = forall (m :: * -> *). MonadPut m => Word32 -> m ()
putWord32le
  deserializeLE :: forall (m :: * -> *). MonadGet m => m Word32
deserializeLE = forall (m :: * -> *). MonadGet m => m Word32
getWord32le

instance SerialEndian Word16 where
  serializeBE :: forall (m :: * -> *). MonadPut m => Word16 -> m ()
serializeBE = forall (m :: * -> *). MonadPut m => Word16 -> m ()
putWord16be
  deserializeBE :: forall (m :: * -> *). MonadGet m => m Word16
deserializeBE = forall (m :: * -> *). MonadGet m => m Word16
getWord16be

  serializeLE :: forall (m :: * -> *). MonadPut m => Word16 -> m ()
serializeLE = forall (m :: * -> *). MonadPut m => Word16 -> m ()
putWord16le
  deserializeLE :: forall (m :: * -> *). MonadGet m => m Word16
deserializeLE = forall (m :: * -> *). MonadGet m => m Word16
getWord16le

instance SerialEndian Int64 where
  serializeBE :: forall (m :: * -> *). MonadPut m => Int64 -> m ()
serializeBE = forall (m :: * -> *). MonadPut m => Word64 -> m ()
putWord64be forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
  deserializeBE :: forall (m :: * -> *). MonadGet m => m Int64
deserializeBE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (m :: * -> *). MonadGet m => m Word64
getWord64be

  serializeLE :: forall (m :: * -> *). MonadPut m => Int64 -> m ()
serializeLE = forall (m :: * -> *). MonadPut m => Word64 -> m ()
putWord64le forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
  deserializeLE :: forall (m :: * -> *). MonadGet m => m Int64
deserializeLE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (m :: * -> *). MonadGet m => m Word64
getWord64le

instance SerialEndian Int32 where
  serializeBE :: forall (m :: * -> *). MonadPut m => Int32 -> m ()
serializeBE = forall (m :: * -> *). MonadPut m => Word32 -> m ()
putWord32be forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
  deserializeBE :: forall (m :: * -> *). MonadGet m => m Int32
deserializeBE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (m :: * -> *). MonadGet m => m Word32
getWord32be

  serializeLE :: forall (m :: * -> *). MonadPut m => Int32 -> m ()
serializeLE = forall (m :: * -> *). MonadPut m => Word32 -> m ()
putWord32le forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
  deserializeLE :: forall (m :: * -> *). MonadGet m => m Int32
deserializeLE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (m :: * -> *). MonadGet m => m Word32
getWord32le

instance SerialEndian Int16 where
  serializeBE :: forall (m :: * -> *). MonadPut m => Int16 -> m ()
serializeBE = forall (m :: * -> *). MonadPut m => Word16 -> m ()
putWord16be forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
  deserializeBE :: forall (m :: * -> *). MonadGet m => m Int16
deserializeBE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (m :: * -> *). MonadGet m => m Word16
getWord16be

  serializeLE :: forall (m :: * -> *). MonadPut m => Int16 -> m ()
serializeLE = forall (m :: * -> *). MonadPut m => Word16 -> m ()
putWord16le forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
  deserializeLE :: forall (m :: * -> *). MonadGet m => m Int16
deserializeLE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (m :: * -> *). MonadGet m => m Word16
getWord16le

------------------------------------------------------------------------------
-- Serialization
------------------------------------------------------------------------------

{-| Methods to serialize and deserialize type 'a' to a binary representation

Instances provided here for fixed-with Integers and Words are big endian.
Instances for strict and lazy bytestrings store also the length of bytestring
big endian. Instances for Word and Int are host endian as they are
machine-specific types.
-}
class Serial a where
  serialize :: MonadPut m => a -> m ()
#ifndef HLINT
  default serialize :: (MonadPut m, GSerial (Rep a), Generic a) => a -> m ()
  serialize = forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadPut m) =>
f a -> m ()
gserialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a x. Generic a => a -> Rep a x
from
#endif

  deserialize :: MonadGet m => m a
#ifndef HLINT
  default deserialize :: (MonadGet m, GSerial (Rep a), Generic a) => m a
  deserialize = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a x. Generic a => Rep a x -> a
to forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadGet m) =>
m (f a)
gdeserialize
#endif

instance Serial Strict.ByteString where
  serialize :: forall (m :: * -> *). MonadPut m => ByteString -> m ()
serialize ByteString
bs = forall (m :: * -> *). MonadPut m => Word32 -> m ()
putWord32be (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
Strict.length ByteString
bs)) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *). MonadPut m => ByteString -> m ()
putByteString ByteString
bs
  deserialize :: forall (m :: * -> *). MonadGet m => m ByteString
deserialize = do
    Word32
n <- forall (m :: * -> *). MonadGet m => m Word32
getWord32be
    forall (m :: * -> *). MonadGet m => Int -> m ByteString
getByteString (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
n)

instance Serial Lazy.ByteString where
  serialize :: forall (m :: * -> *). MonadPut m => ByteString -> m ()
serialize ByteString
bs = forall (m :: * -> *). MonadPut m => Word64 -> m ()
putWord64be (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
Lazy.length ByteString
bs)) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *). MonadPut m => ByteString -> m ()
putLazyByteString ByteString
bs
  deserialize :: forall (m :: * -> *). MonadGet m => m ByteString
deserialize = do
    Word64
n <- forall (m :: * -> *). MonadGet m => m Word64
getWord64be
    forall (m :: * -> *). MonadGet m => Int64 -> m ByteString
getLazyByteString (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)

instance Serial SText.Text where
  serialize :: forall (m :: * -> *). MonadPut m => Text -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
SText.encodeUtf8
  deserialize :: forall (m :: * -> *). MonadGet m => m Text
deserialize = ByteString -> Text
SText.decodeUtf8 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial LText.Text where
  serialize :: forall (m :: * -> *). MonadPut m => Text -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
LText.encodeUtf8
  deserialize :: forall (m :: * -> *). MonadGet m => m Text
deserialize = ByteString -> Text
LText.decodeUtf8 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial ()
instance Serial a => Serial [a]
instance Serial a => Serial (Maybe a)
instance (Serial a, Serial b) => Serial (Either a b)
instance (Serial a, Serial b) => Serial (a, b)
instance (Serial a, Serial b, Serial c) => Serial (a, b, c)
instance (Serial a, Serial b, Serial c, Serial d) => Serial (a, b, c, d)
instance (Serial a, Serial b, Serial c, Serial d, Serial e) => Serial (a, b, c, d, e)

instance Serial Bool

-- | serialize any 'Storable' in a host-specific format.
store :: (MonadPut m, Storable a) => a -> m ()
store :: forall (m :: * -> *) a. (MonadPut m, Storable a) => a -> m ()
store a
a = forall (m :: * -> *). MonadPut m => ByteString -> m ()
putByteString ByteString
bs
  where bs :: ByteString
bs = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ Int -> (Ptr Word8 -> IO ()) -> IO ByteString
create (forall a. Storable a => a -> Int
sizeOf a
a) forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
p -> forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
p) a
a

-- | deserialize any 'Storable' in a host-specific format.
restore :: forall m a. (MonadGet m, Storable a) => m a
restore :: forall (m :: * -> *) a. (MonadGet m, Storable a) => m a
restore = do
  let required :: Int
required = forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: a)
#if MIN_VERSION_bytestring(0,11,0)
  let o :: Int
o = Int
0
  BS ForeignPtr Word8
fp Int
n
#else
  PS fp o n
#endif
    <- forall (m :: * -> *). MonadGet m => Int -> m ByteString
getByteString Int
required
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
n forall a. Ord a => a -> a -> Bool
>= Int
required) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => String -> m a
MonadFail.fail String
"restore: Required more bytes"
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fp forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Word8
p Int
o

instance Serial Double where
  serialize :: forall (m :: * -> *). MonadPut m => Double -> m ()
serialize = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE
  deserialize :: forall (m :: * -> *). MonadGet m => m Double
deserialize = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

instance Serial Float where
  serialize :: forall (m :: * -> *). MonadPut m => Float -> m ()
serialize = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE
  deserialize :: forall (m :: * -> *). MonadGet m => m Float
deserialize = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

instance Serial Char where
  serialize :: forall (m :: * -> *). MonadPut m => Char -> m ()
serialize = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE
  deserialize :: forall (m :: * -> *). MonadGet m => m Char
deserialize = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

 -- host endian
instance Serial Word where
  serialize :: forall (m :: * -> *). MonadPut m => Word -> m ()
serialize = forall (m :: * -> *). MonadPut m => Word -> m ()
putWordhost
  deserialize :: forall (m :: * -> *). MonadGet m => m Word
deserialize = forall (m :: * -> *). MonadGet m => m Word
getWordhost

instance Serial Word64 where
  serialize :: forall (m :: * -> *). MonadPut m => Word64 -> m ()
serialize = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE
  deserialize :: forall (m :: * -> *). MonadGet m => m Word64
deserialize = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

instance Serial Word32 where
  serialize :: forall (m :: * -> *). MonadPut m => Word32 -> m ()
serialize = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE
  deserialize :: forall (m :: * -> *). MonadGet m => m Word32
deserialize = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

instance Serial Word16 where
  serialize :: forall (m :: * -> *). MonadPut m => Word16 -> m ()
serialize = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE
  deserialize :: forall (m :: * -> *). MonadGet m => m Word16
deserialize = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

instance Serial Word8 where
  serialize :: forall (m :: * -> *). MonadPut m => Word8 -> m ()
serialize = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8
  deserialize :: forall (m :: * -> *). MonadGet m => m Word8
deserialize = forall (m :: * -> *). MonadGet m => m Word8
getWord8

 -- host endian
instance Serial Int where
  serialize :: forall (m :: * -> *). MonadPut m => Int -> m ()
serialize = forall (m :: * -> *). MonadPut m => Word -> m ()
putWordhost forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
  deserialize :: forall (m :: * -> *). MonadGet m => m Int
deserialize = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (m :: * -> *). MonadGet m => m Word
getWordhost

instance Serial Int64 where
  serialize :: forall (m :: * -> *). MonadPut m => Int64 -> m ()
serialize = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE
  deserialize :: forall (m :: * -> *). MonadGet m => m Int64
deserialize = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

instance Serial Int32 where
  serialize :: forall (m :: * -> *). MonadPut m => Int32 -> m ()
serialize = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE
  deserialize :: forall (m :: * -> *). MonadGet m => m Int32
deserialize = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

instance Serial Int16 where
  serialize :: forall (m :: * -> *). MonadPut m => Int16 -> m ()
serialize = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE
  deserialize :: forall (m :: * -> *). MonadGet m => m Int16
deserialize = forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

instance Serial Int8 where
  serialize :: forall (m :: * -> *). MonadPut m => Int8 -> m ()
serialize = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
  deserialize :: forall (m :: * -> *). MonadGet m => m Int8
deserialize = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (m :: * -> *). MonadGet m => m Word8
getWord8

instance Serial Sci.Scientific where
  serialize :: forall (m :: * -> *). MonadPut m => Scientific -> m ()
serialize Scientific
s = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize (Scientific -> Integer
Sci.coefficient Scientific
s, Scientific -> Int
Sci.base10Exponent Scientific
s)
  deserialize :: forall (m :: * -> *). MonadGet m => m Scientific
deserialize = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Integer -> Int -> Scientific
Sci.scientific forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial Void where
  serialize :: forall (m :: * -> *). MonadPut m => Void -> m ()
serialize = forall a. Void -> a
absurd
  deserialize :: forall (m :: * -> *). MonadGet m => m Void
deserialize = forall (m :: * -> *) a. MonadFail m => String -> m a
MonadFail.fail String
"I looked into the void."

instance Serial ISet.IntSet where
  serialize :: forall (m :: * -> *). MonadPut m => IntSet -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntSet -> [Int]
ISet.toAscList
  deserialize :: forall (m :: * -> *). MonadGet m => m IntSet
deserialize = [Int] -> IntSet
ISet.fromList forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial a => Serial (Seq.Seq a) where
  serialize :: forall (m :: * -> *). MonadPut m => Seq a -> m ()
serialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserialize :: forall (m :: * -> *). MonadGet m => m (Seq a)
deserialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial a => Serial (NEL.NonEmpty a) where
  serialize :: forall (m :: * -> *). MonadPut m => NonEmpty a -> m ()
serialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserialize :: forall (m :: * -> *). MonadGet m => m (NonEmpty a)
deserialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance (Serial a, Ord a) => Serial (Set.Set a) where
  serialize :: forall (m :: * -> *). MonadPut m => Set a -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Set a -> [a]
Set.toAscList
  deserialize :: forall (m :: * -> *). MonadGet m => m (Set a)
deserialize = forall a. Ord a => [a] -> Set a
Set.fromList forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial v => Serial (IMap.IntMap v) where
  serialize :: forall (m :: * -> *). MonadPut m => IntMap v -> m ()
serialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserialize :: forall (m :: * -> *). MonadGet m => m (IntMap v)
deserialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance (Serial k, Serial v, Ord k) => Serial (Map.Map k v) where
  serialize :: forall (m :: * -> *). MonadPut m => Map k v -> m ()
serialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserialize :: forall (m :: * -> *). MonadGet m => m (Map k v)
deserialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance (Serial k, Serial v, Hashable k, Eq k) => Serial (HMap.HashMap k v) where
  serialize :: forall (m :: * -> *). MonadPut m => HashMap k v -> m ()
serialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserialize :: forall (m :: * -> *). MonadGet m => m (HashMap k v)
deserialize = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance (Serial v, Hashable v, Eq v) => Serial (HSet.HashSet v) where
  serialize :: forall (m :: * -> *). MonadPut m => HashSet v -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> [a]
HSet.toList
  deserialize :: forall (m :: * -> *). MonadGet m => m (HashSet v)
deserialize = forall a. (Eq a, Hashable a) => [a] -> HashSet a
HSet.fromList forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

putVarInt :: (MonadPut m, Integral a, Bits a) => a -> m ()
putVarInt :: forall (m :: * -> *) a.
(MonadPut m, Integral a, Bits a) =>
a -> m ()
putVarInt a
n
  | a
n forall a. Ord a => a -> a -> Bool
< a
0x80 = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n
  | Bool
otherwise = do
    forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
setBit (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n) Int
7
    forall (m :: * -> *) a.
(MonadPut m, Integral a, Bits a) =>
a -> m ()
putVarInt forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftR a
n Int
7
{-# INLINE putVarInt #-}

getVarInt :: (MonadGet m, Num b, Bits b) => Word8 -> m b
getVarInt :: forall (m :: * -> *) b. (MonadGet m, Num b, Bits b) => Word8 -> m b
getVarInt Word8
n
  | forall a. Bits a => a -> Int -> Bool
testBit Word8
n Int
7 = do
    VarInt b
m <- forall (m :: * -> *). MonadGet m => m Word8
getWord8 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) b. (MonadGet m, Num b, Bits b) => Word8 -> m b
getVarInt
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftL b
m Int
7 forall a. Bits a => a -> a -> a
.|. forall a. Bits a => a -> Int -> a
clearBit (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
n) Int
7
  | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
n
{-# INLINE getVarInt #-}


-- | Integer/Word types serialized to base-128 variable-width ints.
--
-- >>> import Data.Monoid (mconcat)
-- >>> import qualified Data.ByteString.Lazy as BSL
-- >>> mconcat $ BSL.toChunks $ runPutL $ serialize (97 :: Word64)
-- "\NUL\NUL\NUL\NUL\NUL\NUL\NULa"
-- >>> mconcat $ BSL.toChunks $ runPutL $ serialize (97 :: VarInt Word64)
-- "a"
instance (Bits n, Integral n, Bits (Unsigned n), Integral (Unsigned n)) => Serial (VarInt n) where
  serialize :: forall (m :: * -> *). MonadPut m => VarInt n -> m ()
serialize (VarInt n
n) = forall (m :: * -> *) a.
(MonadPut m, Integral a, Bits a) =>
a -> m ()
putVarInt forall a b. (a -> b) -> a -> b
$ forall i. (Integral i, Num (Unsigned i)) => i -> Unsigned i
unsigned n
n
  {-# INLINE serialize #-}
  deserialize :: forall (m :: * -> *). MonadGet m => m (VarInt n)
deserialize = forall (m :: * -> *). MonadGet m => m Word8
getWord8 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) b. (MonadGet m, Num b, Bits b) => Word8 -> m b
getVarInt
  {-# INLINE deserialize #-}

-- |
-- >>> (runGetL deserialize $ runPutL $ serialize (1822304234^100::Integer))::Integer
-- 115368812579128172803867366576339947332796540054052185472042218522037227934707037623902492207671987696439966697503243972076991940820348847422930433939639982092916577692754723458548819441583937289395076910527534916776189405228720063994377687015476947534961767053653973945346259230972683338173842343243493433367681264359887291905132383269175086733345253389374961758293922003996035662362278340494093804835649459223465051596978792130073960666112508481814461273829244289795707398202762289955919352549768394583446336873179280924584333491364188425976869717125645749497258775598562132278030402205794994603544837805140410310712693778605743100915046769381631247123664460203591228745772887977959388457679427407639421147498028487544882346912935398848298806021505673449774474457435816552278997100556732447852816961683577731381792363312695347606768120122976105200574809419685234274705929886121600174028733812771637390342332436695318974693376
instance Serial Integer where
  serialize :: forall (m :: * -> *). MonadPut m => Integer -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. n -> VarInt n
VarInt
  deserialize :: forall (m :: * -> *). MonadGet m => m Integer
deserialize = forall n. VarInt n -> n
unVarInt forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

-- |
-- >>> runGetL deserialize (runPutL (serialize (10^10::Natural))) :: Natural
-- 10000000000
instance Serial Natural where
  serialize :: forall (m :: * -> *). MonadPut m => Natural -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. n -> VarInt n
VarInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger
  deserialize :: forall (m :: * -> *). MonadGet m => m Natural
deserialize = forall a. Num a => Integer -> a
fromInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. VarInt n -> n
unVarInt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

-- |
-- >>> (runGetL deserialize $ runPutL $ serialize (1.82::Fixed E2))::Fixed E2
-- 1.82
instance HasResolution a => Serial (Fixed a) where
  serialize :: forall (m :: * -> *). MonadPut m => Fixed a -> m ()
serialize Fixed a
f =
      forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize Integer
i
    where
      i :: Integer
      i :: Integer
i = forall a b. (RealFrac a, Integral b) => a -> b
truncate forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
* Fixed a
r) forall a b. (a -> b) -> a -> b
$ Fixed a
f
      r :: Fixed a
r = forall a. Num a => Integer -> a
fromInteger forall a b. (a -> b) -> a -> b
$ forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
resolution Fixed a
f
  deserialize :: forall (m :: * -> *). MonadGet m => m (Fixed a)
deserialize =
    (((forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Fractional a => a -> a -> a
(/)) (forall a. Num a => Integer -> a
fromInteger forall a b. (a -> b) -> a -> b
$ forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
resolution (forall a. HasCallStack => a
undefined::Fixed a))) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

-- |
-- >>> (runGetL deserialize $ runPutL $ serialize (1.82::DiffTime))::DiffTime
-- 1.82s
instance Serial DiffTime where
  serialize :: forall (m :: * -> *). MonadPut m => DiffTime -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Fractional a => Rational -> a
fromRational forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Real a => a -> Rational
toRational::DiffTime -> Pico)
  deserialize :: forall (m :: * -> *). MonadGet m => m DiffTime
deserialize = (forall a. Fractional a => Rational -> a
fromRational forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Real a => a -> Rational
toRational::Pico -> DiffTime) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

-- |
-- >>> (runGetL deserialize $ runPutL $ serialize (1.82::DiffTime))::DiffTime
-- 1.82s
instance Serial NominalDiffTime where
  serialize :: forall (m :: * -> *). MonadPut m => NominalDiffTime -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Fractional a => Rational -> a
fromRational forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Real a => a -> Rational
toRational::NominalDiffTime -> Pico)
  deserialize :: forall (m :: * -> *). MonadGet m => m NominalDiffTime
deserialize = (forall a. Fractional a => Rational -> a
fromRational forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Real a => a -> Rational
toRational::Pico -> NominalDiffTime) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

-- |
-- >>> (runGetL deserialize $ runPutL $ serialize (ModifiedJulianDay 1))::Day
-- 1858-11-18
instance Serial Day where
  serialize :: forall (m :: * -> *). MonadPut m => Day -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. Day -> Integer
toModifiedJulianDay
  deserialize :: forall (m :: * -> *). MonadGet m => m Day
deserialize = Integer -> Day
ModifiedJulianDay forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

-- |
-- >>> (runGetL deserialize $ runPutL $ serialize (read "2014-01-01 10:54:42.478031 UTC"::UTCTime))::UTCTime
-- 2014-01-01 10:54:42.478031 UTC
instance Serial UTCTime where
  serialize :: forall (m :: * -> *). MonadPut m => UTCTime -> m ()
serialize (UTCTime Day
d DiffTime
t) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize (Day
d, DiffTime
t)
  deserialize :: forall (m :: * -> *). MonadGet m => m UTCTime
deserialize = forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\(Day
d, DiffTime
t) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Day -> DiffTime -> UTCTime
UTCTime Day
d DiffTime
t)

-- |
-- >>> (runGetL deserialize $ runPutL $ serialize (addAbsoluteTime 18.2 taiEpoch))::AbsoluteTime
-- 1858-11-17 00:00:18.2 TAI
instance Serial AbsoluteTime where
  serialize :: forall (m :: * -> *). MonadPut m => AbsoluteTime -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((forall a b c. (a -> b -> c) -> b -> a -> c
flip AbsoluteTime -> AbsoluteTime -> DiffTime
diffAbsoluteTime) AbsoluteTime
taiEpoch)
  deserialize :: forall (m :: * -> *). MonadGet m => m AbsoluteTime
deserialize = ((forall a b c. (a -> b -> c) -> b -> a -> c
flip DiffTime -> AbsoluteTime -> AbsoluteTime
addAbsoluteTime) AbsoluteTime
taiEpoch) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

-- |
-- >>> (runGetL deserialize $ runPutL $ serialize (5 % 11::Ratio Int))::Ratio Int
-- 5 % 11
instance (Serial a, Integral a) => Serial (Ratio a) where
  serialize :: forall (m :: * -> *). MonadPut m => Ratio a -> m ()
serialize Ratio a
r = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize (forall a. Ratio a -> a
numerator Ratio a
r, forall a. Ratio a -> a
denominator Ratio a
r)
  deserialize :: forall (m :: * -> *). MonadGet m => m (Ratio a)
deserialize = (\(a
n, a
d) -> a
n forall a. Integral a => a -> a -> Ratio a
% a
d) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

-- |
-- >>> getModJulianDate $ (runGetL deserialize $ runPutL $ serialize (ModJulianDate $ 5 % 11)::UniversalTime)
-- 5 % 11
instance Serial UniversalTime where
  serialize :: forall (m :: * -> *). MonadPut m => UniversalTime -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. UniversalTime -> Rational
getModJulianDate
  deserialize :: forall (m :: * -> *). MonadGet m => m UniversalTime
deserialize = Rational -> UniversalTime
ModJulianDate forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial TimeZone where
  serialize :: forall (m :: * -> *). MonadPut m => TimeZone -> m ()
serialize (TimeZone Int
m Bool
s String
n) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize (Int
m, Bool
s, String
n)
  deserialize :: forall (m :: * -> *). MonadGet m => m TimeZone
deserialize = (\(Int
m, Bool
s, String
n) -> Int -> Bool -> String -> TimeZone
TimeZone Int
m Bool
s String
n) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial TimeOfDay where
  serialize :: forall (m :: * -> *). MonadPut m => TimeOfDay -> m ()
serialize (TimeOfDay Int
h Int
m Pico
s) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize (Int
h, Int
m, Pico
s)
  deserialize :: forall (m :: * -> *). MonadGet m => m TimeOfDay
deserialize = (\(Int
h, Int
m, Pico
s) -> Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
h Int
m Pico
s) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial LocalTime where
  serialize :: forall (m :: * -> *). MonadPut m => LocalTime -> m ()
serialize (LocalTime Day
d TimeOfDay
t) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize (Day
d, TimeOfDay
t)
  deserialize :: forall (m :: * -> *). MonadGet m => m LocalTime
deserialize = (\(Day
d, TimeOfDay
t) -> Day -> TimeOfDay -> LocalTime
LocalTime Day
d TimeOfDay
t) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial ZonedTime where
  serialize :: forall (m :: * -> *). MonadPut m => ZonedTime -> m ()
serialize (ZonedTime LocalTime
l TimeZone
z) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize (LocalTime
l, TimeZone
z)
  deserialize :: forall (m :: * -> *). MonadGet m => m ZonedTime
deserialize = (\(LocalTime
l, TimeZone
z) -> LocalTime -> TimeZone -> ZonedTime
ZonedTime LocalTime
l TimeZone
z) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

-- |
-- >>> runGetL deserialize $ runPutL $ serialize LT::Ordering
-- LT
-- >>> runGetL deserialize $ runPutL $ serialize EQ::Ordering
-- EQ
-- >>> runGetL deserialize $ runPutL $ serialize GT::Ordering
-- GT
instance Serial Ordering where
  serialize :: forall (m :: * -> *). MonadPut m => Ordering -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a b. (Integral a, Num b) => a -> b
fromIntegral::Int -> Int8) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum
  deserialize :: forall (m :: * -> *). MonadGet m => m Ordering
deserialize = (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::Int8 -> Int)) forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial a => Serial (Down a) where
    serialize :: forall (m :: * -> *). MonadPut m => Down a -> m ()
serialize (Down a
a) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize a
a
    deserialize :: forall (m :: * -> *). MonadGet m => m (Down a)
deserialize = forall a. a -> Down a
Down forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial Version where
    serialize :: forall (m :: * -> *). MonadPut m => Version -> m ()
serialize (Version [Int]
vb [String]
ts) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall n. n -> VarInt n
VarInt [Int]
vb, [String]
ts)
    deserialize :: forall (m :: * -> *). MonadGet m => m Version
deserialize = do ([VarInt Int]
vb,[String]
ts) <- forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
                     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Int] -> [String] -> Version
Version (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall n. VarInt n -> n
unVarInt [VarInt Int]
vb) [String]
ts

instance Serial a => Serial (ZipList a) where
    serialize :: forall (m :: * -> *). MonadPut m => ZipList a -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ZipList a -> [a]
getZipList
    deserialize :: forall (m :: * -> *). MonadGet m => m (ZipList a)
deserialize = forall a. [a] -> ZipList a
ZipList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial a => Serial (Identity a) where
    serialize :: forall (m :: * -> *). MonadPut m => Identity a -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Identity a -> a
runIdentity
    deserialize :: forall (m :: * -> *). MonadGet m => m (Identity a)
deserialize = forall a. a -> Identity a
Identity forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial a => Serial (Constant a b) where
    serialize :: forall (m :: * -> *). MonadPut m => Constant a b -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} a (b :: k). Constant a b -> a
getConstant
    deserialize :: forall (m :: * -> *). MonadGet m => m (Constant a b)
deserialize = forall {k} a (b :: k). a -> Constant a b
Constant forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance (Serial (f a), Serial (g a)) => Serial (Functor.Product f g a) where
    serialize :: forall (m :: * -> *). MonadPut m => Product f g a -> m ()
serialize (Pair f a
f g a
g) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize (f a
f, g a
g)
    deserialize :: forall (m :: * -> *). MonadGet m => m (Product f g a)
deserialize = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall {k} (f :: k -> *) (g :: k -> *) (a :: k).
f a -> g a -> Product f g a
Pair forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial (f a) => Serial (Reverse f a) where
    serialize :: forall (m :: * -> *). MonadPut m => Reverse f a -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k). Reverse f a -> f a
getReverse
    deserialize :: forall (m :: * -> *). MonadGet m => m (Reverse f a)
deserialize = forall {k} (f :: k -> *) (a :: k). f a -> Reverse f a
Reverse forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

------------------------------------------------------------------------------
-- Serialization for newtypes from 'Data.Monoid'
------------------------------------------------------------------------------

instance Serial a => Serial (Dual a) where
    serialize :: forall (m :: * -> *). MonadPut m => Dual a -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Dual a -> a
getDual
    deserialize :: forall (m :: * -> *). MonadGet m => m (Dual a)
deserialize = forall a. a -> Dual a
Dual forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial All where
    serialize :: forall (m :: * -> *). MonadPut m => All -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. All -> Bool
getAll
    deserialize :: forall (m :: * -> *). MonadGet m => m All
deserialize = Bool -> All
All forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial Any where
    serialize :: forall (m :: * -> *). MonadPut m => Any -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. Any -> Bool
getAny
    deserialize :: forall (m :: * -> *). MonadGet m => m Any
deserialize = Bool -> Any
Any forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial a => Serial (Sum a) where
    serialize :: forall (m :: * -> *). MonadPut m => Sum a -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Sum a -> a
getSum
    deserialize :: forall (m :: * -> *). MonadGet m => m (Sum a)
deserialize = forall a. a -> Sum a
Sum forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial a => Serial (Monoid.Product a) where
    serialize :: forall (m :: * -> *). MonadPut m => Product a -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Product a -> a
getProduct
    deserialize :: forall (m :: * -> *). MonadGet m => m (Product a)
deserialize = forall a. a -> Product a
Product forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial a => Serial (First a) where
    serialize :: forall (m :: * -> *). MonadPut m => First a -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. First a -> Maybe a
getFirst
    deserialize :: forall (m :: * -> *). MonadGet m => m (First a)
deserialize = forall a. Maybe a -> First a
First forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial a => Serial (Last a) where
    serialize :: forall (m :: * -> *). MonadPut m => Last a -> m ()
serialize = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Last a -> Maybe a
getLast
    deserialize :: forall (m :: * -> *). MonadGet m => m (Last a)
deserialize = forall a. Maybe a -> Last a
Last forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize



------------------------------------------------------------------------------
-- Generic Serialization
------------------------------------------------------------------------------

-- $generics
--
-- You probably will never need to care that these exist except they
-- provide us with default definitions for 'Serial' and 'SerialEndian'

-- | Used internally to provide generic serialization
class GSerial f where
  gserialize :: MonadPut m => f a -> m ()
  gdeserialize :: MonadGet m => m (f a)

instance GSerial U1 where
  gserialize :: forall (m :: * -> *) a. MonadPut m => U1 a -> m ()
gserialize U1 a
U1 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
  gdeserialize :: forall (m :: * -> *) a. MonadGet m => m (U1 a)
gdeserialize = forall (m :: * -> *) a. Monad m => a -> m a
return forall k (p :: k). U1 p
U1

instance GSerial V1 where
  gserialize :: forall (m :: * -> *) a. MonadPut m => V1 a -> m ()
gserialize V1 a
x =
#if __GLASGOW_HASKELL__ >= 708
    case V1 a
x of {}
#else
    x `seq` error "I looked into the void."
#endif
  gdeserialize :: forall (m :: * -> *) a. MonadGet m => m (V1 a)
gdeserialize = forall (m :: * -> *) a. MonadFail m => String -> m a
MonadFail.fail String
"I looked into the void."

instance (GSerial f, GSerial g) => GSerial (f :*: g) where
  gserialize :: forall (m :: * -> *) a. MonadPut m => (:*:) f g a -> m ()
gserialize (f a
f :*: g a
g) = do
    forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadPut m) =>
f a -> m ()
gserialize f a
f
    forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadPut m) =>
f a -> m ()
gserialize g a
g
  gdeserialize :: forall (m :: * -> *) a. MonadGet m => m ((:*:) f g a)
gdeserialize = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadGet m) =>
m (f a)
gdeserialize forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadGet m) =>
m (f a)
gdeserialize

instance (GSerial f, GSerial g) => GSerial (f :+: g) where
  gserialize :: forall (m :: * -> *) a. MonadPut m => (:+:) f g a -> m ()
gserialize (L1 f a
x) = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
0 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadPut m) =>
f a -> m ()
gserialize f a
x
  gserialize (R1 g a
y) = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadPut m) =>
f a -> m ()
gserialize g a
y
  gdeserialize :: forall (m :: * -> *) a. MonadGet m => m ((:+:) f g a)
gdeserialize = forall (m :: * -> *). MonadGet m => m Word8
getWord8 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word8
a -> case Word8
a of
    Word8
0 -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadGet m) =>
m (f a)
gdeserialize
    Word8
1 -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadGet m) =>
m (f a)
gdeserialize
    Word8
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
MonadFail.fail String
"Missing case"

instance GSerial f => GSerial (M1 i c f) where
  gserialize :: forall (m :: * -> *) a. MonadPut m => M1 i c f a -> m ()
gserialize (M1 f a
x) = forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadPut m) =>
f a -> m ()
gserialize f a
x
  gdeserialize :: forall (m :: * -> *) a. MonadGet m => m (M1 i c f a)
gdeserialize = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadGet m) =>
m (f a)
gdeserialize

instance Serial a => GSerial (K1 i a) where
  gserialize :: forall (m :: * -> *) a. MonadPut m => K1 i a a -> m ()
gserialize (K1 a
x) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize a
x
  gdeserialize :: forall (m :: * -> *) a. MonadGet m => m (K1 i a a)
gdeserialize = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k i c (p :: k). c -> K1 i c p
K1 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize


-- | Used internally to provide generic big-endian serialization
class GSerialEndian f where
  gserializeBE :: MonadPut m => f a -> m ()
#ifndef HLINT
  default gserializeBE :: (MonadPut m, GSerial f) => f a -> m ()
  gserializeBE = forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadPut m) =>
f a -> m ()
gserialize
#endif

  gdeserializeBE :: MonadGet m => m (f a)
#ifndef HLINT
  default gdeserializeBE :: (MonadGet m, GSerial f) => m (f a)
  gdeserializeBE = forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadGet m) =>
m (f a)
gdeserialize
#endif

  gserializeLE :: MonadPut m => f a -> m ()
#ifndef HLINT
  default gserializeLE :: (MonadPut m, GSerial f) => f a -> m ()
  gserializeLE = forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadPut m) =>
f a -> m ()
gserialize
#endif

  gdeserializeLE :: MonadGet m => m (f a)
#ifndef HLINT
  default gdeserializeLE :: (MonadGet m, GSerial f) => m (f a)
  gdeserializeLE = forall (f :: * -> *) (m :: * -> *) a.
(GSerial f, MonadGet m) =>
m (f a)
gdeserialize
#endif

-- only difference between GSerialEndian and GSerial
instance SerialEndian a => GSerialEndian (K1 i a) where
  gserializeBE :: forall (m :: * -> *) a. MonadPut m => K1 i a a -> m ()
gserializeBE (K1 a
x) = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeBE a
x
  gdeserializeBE :: forall (m :: * -> *) a. MonadGet m => m (K1 i a a)
gdeserializeBE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k i c (p :: k). c -> K1 i c p
K1 forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeBE

  gserializeLE :: forall (m :: * -> *) a. MonadPut m => K1 i a a -> m ()
gserializeLE (K1 a
x) = forall a (m :: * -> *). (SerialEndian a, MonadPut m) => a -> m ()
serializeLE a
x
  gdeserializeLE :: forall (m :: * -> *) a. MonadGet m => m (K1 i a a)
gdeserializeLE = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k i c (p :: k). c -> K1 i c p
K1 forall a (m :: * -> *). (SerialEndian a, MonadGet m) => m a
deserializeLE

------------------------------------------------------------------------------
-- Higher-Rank Serialization
------------------------------------------------------------------------------

-- $higher
--
-- These classes provide us with the ability to serialize containers that need
-- polymorphic recursion.

class Serial1 f where
  serializeWith :: MonadPut m => (a -> m ()) -> f a -> m ()
#ifndef HLINT
  default serializeWith :: (MonadPut m, GSerial1 (Rep1 f), Generic1 f) => (a -> m ()) -> f a -> m ()
  serializeWith a -> m ()
f = forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
gserializeWith a -> m ()
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1
#endif

  deserializeWith :: MonadGet m => m a -> m (f a)
#ifndef HLINT
  default deserializeWith :: (MonadGet m, GSerial1 (Rep1 f), Generic1 f) => m a -> m (f a)
  deserializeWith m a
f = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k (f :: k -> *) (a :: k). Generic1 f => Rep1 f a -> f a
to1 (forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadGet m) =>
m a -> m (f a)
gdeserializeWith m a
f)
#endif

instance Serial1 [] where
  serializeWith :: forall (m :: * -> *) a. MonadPut m => (a -> m ()) -> [a] -> m ()
serializeWith a -> m ()
_ [] = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
0
  serializeWith a -> m ()
f (a
x:[a]
xs) = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m ()
f a
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> m ()
f [a]
xs
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m [a]
deserializeWith m a
m = forall (m :: * -> *). MonadGet m => m Word8
getWord8 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word8
a -> case Word8
a of
    Word8
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return []
    Word8
1 -> forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (:) m a
m (forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith m a
m)
    Word8
_ -> forall a. HasCallStack => String -> a
error String
"[].deserializeWith: Missing case"
instance Serial1 Maybe where
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> Maybe a -> m ()
serializeWith a -> m ()
_ Maybe a
Nothing = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
0
  serializeWith a -> m ()
f (Just a
a) = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m ()
f a
a
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (Maybe a)
deserializeWith m a
m = forall (m :: * -> *). MonadGet m => m Word8
getWord8 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word8
a -> case Word8
a of
    Word8
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
    Word8
1 -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a. a -> Maybe a
Just m a
m
    Word8
_ -> forall a. HasCallStack => String -> a
error String
"Maybe.deserializeWith: Missing case"
instance Serial a => Serial1 (Either a) where
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> Either a a -> m ()
serializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (Either a a)
deserializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance Serial a => Serial1 ((,) a) where
  serializeWith :: forall (m :: * -> *) a. MonadPut m => (a -> m ()) -> (a, a) -> m ()
serializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (a, a)
deserializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance (Serial a, Serial b) => Serial1 ((,,) a b) where
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> (a, b, a) -> m ()
serializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (a, b, a)
deserializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance (Serial a, Serial b, Serial c) => Serial1 ((,,,) a b c) where
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> (a, b, c, a) -> m ()
serializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (a, b, c, a)
deserializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
instance (Serial a, Serial b, Serial c, Serial d) => Serial1 ((,,,,) a b c d) where
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> (a, b, c, d, a) -> m ()
serializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (a, b, c, d, a)
deserializeWith = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Serial1 Seq.Seq where
  serializeWith :: forall (m :: * -> *) a. MonadPut m => (a -> m ()) -> Seq a -> m ()
serializeWith a -> m ()
pv = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> m ()
pv forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (Seq a)
deserializeWith m a
gv = forall a. [a] -> Seq a
Seq.fromList forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith m a
gv

instance Serial1 NEL.NonEmpty where
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> NonEmpty a -> m ()
serializeWith a -> m ()
pv = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> m ()
pv forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (NonEmpty a)
deserializeWith m a
gv = forall a. [a] -> NonEmpty a
NEL.fromList forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith m a
gv

{-
instance Serial1 Set.Set where
  serializeWith pv = serializeWith pv . Set.toAscList
  deserializeWith gv = Set.fromList `liftM` deserializeWith gv
-}

instance Serial1 IMap.IntMap where
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> IntMap a -> m ()
serializeWith a -> m ()
pv = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith (forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize a -> m ()
pv)
                   forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> [(Int, a)]
IMap.toAscList
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (IntMap a)
deserializeWith m a
gv = forall a. [(Int, a)] -> IntMap a
IMap.fromList
               forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith (forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize m a
gv)

instance (Ord k, Serial k) => Serial1 (Map.Map k) where
  -- serializeWith = serializeWith2 serialize
  -- deserializeWith = deserializeWith2 deserialize
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> Map k a -> m ()
serializeWith a -> m ()
pv = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith (forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize a -> m ()
pv)
                   forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
Map.toAscList
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (Map k a)
deserializeWith m a
gv = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
               forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith (forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize m a
gv)

instance (Hashable k, Eq k, Serial k) => Serial1 (HMap.HashMap k) where
  serializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> HashMap k a -> m ()
serializeWith a -> m ()
pv = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith (forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize a -> m ()
pv)
                   forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. HashMap k v -> [(k, v)]
HMap.toList
  deserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (HashMap k a)
deserializeWith m a
gv = forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HMap.fromList
               forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith (forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize m a
gv)

serialize1 :: (MonadPut m, Serial1 f, Serial a) => f a -> m ()
serialize1 :: forall (m :: * -> *) (f :: * -> *) a.
(MonadPut m, Serial1 f, Serial a) =>
f a -> m ()
serialize1 = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
{-# INLINE serialize1 #-}

deserialize1 :: (MonadGet m, Serial1 f, Serial a) => m (f a)
deserialize1 :: forall (m :: * -> *) (f :: * -> *) a.
(MonadGet m, Serial1 f, Serial a) =>
m (f a)
deserialize1 = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
{-# INLINE deserialize1 #-}

------------------------------------------------------------------------------
-- Higher-Rank Generic Serialization
------------------------------------------------------------------------------

-- | Used internally to provide generic serialization
class GSerial1 f where
  gserializeWith :: MonadPut m => (a -> m ()) -> f a -> m ()
  gdeserializeWith :: MonadGet m => m a -> m (f a)

instance GSerial1 Par1 where
  gserializeWith :: forall (m :: * -> *) a. MonadPut m => (a -> m ()) -> Par1 a -> m ()
gserializeWith a -> m ()
f (Par1 a
a) = a -> m ()
f a
a
  gdeserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (Par1 a)
gdeserializeWith m a
m = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall p. p -> Par1 p
Par1 m a
m

instance Serial1 f => GSerial1 (Rec1 f) where
  gserializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> Rec1 f a -> m ()
gserializeWith a -> m ()
f (Rec1 f a
fa) = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> m ()
f f a
fa
  gdeserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (Rec1 f a)
gdeserializeWith m a
m = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k (f :: k -> *) (p :: k). f p -> Rec1 f p
Rec1 (forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith m a
m)

-- instance (Serial1 f, GSerial1 g) => GSerial1 (f :.: g) where

instance GSerial1 U1 where
  gserializeWith :: forall (m :: * -> *) a. MonadPut m => (a -> m ()) -> U1 a -> m ()
gserializeWith a -> m ()
_ U1 a
U1 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
  gdeserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (U1 a)
gdeserializeWith m a
_  = forall (m :: * -> *) a. Monad m => a -> m a
return forall k (p :: k). U1 p
U1

instance GSerial1 V1 where
  gserializeWith :: forall (m :: * -> *) a. MonadPut m => (a -> m ()) -> V1 a -> m ()
gserializeWith a -> m ()
_ V1 a
x =
#if __GLASGOW_HASKELL__ >= 708
    case V1 a
x of {}
#else
    x `seq` error "I looked into the void."
#endif
  gdeserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (V1 a)
gdeserializeWith m a
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
MonadFail.fail String
"I looked into the void."

instance (GSerial1 f, GSerial1 g) => GSerial1 (f :*: g) where
  gserializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> (:*:) f g a -> m ()
gserializeWith a -> m ()
f (f a
a :*: g a
b) = forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
gserializeWith a -> m ()
f f a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
gserializeWith a -> m ()
f g a
b
  gdeserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m ((:*:) f g a)
gdeserializeWith m a
m = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadGet m) =>
m a -> m (f a)
gdeserializeWith m a
m) (forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadGet m) =>
m a -> m (f a)
gdeserializeWith m a
m)

instance (GSerial1 f, GSerial1 g) => GSerial1 (f :+: g) where
  gserializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> (:+:) f g a -> m ()
gserializeWith a -> m ()
f (L1 f a
x) = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
0 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
gserializeWith a -> m ()
f f a
x
  gserializeWith a -> m ()
f (R1 g a
y) = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
gserializeWith a -> m ()
f g a
y
  gdeserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m ((:+:) f g a)
gdeserializeWith m a
m = forall (m :: * -> *). MonadGet m => m Word8
getWord8 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word8
a -> case Word8
a of
    Word8
0 -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadGet m) =>
m a -> m (f a)
gdeserializeWith m a
m)
    Word8
1 -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadGet m) =>
m a -> m (f a)
gdeserializeWith m a
m)
    Word8
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
MonadFail.fail String
"Missing case"

instance (Serial1 f, GSerial1 g) => GSerial1 (f :.: g) where
  gserializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> (:.:) f g a -> m ()
gserializeWith a -> m ()
f (Comp1 f (g a)
m) = forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith (forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
gserializeWith a -> m ()
f) f (g a)
m
  gdeserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m ((:.:) f g a)
gdeserializeWith m a
m = forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (p :: k1).
f (g p) -> (:.:) f g p
Comp1 forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith (forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadGet m) =>
m a -> m (f a)
gdeserializeWith m a
m)

instance GSerial1 f => GSerial1 (M1 i c f) where
  gserializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> M1 i c f a -> m ()
gserializeWith a -> m ()
f (M1 f a
x) = forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
gserializeWith a -> m ()
f f a
x
  gdeserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (M1 i c f a)
gdeserializeWith = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) (m :: * -> *) a.
(GSerial1 f, MonadGet m) =>
m a -> m (f a)
gdeserializeWith

instance Serial a => GSerial1 (K1 i a) where
  gserializeWith :: forall (m :: * -> *) a.
MonadPut m =>
(a -> m ()) -> K1 i a a -> m ()
gserializeWith a -> m ()
_ (K1 a
x) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize a
x
  gdeserializeWith :: forall (m :: * -> *) a. MonadGet m => m a -> m (K1 i a a)
gdeserializeWith m a
_ = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k i c (p :: k). c -> K1 i c p
K1 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

------------------------------------------------------------------------------
-- Higher-Rank Serialization
------------------------------------------------------------------------------

class Serial2 f where
  serializeWith2 :: MonadPut m => (a -> m ()) -> (b -> m ()) -> f a b -> m ()
  deserializeWith2 :: MonadGet m => m a -> m b ->  m (f a b)

serialize2 :: (MonadPut m, Serial2 f, Serial a, Serial b) => f a b -> m ()
serialize2 :: forall (m :: * -> *) (f :: * -> * -> *) a b.
(MonadPut m, Serial2 f, Serial a, Serial b) =>
f a b -> m ()
serialize2 = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadPut m) =>
(a -> m ()) -> (b -> m ()) -> f a b -> m ()
serializeWith2 forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
{-# INLINE serialize2 #-}

deserialize2 :: (MonadGet m, Serial2 f, Serial a, Serial b) => m (f a b)
deserialize2 :: forall (m :: * -> *) (f :: * -> * -> *) a b.
(MonadGet m, Serial2 f, Serial a, Serial b) =>
m (f a b)
deserialize2 = forall (f :: * -> * -> *) (m :: * -> *) a b.
(Serial2 f, MonadGet m) =>
m a -> m b -> m (f a b)
deserializeWith2 forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize
{-# INLINE deserialize2 #-}

instance Serial2 Either where
  serializeWith2 :: forall (m :: * -> *) a b.
MonadPut m =>
(a -> m ()) -> (b -> m ()) -> Either a b -> m ()
serializeWith2 a -> m ()
f b -> m ()
_ (Left a
x)  = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
0 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m ()
f a
x
  serializeWith2 a -> m ()
_ b -> m ()
g (Right b
y) = forall (m :: * -> *). MonadPut m => Word8 -> m ()
putWord8 Word8
1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> m ()
g b
y
  deserializeWith2 :: forall (m :: * -> *) a b.
MonadGet m =>
m a -> m b -> m (Either a b)
deserializeWith2 m a
m m b
n = forall (m :: * -> *). MonadGet m => m Word8
getWord8 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word8
a -> case Word8
a of
    Word8
0 -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. a -> Either a b
Left m a
m
    Word8
1 -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. b -> Either a b
Right m b
n
    Word8
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
MonadFail.fail String
"Missing case"

instance Serial2 (,) where
  serializeWith2 :: forall (m :: * -> *) a b.
MonadPut m =>
(a -> m ()) -> (b -> m ()) -> (a, b) -> m ()
serializeWith2 a -> m ()
f b -> m ()
g (a
a, b
b) = a -> m ()
f a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> m ()
g b
b
  deserializeWith2 :: forall (m :: * -> *) a b. MonadGet m => m a -> m b -> m (a, b)
deserializeWith2 m a
m m b
n = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (,) m a
m m b
n

instance Serial a => Serial2 ((,,) a) where
  serializeWith2 :: forall (m :: * -> *) a b.
MonadPut m =>
(a -> m ()) -> (b -> m ()) -> (a, a, b) -> m ()
serializeWith2 a -> m ()
f b -> m ()
g (a
a, a
b, b
c) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m ()
f a
b forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> m ()
g b
c
  deserializeWith2 :: forall (m :: * -> *) a b. MonadGet m => m a -> m b -> m (a, a, b)
deserializeWith2 m a
m m b
n = forall (m :: * -> *) a1 a2 a3 r.
Monad m =>
(a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
liftM3 (,,) forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize m a
m m b
n

instance (Serial a, Serial b) => Serial2 ((,,,) a b) where
  serializeWith2 :: forall (m :: * -> *) a b.
MonadPut m =>
(a -> m ()) -> (b -> m ()) -> (a, b, a, b) -> m ()
serializeWith2 a -> m ()
f b -> m ()
g (a
a, b
b, a
c, b
d) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize b
b forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m ()
f a
c forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> m ()
g b
d
  deserializeWith2 :: forall (m :: * -> *) a b.
MonadGet m =>
m a -> m b -> m (a, b, a, b)
deserializeWith2 m a
m m b
n = forall (m :: * -> *) a1 a2 a3 a4 r.
Monad m =>
(a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
liftM4 (,,,) forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize m a
m m b
n

instance (Serial a, Serial b, Serial c) => Serial2 ((,,,,) a b c) where
  serializeWith2 :: forall (m :: * -> *) a b.
MonadPut m =>
(a -> m ()) -> (b -> m ()) -> (a, b, c, a, b) -> m ()
serializeWith2 a -> m ()
f b -> m ()
g (a
a, b
b, c
c, a
d, b
e) = forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize b
b forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize c
c forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m ()
f a
d forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> m ()
g b
e
  deserializeWith2 :: forall (m :: * -> *) a b.
MonadGet m =>
m a -> m b -> m (a, b, c, a, b)
deserializeWith2 m a
m m b
n = forall (m :: * -> *) a1 a2 a3 a4 a5 r.
Monad m =>
(a1 -> a2 -> a3 -> a4 -> a5 -> r)
-> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
liftM5 (,,,,) forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize m a
m m b
n