Packing and unpacking from lists packBytes' :: Monad m => [Word8]
-> ByteString m () packBytes' cs0 = packChunks 32 cs0 where
packChunks n cs = case B.packUptoLenBytes n cs of (bs, []) -> Chunk
bs (Empty ()) (bs, cs') -> Chunk bs (packChunks (min (n * 2)
BI.smallChunkSize) cs') -- packUptoLenBytes :: Int -> [Word8] ->
(ByteString, [Word8]) packUptoLenBytes len xs0 =
accursedUnutterablePerformIO (createUptoN' len $ p -> go p len xs0)
where go !_ !n [] = return (len-n, []) go !_ !0 xs = return (len, xs)
go !p !n (x:xs) = poke p x >> go (p
plusPtr 1) (n-1) xs
createUptoN' :: Int -> (Ptr Word8 -> IO (Int, a)) -> IO
(B.ByteString, a) createUptoN' l f = do fp <- B.mallocByteString l
(l', res)
withForeignPtr fp $ p - f p assert (l' <= l) $
return (B.PS fp 0 l', res) {-# INLINABLE packBytes' #-}
Convert a
Stream of pure
Word8 into a chunked
ByteStream.