on master help sign in
Add putBytes and putLines
commit668a26318e6e16b516c304e09f4bc2fc63a44946 authorHarendra Kumar <harendra.kumar@gmail.com> dateMon 18 Nov 2019, 18:49 (7 years ago) parentf336d57
1 file changed+29−1
src/Streamly/Internal/FileSystem/Handle.hs+29−1
src/Streamly/Internal/FileSystem/Handle.hs+29−1
@@ -64,7 +64,8 @@ module Streamly.Internal.FileSystem.Handle
6464 , fromChunks
6565 , putChunks
6666 , putStrings
67− -- , putLines
67+ , putBytes
68+ , putLines
6869
6970 -- -- * Random Access (Seek)
7071 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
@@ -107,6 +108,9 @@ module Streamly.Internal.FileSystem.Handle
107108where
108109
109110import Control.Monad.IO.Class (MonadIO(..))
111+#if __GLASGOW_HASKELL__ < 808
112+import Data.Semigroup (Semigroup(..))
113+#endif
110114import Data.Word (Word8)
111115import Foreign.ForeignPtr (withForeignPtr)
112116import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
@@ -346,6 +350,7 @@ writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
346350-- Writing
347351-------------------------------------------------------------------------------
348352
353+-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
349354-- | Write a stream of arrays to a handle.
350355--
351356-- @since 0.7.0
@@ -363,6 +368,7 @@ putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
363368putChunks = fromChunks stdout
364369
365370-- | Write a stream of strings to standard output using Latin1 encoding.
371+-- Output is flushed to the device for each string.
366372--
367373-- /Internal/
368374--
@@ -370,6 +376,28 @@ putChunks = fromChunks stdout
370376putStrings :: MonadAsync m => SerialT m String -> m ()
371377putStrings = putChunks . S.mapM (IA.fromStream . U.encodeLatin1 . S.fromList)
372378
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+
373401-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
374402-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
375403-- The chunk size is only a maximum and the actual writes could be smaller as