module Data.Serialize.Put (
Put
, PutM(..)
, Putter
, runPut
, runPutM
, runPutLazy
, runPutMLazy
, putBuilder
, execPut
, flush
, putWord8
, putByteString
, putLazyByteString
, putWord16be
, putWord32be
, putWord64be
, putWord16le
, putWord32le
, putWord64le
, putWordhost
, putWord16host
, putWord32host
, putWord64host
, putTwoOf
, putListOf
, putIArrayOf
, putSeqOf
, putTreeOf
, putMapOf
, putIntMapOf
, putSetOf
, putIntSetOf
, putMaybeOf
, putEitherOf
) where
import Data.Serialize.Builder (Builder, toByteString, toLazyByteString)
import qualified Data.Serialize.Builder as B
import Control.Applicative
import Data.Array.Unboxed
import Data.Monoid
import Data.Word
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Data.IntMap as IntMap
import qualified Data.IntSet as IntSet
import qualified Data.Map as Map
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.Tree as T
data PairS a = PairS a !Builder
sndS :: PairS a -> Builder
sndS (PairS _ b) = b
newtype PutM a = Put { unPut :: PairS a }
type Put = PutM ()
type Putter a = a -> Put
instance Functor PutM where
fmap f m = Put $ let PairS a w = unPut m in PairS (f a) w
instance Applicative PutM where
pure = return
m <*> k = Put $
let PairS f w = unPut m
PairS x w' = unPut k
in PairS (f x) (w `mappend` w')
instance Monad PutM where
return a = Put (PairS a mempty)
m >>= k = Put $
let PairS a w = unPut m
PairS b w' = unPut (k a)
in PairS b (w `mappend` w')
m >> k = Put $
let PairS _ w = unPut m
PairS b w' = unPut k
in PairS b (w `mappend` w')
tell :: Putter Builder
tell b = Put $ PairS () b
putBuilder :: Putter Builder
putBuilder = tell
execPut :: PutM a -> Builder
execPut = sndS . unPut
runPut :: Put -> S.ByteString
runPut = toByteString . sndS . unPut
runPutM :: PutM a -> (a, S.ByteString)
runPutM (Put (PairS f s)) = (f, toByteString s)
runPutLazy :: Put -> L.ByteString
runPutLazy = toLazyByteString . sndS . unPut
runPutMLazy :: PutM a -> (a, L.ByteString)
runPutMLazy (Put (PairS f s)) = (f, toLazyByteString s)
flush :: Put
flush = tell B.flush
putWord8 :: Putter Word8
putWord8 = tell . B.singleton
putByteString :: Putter S.ByteString
putByteString = tell . B.fromByteString
putLazyByteString :: Putter L.ByteString
putLazyByteString = tell . B.fromLazyByteString
putWord16be :: Putter Word16
putWord16be = tell . B.putWord16be
putWord16le :: Putter Word16
putWord16le = tell . B.putWord16le
putWord32be :: Putter Word32
putWord32be = tell . B.putWord32be
putWord32le :: Putter Word32
putWord32le = tell . B.putWord32le
putWord64be :: Putter Word64
putWord64be = tell . B.putWord64be
putWord64le :: Putter Word64
putWord64le = tell . B.putWord64le
putWordhost :: Putter Word
putWordhost = tell . B.putWordhost
putWord16host :: Putter Word16
putWord16host = tell . B.putWord16host
putWord32host :: Putter Word32
putWord32host = tell . B.putWord32host
putWord64host :: Putter Word64
putWord64host = tell . B.putWord64host
putTwoOf :: Putter a -> Putter b -> Putter (a,b)
putTwoOf pa pb (a,b) = pa a >> pb b
putListOf :: Putter a -> Putter [a]
putListOf pa = go 0 (return ())
where
go n body [] = putWord64be n >> body
go n body (x:xs) = n' `seq` go n' (body >> pa x) xs
where n' = n + 1
putIArrayOf :: (Ix i, IArray a e) => Putter i -> Putter e -> Putter (a i e)
putIArrayOf pix pe a = do
putTwoOf pix pix (bounds a)
putListOf pe (elems a)
putSeqOf :: Putter a -> Putter (Seq.Seq a)
putSeqOf pa = go 0 (return ())
where
go n body s = case Seq.viewl s of
Seq.EmptyL -> putWord64be n >> body
a Seq.:< as -> n' `seq` go n' (body >> pa a) as
where n' = n + 1
putTreeOf :: Putter a -> Putter (T.Tree a)
putTreeOf pa (T.Node r s) = pa r >> putListOf (putTreeOf pa) s
putMapOf :: Ord k => Putter k -> Putter a -> Putter (Map.Map k a)
putMapOf pk pa = putListOf (putTwoOf pk pa) . Map.toAscList
putIntMapOf :: Putter Int -> Putter a -> Putter (IntMap.IntMap a)
putIntMapOf pix pa = putListOf (putTwoOf pix pa) . IntMap.toAscList
putSetOf :: Putter a -> Putter (Set.Set a)
putSetOf pa = putListOf pa . Set.toAscList
putIntSetOf :: Putter Int -> Putter IntSet.IntSet
putIntSetOf pix = putListOf pix . IntSet.toAscList
putMaybeOf :: Putter a -> Putter (Maybe a)
putMaybeOf _ Nothing = putWord8 0
putMaybeOf pa (Just a) = putWord8 1 >> pa a
putEitherOf :: Putter a -> Putter b -> Putter (Either a b)
putEitherOf pa _ (Left a) = putWord8 0 >> pa a
putEitherOf _ pb (Right b) = putWord8 1 >> pb b