on master help sign in
Add putBytes and putLines
668a263Harendra Kumar
src/Streamly/Internal/FileSystem/Handle.hs+29−1
@@ -64,7 +64,8 @@ module Streamly.Internal.FileSystem.Handle
64 , fromChunks
64 , fromChunks
65 , putChunks
65 , putChunks
66 , putStrings
66 , putStrings
67+ , putBytes
67− -- , putLines
68+ , putLines
68
69
69 -- -- * Random Access (Seek)
70 -- -- * Random Access (Seek)
70 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
71 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
@@ -107,6 +108,9 @@ module Streamly.Internal.FileSystem.Handle
107where
108where
108
109
109import Control.Monad.IO.Class (MonadIO(..))
110import Control.Monad.IO.Class (MonadIO(..))
111+#if __GLASGOW_HASKELL__ < 808
112+import Data.Semigroup (Semigroup(..))
113+#endif
110import Data.Word (Word8)
114import Data.Word (Word8)
111import Foreign.ForeignPtr (withForeignPtr)
115import Foreign.ForeignPtr (withForeignPtr)
112import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
116import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
@@ -346,6 +350,7 @@ writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
346-- Writing
350-- Writing
347-------------------------------------------------------------------------------
351-------------------------------------------------------------------------------
348
352
353+-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
349-- | Write a stream of arrays to a handle.
354-- | Write a stream of arrays to a handle.
350--
355--
351-- @since 0.7.0
356-- @since 0.7.0
@@ -363,6 +368,7 @@ putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
363putChunks = fromChunks stdout
368putChunks = fromChunks stdout
364
369
365-- | Write a stream of strings to standard output using Latin1 encoding.
370-- | Write a stream of strings to standard output using Latin1 encoding.
371+-- Output is flushed to the device for each string.
366--
372--
367-- /Internal/
373-- /Internal/
368--
374--
@@ -370,6 +376,28 @@ putChunks = fromChunks stdout
370putStrings :: MonadAsync m => SerialT m String -> m ()
376putStrings :: MonadAsync m => SerialT m String -> m ()
371putStrings = putChunks . S.mapM (IA.fromStream . U.encodeLatin1 . S.fromList)
377putStrings = putChunks . S.mapM (IA.fromStream . U.encodeLatin1 . S.fromList)
372
378
379+-- XXX use an unfold so that we can put lines from any object
380+-- | Write a stream of strings as separate lines to standard output using
381+-- Latin1 encoding. Output is line buffered i.e. the output is written to the
382+-- device as soon as a newline is encountered.
383+--
384+-- /Internal/
385+--
386+{-# INLINE putLines #-}
387+putLines :: MonadAsync m => SerialT m String -> m ()
388+putLines = putChunks . S.mapM
389+ (\xs -> IA.fromStream $ U.encodeLatin1 (S.fromList xs <> S.yield '\n'))
390+
391+-- | Write a stream of bytes from standard output.
392+--
393+-- > putBytes = fromBytes stdout
394+--
395+-- /Internal/
396+--
397+{-# INLINE putBytes #-}
398+putBytes :: MonadIO m => SerialT m Word8 -> m ()
399+putBytes = fromBytes stdout
400+
373-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
401-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
374-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
402-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
375-- The chunk size is only a maximum and the actual writes could be smaller as
403-- The chunk size is only a maximum and the actual writes could be smaller as