module Throughput.BinaryBuilderDeclarative (
serialize
) where
import Data.Monoid
import Data.Word
import qualified Data.ByteString.Lazy as L
import Data.Binary.Builder
import Control.Monad
import Throughput.Utils
serialize :: Int -> Int -> Endian -> Int -> Maybe L.ByteString
serialize wordSize chunkSize end iters = fmap toLazyByteString $
case (wordSize, chunkSize, end) of
(1, 1,_) -> return $ writeByteN1 iters
(2, 1, Big) -> return $ writeWord16N1Big iters
(2, 1, Little) -> return $ writeWord16N1Little iters
(2, 1, Host) -> return $ writeWord16N1Host iters
(4, 1, Big) -> return $ writeWord32N1Big iters
(4, 1, Little) -> return $ writeWord32N1Little iters
(4, 1, Host) -> return $ writeWord32N1Host iters
(8, 1, Host) -> return $ writeWord64N1Host iters
(8, 1, Big) -> return $ writeWord64N1Big iters
(8, 1, Little) -> return $ writeWord64N1Little iters
_ -> mzero
word8List :: Int -> [Word8]
word8List n = take n $ cycle $ [0..]
writeByteN1 = mconcat . map singleton . word8List
word16List :: Int -> [Word16]
word16List n = take n $ cycle $ [0..]
writeWord16N1Big = mconcat . map putWord16be . word16List
writeWord16N1Little = mconcat . map putWord16le . word16List
writeWord16N1Host = mconcat . map putWord16host . word16List
word32List :: Int -> [Word32]
word32List n = [0..fromIntegral (n-1)]
writeWord32N1Big = mconcat . map putWord32be . word32List
writeWord32N1Little = mconcat . map putWord32le . word32List
writeWord32N1Host = mconcat . map putWord32host . word32List
word64List :: Int -> [Word64]
word64List n = [0..fromIntegral (n-1)]
writeWord64N1Big = mconcat . map putWord64be . word64List
writeWord64N1Little = mconcat . map putWord64le . word64List
writeWord64N1Host = mconcat . map putWord64host . word64List