Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data ByteString m r
- = Empty r
- | Chunk !ByteString (ByteString m r)
- | Go (m (ByteString m r))
- consChunk :: ByteString -> ByteString m r -> ByteString m r
- chunkOverhead :: Int
- defaultChunkSize :: Int
- materialize :: (forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x) -> ByteString m r
- dematerialize :: Monad m => ByteString m r -> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x
- foldrChunks :: Monad m => (ByteString -> a -> a) -> a -> ByteString m r -> m a
- foldlChunks :: Monad m => (a -> ByteString -> a) -> a -> ByteString m r -> m (Of a r)
- foldrChunksM :: Monad m => (ByteString -> m a -> m a) -> m a -> ByteString m r -> m a
- foldlChunksM :: Monad m => (a -> ByteString -> m a) -> m a -> ByteString m r -> m (Of a r)
- chunkFold :: Monad m => (x -> ByteString -> x) -> x -> (x -> a) -> ByteString m r -> m (Of a r)
- chunkFoldM :: Monad m => (x -> ByteString -> m x) -> m x -> (x -> m a) -> ByteString m r -> m (Of a r)
- chunkMap :: Monad m => (ByteString -> ByteString) -> ByteString m r -> ByteString m r
- chunkMapM :: Monad m => (ByteString -> m ByteString) -> ByteString m r -> ByteString m r
- chunkMapM_ :: Monad m => (ByteString -> m x) -> ByteString m r -> m r
- unfoldMChunks :: Monad m => (s -> m (Maybe (ByteString, s))) -> s -> ByteString m ()
- unfoldrChunks :: Monad m => (s -> m (Either r (ByteString, s))) -> s -> ByteString m r
- packChars :: Monad m => Stream (Of Char) m r -> ByteString m r
- smallChunkSize :: Int
- unpackBytes :: Monad m => ByteString m r -> Stream (Of Word8) m r
- packBytes :: Monad m => Stream (Of Word8) m r -> ByteString m r
- chunk :: ByteString -> ByteString m ()
- mwrap :: m (ByteString m r) -> ByteString m r
- unfoldrNE :: Int -> (a -> Either r (Word8, a)) -> a -> (ByteString, Either r a)
- reread :: Monad m => (s -> m (Maybe ByteString)) -> s -> ByteString m ()
- inlinePerformIO :: IO a -> a
- unsafeLast :: ByteString -> Word8
- unsafeInit :: ByteString -> ByteString
- copy :: Monad m => ByteString m r -> ByteString (ByteString m) r
- bracketByteString :: MonadResource m => IO a -> (a -> IO ()) -> (a -> ByteString m b) -> ByteString m b
Documentation
data ByteString m r Source #
A space-efficient representation of a succession of Word8
vectors, supporting many
efficient operations.
An effectful ByteString
contains 8-bit bytes, or by using the operations
from Data.ByteString.Streaming.Char8 it can be interpreted as containing
8-bit characters.
Empty r | |
Chunk !ByteString (ByteString m r) | |
Go (m (ByteString m r)) |
MonadTrans ByteString Source # | |
MonadBase b m => MonadBase b (ByteString m) Source # | |
Monad m => Monad (ByteString m) Source # | |
Monad m => Functor (ByteString m) Source # | |
Monad m => Applicative (ByteString m) Source # | |
MonadIO m => MonadIO (ByteString m) Source # | |
MonadThrow m => MonadThrow (ByteString m) Source # | |
MonadCatch m => MonadCatch (ByteString m) Source # | |
MonadResource m => MonadResource (ByteString m) Source # | |
MFunctor * ByteString Source # | |
((~) (* -> *) m Identity, Show r) => Show (ByteString m r) Source # | |
(~) * r () => IsString (ByteString m r) Source # | |
(Semigroup r, Monad m) => Semigroup (ByteString m r) Source # | |
(Monoid r, Monad m) => Monoid (ByteString m r) Source # | |
consChunk :: ByteString -> ByteString m r -> ByteString m r Source #
Smart constructor for Chunk
.
chunkOverhead :: Int Source #
The memory management overhead. Currently this is tuned for GHC only.
defaultChunkSize :: Int Source #
The chunk size used for I/O. Currently set to 32k, less the memory management overhead
materialize :: (forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x) -> ByteString m r Source #
Construct a succession of chunks from its Church encoding (compare GHC.Exts.build
)
dematerialize :: Monad m => ByteString m r -> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x Source #
Resolve a succession of chunks into its Church encoding; this is not a safe operation; it is equivalent to exposing the constructors
foldrChunks :: Monad m => (ByteString -> a -> a) -> a -> ByteString m r -> m a Source #
Consume the chunks of an effectful ByteString with a natural right fold.
foldlChunks :: Monad m => (a -> ByteString -> a) -> a -> ByteString m r -> m (Of a r) Source #
foldrChunksM :: Monad m => (ByteString -> m a -> m a) -> m a -> ByteString m r -> m a Source #
Consume the chunks of an effectful ByteString with a natural right monadic fold.
foldlChunksM :: Monad m => (a -> ByteString -> m a) -> m a -> ByteString m r -> m (Of a r) Source #
chunkFold :: Monad m => (x -> ByteString -> x) -> x -> (x -> a) -> ByteString m r -> m (Of a r) Source #
chunkFold
is preferable to foldlChunks
since it is
an appropriate argument for Control.Foldl.purely
which
permits many folds and sinks to be run simulaneously on one bytestream.
chunkFoldM :: Monad m => (x -> ByteString -> m x) -> m x -> (x -> m a) -> ByteString m r -> m (Of a r) Source #
chunkFoldM
is preferable to foldlChunksM
since it is
an appropriate argument for Control.Foldl.impurely
which
permits many folds and sinks to be run simulaneously on one bytestream.
chunkMap :: Monad m => (ByteString -> ByteString) -> ByteString m r -> ByteString m r Source #
chunkMapM :: Monad m => (ByteString -> m ByteString) -> ByteString m r -> ByteString m r Source #
chunkMapM_ :: Monad m => (ByteString -> m x) -> ByteString m r -> m r Source #
unfoldMChunks :: Monad m => (s -> m (Maybe (ByteString, s))) -> s -> ByteString m () Source #
unfoldrChunks :: Monad m => (s -> m (Either r (ByteString, s))) -> s -> ByteString m r Source #
smallChunkSize :: Int Source #
The recommended chunk size. Currently set to 4k, less the memory management overhead
unpackBytes :: Monad m => ByteString m r -> Stream (Of Word8) m r Source #
packBytes :: Monad m => Stream (Of Word8) m r -> ByteString m r Source #
Packing and unpacking from lists
packBytes' :: Monad m => [Word8] -> ByteString m ()
packBytes' cs0 =
packChunks 32 cs0
where
packChunks n cs = case S.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 =
unsafeDupablePerformIO (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 (S.ByteString, a)
createUptoN' l f = do
fp <- S.mallocByteString l
(l', res) withForeignPtr fp $ p - f p
assert (l' <= l) $ return (S.PS fp 0 l', res)
{--}
chunk :: ByteString -> ByteString m () Source #
Yield-style smart constructor for Chunk
.
mwrap :: m (ByteString m r) -> ByteString m r Source #
Reconceive an effect that results in an effectful bytestring as an effectful bytestring. Compare Streaming.mwrap. The closes equivalent of
>>>
Streaming.wrap :: f (Stream f m r) -> Stream f m r
is here consChunk
. mwrap
is the smart constructor for the internal Go
constructor.
reread :: Monad m => (s -> m (Maybe ByteString)) -> s -> ByteString m () Source #
Stream chunks from something that contains IO (Maybe ByteString)
until it returns Nothing
. reread
is of particular use rendering io-streams
input streams as byte streams in the present sense
Q.reread Streams.read :: InputStream S.ByteString -> Q.ByteString IO () Q.reread (liftIO . Streams.read) :: MonadIO m => InputStream S.ByteString -> Q.ByteString m ()
The other direction here is
Streams.unfoldM Q.unconsChunk :: Q.ByteString IO r -> IO (InputStream S.ByteString)
inlinePerformIO :: IO a -> a Source #
unsafeLast :: ByteString -> Word8 Source #
unsafeInit :: ByteString -> ByteString Source #
copy :: Monad m => ByteString m r -> ByteString (ByteString m) r Source #
Make the information in a bytestring available to more than one eliminating fold, e.g.
>>>
Q.count 'l' $ Q.count 'o' $ Q.copy $ "hello\nworld"
3 :> (2 :> ())
>>>
Q.length $ Q.count 'l' $ Q.count 'o' $ Q.copy $ Q.copy "hello\nworld"
11 :> (3 :> (2 :> ()))
>>>
runResourceT $ Q.writeFile "hello2.txt" $ Q.writeFile "hello1.txt" $ Q.copy $ "hello\nworld\n"
>>>
:! cat hello2.txt
hello world>>>
:! cat hello1.txt
hello world
This sort of manipulation could as well be acheived by combining folds - using
Control.Foldl
for example. But any sort of manipulation can be involved in
the fold. Here are a couple of trivial complications involving splitting by lines:
>>>
let doubleLines = Q.unlines . maps (<* Q.chunk "\n" ) . Q.lines
>>>
let emphasize = Q.unlines . maps (<* Q.chunk "!" ) . Q.lines
>>>
runResourceT $ Q.writeFile "hello2.txt" $ emphasize $ Q.writeFile "hello1.txt" $ doubleLines $ Q.copy $ "hello\nworld"
>>>
:! cat hello2.txt
hello! world!>>>
:! cat hello1.txt
hello
world
As with the parallel operations in Streaming.Prelude
, we have
Q.effects . Q.copy = id hoist Q.effects . Q.copy = id
The duplication does not by itself involve the copying of bytestring chunks; it just makes two references to each chunk as it arises. This does, however double the number of constructors associated with each chunk.
ResourceT help
bracketByteString :: MonadResource m => IO a -> (a -> IO ()) -> (a -> ByteString m b) -> ByteString m b Source #