on master help sign in
Change Handle, Stdio, File unfold APIs (#1998)
commite03cceb344a1e70ce6397e44225da0333070ed8b authorHarendra Kumar <harendra@composewell.com> dateThu 20 Oct 2022, 17:27 (4 years ago) parent2ea9fb0
16 files changed+305−259
benchmark/Streamly/Benchmark/Data/Array/Stream/Unboxed.hs+8−8 benchmark/Streamly/Benchmark/Data/Parser.hs+1−1 benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs+14−14 benchmark/Streamly/Benchmark/Data/Unfold.hs+7−7 benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+14−14 benchmark/Streamly/Benchmark/FileSystem/Handle/ReadWrite.hs+8−8 benchmark/Streamly/Benchmark/Unicode/Stream.hs+13−14 core/src/Streamly/Console/Stdio.hs+29−4 core/src/Streamly/FileSystem/Handle.hs+38−7 core/src/Streamly/Internal/Console/Stdio.hs+33−29 core/src/Streamly/Internal/FileSystem/Handle.hs+82−96 hie.yaml+3−1 src/Streamly/Internal/FileSystem/Event/Darwin.hs+1−1 src/Streamly/Internal/FileSystem/Event/Linux.hs+1−1 src/Streamly/Internal/FileSystem/File.hs+45−46 test/Streamly/Test/FileSystem/Handle.hs+8−8
benchmark/Streamly/Benchmark/Data/Array/Stream/Unboxed.hs+8−8
@@ -83,7 +83,7 @@ benchIO name src sink =
8383-- | Get the last byte from a file bytestream.
8484toChunksLast :: Handle -> IO (Maybe Word8)
8585toChunksLast inh = do
86− let s = Handle.getChunks inh
86+ let s = Handle.readChunks inh
8787 larr <- Stream.last s
8888 return $ case larr of
8989 Nothing -> Nothing
@@ -97,7 +97,7 @@ inspect $ 'toChunksLast `hasNoType` ''Step
9797-- | Count the number of bytes in a file.
9898toChunksSumLengths :: Handle -> IO Int
9999toChunksSumLengths inh =
100− let s = Handle.getChunks inh
100+ let s = Handle.readChunks inh
101101 in Stream.sum (Stream.map Array.length s)
102102
103103#ifdef INSPECTION
@@ -109,7 +109,7 @@ inspect $ 'toChunksSumLengths `hasNoType` ''Step
109109toChunksCountBytes :: Handle -> IO Word8
110110toChunksCountBytes inh = do
111111 let foldlArr' f z = runIdentity . Stream.foldl' f z . Array.toStream
112− let s = Handle.getChunks inh
112+ let s = Handle.readChunks inh
113113 Stream.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
114114
115115#ifdef INSPECTION
@@ -119,7 +119,7 @@ inspect $ 'toChunksCountBytes `hasNoType` ''Step
119119
120120toChunksDecodeUtf8Arrays :: Handle -> IO ()
121121toChunksDecodeUtf8Arrays =
122− Stream.drain . Unicode.decodeUtf8Arrays . Handle.getChunks
122+ Stream.drain . Unicode.decodeUtf8Arrays . Handle.readChunks
123123
124124#ifdef INSPECTION
125125inspect $ hasNoTypeClasses 'toChunksDecodeUtf8Arrays
@@ -133,7 +133,7 @@ inspect $ hasNoTypeClasses 'toChunksDecodeUtf8Arrays
133133-- | Count the number of lines in a file.
134134toChunksSplitOnSuffix :: Handle -> IO Int
135135toChunksSplitOnSuffix =
136− Stream.length . ArrayStream.splitOnSuffix 10 . Handle.getChunks
136+ Stream.length . ArrayStream.splitOnSuffix 10 . Handle.readChunks
137137
138138#ifdef INSPECTION
139139inspect $ hasNoTypeClasses 'toChunksSplitOnSuffix
@@ -143,7 +143,7 @@ inspect $ 'toChunksSplitOnSuffix `hasNoType` ''Step
143143-- XXX use a word splitting combinator instead of splitOn and test it.
144144-- | Count the number of words in a file.
145145toChunksSplitOn :: Handle -> IO Int
146−toChunksSplitOn = Stream.length . ArrayStream.splitOn 32 . Handle.getChunks
146+toChunksSplitOn = Stream.length . ArrayStream.splitOn 32 . Handle.readChunks
147147
148148#ifdef INSPECTION
149149inspect $ hasNoTypeClasses 'toChunksSplitOn
@@ -183,7 +183,7 @@ copyChunksSplitInterposeSuffix inh outh =
183183 Stream.fold (Handle.write outh)
184184 $ ArrayStream.interposeSuffix 10
185185 $ ArrayStream.splitOnSuffix 10
186− $ Handle.getChunks inh
186+ $ Handle.readChunks inh
187187
188188#ifdef INSPECTION
189189inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterposeSuffix [''Unboxed]
@@ -198,7 +198,7 @@ copyChunksSplitInterpose inh outh =
198198 $ ArrayStream.interpose 32
199199 -- XXX this is not correct word splitting combinator
200200 $ ArrayStream.splitOn 32
201− $ Handle.getChunks inh
201+ $ Handle.readChunks inh
202202
203203#ifdef INSPECTION
204204inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterpose [''Unboxed]
benchmark/Streamly/Benchmark/Data/Parser.hs+1−1
@@ -330,7 +330,7 @@ parseManyChunksOfSum n inh =
330330 Stream.fold Fold.length
331331 $ Stream.parseMany
332332 (PR.fromFold $ Fold.take n Fold.sum)
333− (Stream.unfold Handle.read inh)
333+ (Stream.unfold Handle.reader inh)
334334
335335-------------------------------------------------------------------------------
336336-- Parsing with unfolds
benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs+14−14
@@ -149,7 +149,7 @@ o_1_space_serial_exceptions length =
149149-- | Send the file contents to /dev/null with exception handling
150150readWriteOnExceptionStream :: Handle -> Handle -> IO ()
151151readWriteOnExceptionStream inh devNull =
152− let readEx = Stream.onException (hClose inh) (Stream.unfold FH.read inh)
152+ let readEx = Stream.onException (hClose inh) (Stream.unfold FH.reader inh)
153153 in Stream.fold (FH.write devNull) readEx
154154
155155#ifdef INSPECTION
@@ -160,7 +160,7 @@ inspect $ hasNoTypeClasses 'readWriteOnExceptionStream
160160readWriteHandleExceptionStream :: Handle -> Handle -> IO ()
161161readWriteHandleExceptionStream inh devNull =
162162 let handler (_e :: SomeException) = Stream.fromEffect (hClose inh >> return 10)
163− readEx = Stream.handle handler (Stream.unfold FH.read inh)
163+ readEx = Stream.handle handler (Stream.unfold FH.reader inh)
164164 in Stream.fold (FH.write devNull) readEx
165165
166166#ifdef INSPECTION
@@ -170,7 +170,7 @@ inspect $ hasNoTypeClasses 'readWriteHandleExceptionStream
170170-- | Send the file contents to /dev/null with exception handling
171171readWriteFinally_Stream :: Handle -> Handle -> IO ()
172172readWriteFinally_Stream inh devNull =
173− let readEx = Stream.finally_ (hClose inh) (Stream.unfold FH.read inh)
173+ let readEx = Stream.finally_ (hClose inh) (Stream.unfold FH.reader inh)
174174 in Stream.fold (FH.write devNull) readEx
175175
176176#ifdef INSPECTION
@@ -179,14 +179,14 @@ inspect $ hasNoTypeClasses 'readWriteFinally_Stream
179179
180180readWriteFinallyStream :: Handle -> Handle -> IO ()
181181readWriteFinallyStream inh devNull =
182− let readEx = Stream.finally (hClose inh) (Stream.unfold FH.read inh)
182+ let readEx = Stream.finally (hClose inh) (Stream.unfold FH.reader inh)
183183 in Stream.fold (FH.write devNull) readEx
184184
185185-- | Send the file contents to /dev/null with exception handling
186186fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
187187fromToBytesBracket_Stream inh devNull =
188188 let readEx = Stream.bracket_ (return ()) (\_ -> hClose inh)
189− (\_ -> IFH.getBytes inh)
189+ (\_ -> IFH.read inh)
190190 in IFH.putBytes devNull readEx
191191
192192#ifdef INSPECTION
@@ -196,14 +196,14 @@ inspect $ hasNoTypeClasses 'fromToBytesBracket_Stream
196196fromToBytesBracketStream :: Handle -> Handle -> IO ()
197197fromToBytesBracketStream inh devNull =
198198 let readEx = Stream.bracket (return ()) (\_ -> hClose inh)
199− (\_ -> IFH.getBytes inh)
199+ (\_ -> IFH.read inh)
200200 in IFH.putBytes devNull readEx
201201
202202readWriteBeforeAfterStream :: Handle -> Handle -> IO ()
203203readWriteBeforeAfterStream inh devNull =
204204 let readEx =
205205 Stream.after (hClose inh)
206− $ Stream.before (hPutChar devNull 'A') (Stream.unfold FH.read inh)
206+ $ Stream.before (hPutChar devNull 'A') (Stream.unfold FH.reader inh)
207207 in Stream.fold (FH.write devNull) readEx
208208
209209#ifdef INSPECTION
@@ -212,7 +212,7 @@ inspect $ 'readWriteBeforeAfterStream `hasNoType` ''D.Step
212212
213213readWriteAfterStream :: Handle -> Handle -> IO ()
214214readWriteAfterStream inh devNull =
215− let readEx = Stream.after (hClose inh) (Stream.unfold FH.read inh)
215+ let readEx = Stream.after (hClose inh) (Stream.unfold FH.reader inh)
216216 in Stream.fold (FH.write devNull) readEx
217217
218218#ifdef INSPECTION
@@ -221,7 +221,7 @@ inspect $ 'readWriteAfterStream `hasNoType` ''D.Step
221221
222222readWriteAfter_Stream :: Handle -> Handle -> IO ()
223223readWriteAfter_Stream inh devNull =
224− let readEx = Stream.after_ (hClose inh) (Stream.unfold FH.read inh)
224+ let readEx = Stream.after_ (hClose inh) (Stream.unfold FH.reader inh)
225225 in Stream.fold (FH.write devNull) readEx
226226
227227#ifdef INSPECTION
@@ -262,7 +262,7 @@ o_1_space_copy_stream_exceptions env =
262262-- | Send the file contents to /dev/null with exception handling
263263readChunksOnException :: Handle -> Handle -> IO ()
264264readChunksOnException inh devNull =
265− let readEx = IUF.onException (\_ -> hClose inh) FH.readChunks
265+ let readEx = IUF.onException (\_ -> hClose inh) FH.chunkReader
266266 in IUF.fold (IFH.writeChunks devNull) readEx inh
267267
268268#ifdef INSPECTION
@@ -272,7 +272,7 @@ inspect $ hasNoTypeClasses 'readChunksOnException
272272-- | Send the file contents to /dev/null with exception handling
273273readChunksBracket_ :: Handle -> Handle -> IO ()
274274readChunksBracket_ inh devNull =
275− let readEx = IUF.bracket_ return (\_ -> hClose inh) FH.readChunks
275+ let readEx = IUF.bracket_ return (\_ -> hClose inh) FH.chunkReader
276276 in IUF.fold (IFH.writeChunks devNull) readEx inh
277277
278278#ifdef INSPECTION
@@ -281,7 +281,7 @@ inspect $ hasNoTypeClasses 'readChunksBracket_
281281
282282readChunksBracket :: Handle -> Handle -> IO ()
283283readChunksBracket inh devNull =
284− let readEx = IUF.bracket return (\_ -> hClose inh) FH.readChunks
284+ let readEx = IUF.bracket return (\_ -> hClose inh) FH.chunkReader
285285 in IUF.fold (IFH.writeChunks devNull) readEx inh
286286
287287o_1_space_copy_exceptions_readChunks :: BenchEnv -> [Benchmark]
@@ -306,7 +306,7 @@ toChunksBracket_ inh devNull =
306306 let readEx = Stream.bracket_
307307 (return ())
308308 (\_ -> hClose inh)
309− (\_ -> IFH.getChunks inh)
309+ (\_ -> IFH.readChunks inh)
310310 in Stream.fold (IFH.writeChunks devNull) readEx
311311
312312#ifdef INSPECTION
@@ -318,7 +318,7 @@ toChunksBracket inh devNull =
318318 let readEx = Stream.bracket
319319 (return ())
320320 (\_ -> hClose inh)
321− (\_ -> IFH.getChunks inh)
321+ (\_ -> IFH.readChunks inh)
322322 in Stream.fold (IFH.writeChunks devNull) readEx
323323
324324o_1_space_copy_exceptions_toChunks :: BenchEnv -> [Benchmark]
benchmark/Streamly/Benchmark/Data/Unfold.hs+7−7
@@ -707,7 +707,7 @@ lf = fromIntegral (ord '\n')
707707-- | Split on line feed.
708708foldManySepBy :: Handle -> IO Int
709709foldManySepBy =
710− let u = UF.foldMany (FL.takeEndBy_ (== lf) FL.drain) FH.read
710+ let u = UF.foldMany (FL.takeEndBy_ (== lf) FL.drain) FH.reader
711711 in UF.fold FL.length u
712712
713713o_1_space_nested :: BenchEnv -> Int -> [Benchmark]
@@ -748,7 +748,7 @@ o_n_space_nested size =
748748-- | Send the file contents to /dev/null with exception handling
749749readWriteOnExceptionUnfold :: Handle -> Handle -> IO ()
750750readWriteOnExceptionUnfold inh devNull =
751− let readEx = UF.onException (\_ -> hClose inh) FH.read
751+ let readEx = UF.onException (\_ -> hClose inh) FH.reader
752752 in S.fold (FH.write devNull) $ S.unfold readEx inh
753753
754754#ifdef INSPECTION
@@ -760,7 +760,7 @@ inspect $ hasNoTypeClasses 'readWriteOnExceptionUnfold
760760readWriteHandleExceptionUnfold :: Handle -> Handle -> IO ()
761761readWriteHandleExceptionUnfold inh devNull =
762762 let handler (_e :: SomeException) = hClose inh >> return 10
763− readEx = UF.handle (UF.functionM handler) FH.read
763+ readEx = UF.handle (UF.functionM handler) FH.reader
764764 in S.fold (FH.write devNull) $ S.unfold readEx inh
765765
766766#ifdef INSPECTION
@@ -771,7 +771,7 @@ inspect $ hasNoTypeClasses 'readWriteHandleExceptionUnfold
771771-- | Send the file contents to /dev/null with exception handling
772772readWriteFinally_Unfold :: Handle -> Handle -> IO ()
773773readWriteFinally_Unfold inh devNull =
774− let readEx = UF.finally_ (\_ -> hClose inh) FH.read
774+ let readEx = UF.finally_ (\_ -> hClose inh) FH.reader
775775 in S.fold (FH.write devNull) $ S.unfold readEx inh
776776
777777#ifdef INSPECTION
@@ -781,13 +781,13 @@ inspect $ hasNoTypeClasses 'readWriteFinally_Unfold
781781
782782readWriteFinallyUnfold :: Handle -> Handle -> IO ()
783783readWriteFinallyUnfold inh devNull =
784− let readEx = UF.finally (\_ -> hClose inh) FH.read
784+ let readEx = UF.finally (\_ -> hClose inh) FH.reader
785785 in S.fold (FH.write devNull) $ S.unfold readEx inh
786786
787787-- | Send the file contents to /dev/null with exception handling
788788readWriteBracket_Unfold :: Handle -> Handle -> IO ()
789789readWriteBracket_Unfold inh devNull =
790− let readEx = UF.bracket_ return (\_ -> hClose inh) FH.read
790+ let readEx = UF.bracket_ return (\_ -> hClose inh) FH.reader
791791 in S.fold (FH.write devNull) $ S.unfold readEx inh
792792
793793#ifdef INSPECTION
@@ -797,7 +797,7 @@ inspect $ hasNoTypeClasses 'readWriteBracket_Unfold
797797
798798readWriteBracketUnfold :: Handle -> Handle -> IO ()
799799readWriteBracketUnfold inh devNull =
800− let readEx = UF.bracket return (\_ -> hClose inh) FH.read
800+ let readEx = UF.bracket return (\_ -> hClose inh) FH.reader
801801 in S.fold (FH.write devNull) $ S.unfold readEx inh
802802
803803o_1_space_copy_read_exceptions :: BenchEnv -> [Benchmark]
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+14−14
@@ -59,7 +59,7 @@ import Test.Inspection
5959
6060-- | Get the last byte from a file bytestream.
6161readLast :: Handle -> IO (Maybe Word8)
62−readLast = S.last . S.unfold FH.read
62+readLast = S.last . S.unfold FH.reader
6363
6464#ifdef INSPECTION
6565inspect $ hasNoTypeClasses 'readLast
@@ -71,7 +71,7 @@ inspect $ 'readLast `hasNoType` ''MA.ArrayUnsafe -- FH.read/A.read
7171-- assert that flattenArrays constructors are not present
7272-- | Count the number of bytes in a file.
7373readCountBytes :: Handle -> IO Int
74−readCountBytes = S.length . S.unfold FH.read
74+readCountBytes = S.length . S.unfold FH.reader
7575
7676#ifdef INSPECTION
7777inspect $ hasNoTypeClasses 'readCountBytes
@@ -86,7 +86,7 @@ readCountLines =
8686 S.length
8787 . IUS.lines FL.drain
8888 . SS.decodeLatin1
89− . S.unfold FH.read
89+ . S.unfold FH.reader
9090
9191#ifdef INSPECTION
9292inspect $ hasNoTypeClasses 'readCountLines
@@ -101,7 +101,7 @@ readCountWords =
101101 S.length
102102 . IUS.words FL.drain
103103 . SS.decodeLatin1
104− . S.unfold FH.read
104+ . S.unfold FH.reader
105105
106106#ifdef INSPECTION
107107inspect $ hasNoTypeClasses 'readCountWords
@@ -110,7 +110,7 @@ inspect $ hasNoTypeClasses 'readCountWords
110110
111111-- | Sum the bytes in a file.
112112readSumBytes :: Handle -> IO Word8
113−readSumBytes = S.sum . S.unfold FH.read
113+readSumBytes = S.sum . S.unfold FH.reader
114114
115115#ifdef INSPECTION
116116inspect $ hasNoTypeClasses 'readSumBytes
@@ -130,7 +130,7 @@ inspect $ 'readSumBytes `hasNoType` ''MA.ArrayUnsafe -- FH.read/A.read
130130-- fusion-plugin to propagate INLINE phase information such that this problem
131131-- does not occur.
132132readDrain :: Handle -> IO ()
133−readDrain inh = S.drain $ S.unfold FH.read inh
133+readDrain inh = S.drain $ S.unfold FH.reader inh
134134
135135-- XXX investigate why we need an INLINE in this case (GHC)
136136{-# INLINE readDecodeLatin1 #-}
@@ -138,13 +138,13 @@ readDecodeLatin1 :: Handle -> IO ()
138138readDecodeLatin1 inh =
139139 S.drain
140140 $ SS.decodeLatin1
141− $ S.unfold FH.read inh
141+ $ S.unfold FH.reader inh
142142
143143readDecodeUtf8 :: Handle -> IO ()
144144readDecodeUtf8 inh =
145145 S.drain
146146 $ SS.decodeUtf8
147− $ S.unfold FH.read inh
147+ $ S.unfold FH.reader inh
148148
149149#ifdef INSPECTION
150150inspect $ hasNoTypeClasses 'readDecodeUtf8
@@ -189,7 +189,7 @@ getChunksConcatUnfoldCountLines inh =
189189 $ IUS.lines FL.drain
190190 $ SS.decodeLatin1
191191 -- XXX replace with toBytes
192− $ S.unfoldMany A.read (IFH.getChunks inh)
192+ $ S.unfoldMany A.read (IFH.readChunks inh)
193193
194194#ifdef INSPECTION
195195inspect $ hasNoTypeClasses 'getChunksConcatUnfoldCountLines
@@ -210,15 +210,15 @@ o_1_space_reduce_toBytes env =
210210-------------------------------------------------------------------------------
211211
212212chunksOfSum :: Int -> Handle -> IO Int
213−chunksOfSum n inh = S.length $ S.chunksOf n FL.sum (S.unfold FH.read inh)
213+chunksOfSum n inh = S.length $ S.chunksOf n FL.sum (S.unfold FH.reader inh)
214214
215215foldManyPostChunksOfSum :: Int -> Handle -> IO Int
216216foldManyPostChunksOfSum n inh =
217− S.length $ IP.foldManyPost (FL.take n FL.sum) (S.unfold FH.read inh)
217+ S.length $ IP.foldManyPost (FL.take n FL.sum) (S.unfold FH.reader inh)
218218
219219foldManyChunksOfSum :: Int -> Handle -> IO Int
220220foldManyChunksOfSum n inh =
221− S.length $ IP.foldMany (FL.take n FL.sum) (S.unfold FH.read inh)
221+ S.length $ IP.foldMany (FL.take n FL.sum) (S.unfold FH.reader inh)
222222
223223-- XXX investigate why we need an INLINE in this case (GHC)
224224-- Even though allocations remain the same in both cases inlining improves time
@@ -228,7 +228,7 @@ foldManyChunksOfSum n inh =
228228chunksOf :: Int -> Handle -> IO Int
229229chunksOf n inh =
230230 -- writeNUnsafe gives 2.5x boost here over writeN.
231− S.length $ S.chunksOf n (AT.writeNUnsafe n) (S.unfold FH.read inh)
231+ S.length $ S.chunksOf n (AT.writeNUnsafe n) (S.unfold FH.reader inh)
232232
233233#ifdef INSPECTION
234234inspect $ hasNoTypeClasses 'chunksOf
@@ -241,7 +241,7 @@ inspect $ 'chunksOf `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
241241
242242{-# INLINE arraysOf #-}
243243arraysOf :: Int -> Handle -> IO Int
244−arraysOf n inh = S.length $ IP.arraysOf n (S.unfold FH.read inh)
244+arraysOf n inh = S.length $ IP.arraysOf n (S.unfold FH.reader inh)
245245
246246o_1_space_reduce_read_grouped :: BenchEnv -> [Benchmark]
247247o_1_space_reduce_read_grouped env =
benchmark/Streamly/Benchmark/FileSystem/Handle/ReadWrite.hs+8−8
@@ -52,7 +52,7 @@ import Test.Inspection
5252
5353-- | Copy file
5454copyChunks :: Handle -> Handle -> IO ()
55−copyChunks inh outh = S.fold (IFH.writeChunks outh) $ IFH.getChunks inh
55+copyChunks inh outh = S.fold (IFH.writeChunks outh) $ IFH.readChunks inh
5656
5757#ifdef INSPECTION
5858inspect $ hasNoTypeClasses 'copyChunks
@@ -75,7 +75,7 @@ o_1_space_copy_chunked env =
7575
7676-- | Copy file
7777copyStream :: Handle -> Handle -> IO ()
78−copyStream inh outh = S.fold (FH.write outh) (S.unfold FH.read inh)
78+copyStream inh outh = S.fold (FH.write outh) (S.unfold FH.reader inh)
7979
8080#ifdef INSPECTION
8181inspect $ hasNoTypeClasses 'copyStream
@@ -102,7 +102,7 @@ o_1_space_copy_read env =
102102
103103-- | Send the file contents to /dev/null
104104readFromBytesNull :: Handle -> Handle -> IO ()
105−readFromBytesNull inh devNull = IFH.putBytes devNull $ S.unfold FH.read inh
105+readFromBytesNull inh devNull = IFH.putBytes devNull $ S.unfold FH.reader inh
106106
107107#ifdef INSPECTION
108108inspect $ hasNoTypeClasses 'readFromBytesNull
@@ -116,7 +116,7 @@ inspect $ 'readFromBytesNull `hasNoType` ''D.FoldMany
116116readWithFromBytesNull :: Handle -> Handle -> IO ()
117117readWithFromBytesNull inh devNull =
118118 IFH.putBytes devNull
119− $ S.unfold FH.readWith (defaultChunkSize, inh)
119+ $ S.unfold FH.readerWith (defaultChunkSize, inh)
120120
121121#ifdef INSPECTION
122122inspect $ hasNoTypeClasses 'readWithFromBytesNull
@@ -134,7 +134,7 @@ _readChunks inh devNull = IUF.fold fld unf inh
134134 where
135135
136136 fld = FH.write devNull
137− unf = IUF.many A.read FH.readChunks
137+ unf = IUF.many A.read FH.chunkReader
138138
139139-- | Send the chunk content to /dev/null
140140-- Implicitly benchmarked via 'readWithFromBytesNull'
@@ -144,7 +144,7 @@ _readChunksWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
144144 where
145145
146146 fld = FH.write devNull
147− unf = IUF.many A.read FH.readChunksWith
147+ unf = IUF.many A.read FH.chunkReaderWith
148148
149149o_1_space_copy_fromBytes :: BenchEnv -> [Benchmark]
150150o_1_space_copy_fromBytes env =
@@ -163,7 +163,7 @@ writeReadWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
163163 where
164164
165165 fld = FH.writeWith defaultChunkSize devNull
166− unf = FH.readWith
166+ unf = FH.readerWith
167167
168168#ifdef INSPECTION
169169inspect $ hasNoTypeClasses 'writeReadWith
@@ -180,7 +180,7 @@ writeRead inh devNull = IUF.fold fld unf inh
180180 where
181181
182182 fld = FH.write devNull
183− unf = FH.read
183+ unf = FH.reader
184184
185185#ifdef INSPECTION
186186inspect $ hasNoTypeClasses 'writeRead
benchmark/Streamly/Benchmark/Unicode/Stream.hs+13−14
@@ -23,7 +23,6 @@ import System.IO (Handle)
2323
2424import qualified Streamly.Data.Array.Unboxed as Array
2525import qualified Streamly.Data.Fold as Fold
26−import qualified Streamly.FileSystem.Handle as Handle
2726import qualified Streamly.Internal.Data.Stream.IsStream as Stream
2827import qualified Streamly.Internal.Data.Unfold as Unfold
2928import qualified Streamly.Internal.FileSystem.Handle as Handle
@@ -54,7 +53,7 @@ copyCodecUtf8ArraysLenient inh outh =
5453 Stream.fold (Handle.write outh)
5554 $ Unicode.encodeUtf8'
5655 $ Unicode.decodeUtf8Arrays
57− $ Handle.getChunks inh
56+ $ Handle.readChunks inh
5857
5958#ifdef INSPECTION
6059inspect $ hasNoTypeClasses 'copyCodecUtf8ArraysLenient
@@ -82,7 +81,7 @@ linesUnlinesCopy inh outh =
8281 $ Unicode.unlines Unfold.fromList
8382 $ Stream.splitOnSuffix (== '\n') Fold.toList
8483 $ Unicode.decodeLatin1
85− $ Stream.unfold Handle.read inh
84+ $ Stream.unfold Handle.reader inh
8685
8786{-# NOINLINE linesUnlinesArrayWord8Copy #-}
8887linesUnlinesArrayWord8Copy :: Handle -> Handle -> IO ()
@@ -90,7 +89,7 @@ linesUnlinesArrayWord8Copy inh outh =
9089 Stream.fold (Handle.write outh)
9190 $ Stream.interposeSuffix 10 Array.read
9291 $ Stream.splitOnSuffix (== 10) Array.write
93− $ Stream.unfold Handle.read inh
92+ $ Stream.unfold Handle.reader inh
9493
9594-- XXX splitSuffixOn requires -funfolding-use-threshold=150 for better fusion
9695-- | Lines and unlines
@@ -102,7 +101,7 @@ linesUnlinesArrayCharCopy inh outh =
102101 $ UnicodeArr.unlines
103102 $ UnicodeArr.lines
104103 $ Unicode.decodeLatin1
105− $ Stream.unfold Handle.read inh
104+ $ Stream.unfold Handle.reader inh
106105
107106#ifdef INSPECTION
108107inspect $ hasNoTypeClassesExcept 'linesUnlinesArrayCharCopy [''Unboxed]
@@ -130,7 +129,7 @@ wordsUnwordsCopyWord8 inh outh =
130129 Stream.fold (Handle.write outh)
131130 $ Stream.interposeSuffix 32 Unfold.fromList
132131 $ Stream.wordsBy isSp Fold.toList
133− $ Stream.unfold Handle.read inh
132+ $ Stream.unfold Handle.reader inh
134133
135134#ifdef INSPECTION
136135inspect $ hasNoTypeClasses 'wordsUnwordsCopyWord8
@@ -154,7 +153,7 @@ wordsUnwordsCopy inh outh =
154153 $ Stream.wordsBy isSpace Fold.toList
155154 -- -- $ Stream.splitOn isSpace Fold.toList
156155 $ Unicode.decodeLatin1
157− $ Stream.unfold Handle.read inh
156+ $ Stream.unfold Handle.reader inh
158157
159158#ifdef INSPECTION
160159-- inspect $ hasNoTypeClasses 'wordsUnwordsCopy
@@ -169,7 +168,7 @@ wordsUnwordsCharArrayCopy inh outh =
169168 $ UnicodeArr.unwords
170169 $ UnicodeArr.words
171170 $ Unicode.decodeLatin1
172− $ Stream.unfold Handle.read inh
171+ $ Stream.unfold Handle.reader inh
173172
174173o_1_space_copy_read_group_ungroup :: BenchEnv -> [Benchmark]
175174o_1_space_copy_read_group_ungroup env =
@@ -201,7 +200,7 @@ copyStreamLatin1' inh outh =
201200 Stream.fold (Handle.write outh)
202201 $ Unicode.encodeLatin1'
203202 $ Unicode.decodeLatin1
204− $ Stream.unfold Handle.read inh
203+ $ Stream.unfold Handle.reader inh
205204
206205#ifdef INSPECTION
207206inspect $ hasNoTypeClasses 'copyStreamLatin1'
@@ -221,7 +220,7 @@ copyStreamLatin1 inh outh =
221220 Stream.fold (Handle.write outh)
222221 $ Unicode.encodeLatin1
223222 $ Unicode.decodeLatin1
224− $ Stream.unfold Handle.read inh
223+ $ Stream.unfold Handle.reader inh
225224
226225#ifdef INSPECTION
227226inspect $ hasNoTypeClasses 'copyStreamLatin1
@@ -241,7 +240,7 @@ _copyStreamUtf8' inh outh =
241240 Stream.fold (Handle.write outh)
242241 $ Unicode.encodeUtf8'
243242 $ Unicode.decodeUtf8'
244− $ Stream.unfold Handle.read inh
243+ $ Stream.unfold Handle.reader inh
245244
246245#ifdef INSPECTION
247246inspect $ hasNoTypeClasses '_copyStreamUtf8'
@@ -257,7 +256,7 @@ copyStreamUtf8 inh outh =
257256 Stream.fold (Handle.write outh)
258257 $ Unicode.encodeUtf8
259258 $ Unicode.decodeUtf8
260− $ Stream.unfold Handle.read inh
259+ $ Stream.unfold Handle.reader inh
261260
262261#ifdef INSPECTION
263262inspect $ hasNoTypeClasses 'copyStreamUtf8
@@ -272,7 +271,7 @@ _copyStreamUtf8'Fold inh outh =
272271 Stream.fold (Handle.write outh)
273272 $ Unicode.encodeUtf8
274273 $ Stream.foldMany Unicode.writeCharUtf8'
275− $ Stream.unfold Handle.read inh
274+ $ Stream.unfold Handle.reader inh
276275
277276{-# NOINLINE _copyStreamUtf8Parser #-}
278277_copyStreamUtf8Parser :: Handle -> Handle -> IO ()
@@ -281,7 +280,7 @@ _copyStreamUtf8Parser inh outh =
281280 $ Unicode.encodeUtf8
282281 $ Stream.parseMany
283282 (Unicode.parseCharUtf8With Unicode.TransliterateCodingFailure)
284− $ Stream.unfold Handle.read inh
283+ $ Stream.unfold Handle.reader inh
285284
286285o_1_space_decode_encode_read :: BenchEnv -> [Benchmark]
287286o_1_space_decode_encode_read env =
core/src/Streamly/Console/Stdio.hs+29−4
@@ -13,9 +13,9 @@
1313
1414module Streamly.Console.Stdio
1515 (
16− -- * Read (stdin)
17− read
18− , readChunks
16+ -- * Unfolds (stdin)
17+ reader
18+ , chunkReader
1919
2020 -- * Write (stdout)
2121 , write
@@ -24,8 +24,33 @@ module Streamly.Console.Stdio
2424 -- * Write (stderr)
2525 , writeErr
2626 , writeErrChunks
27+
28+ -- * Deprecated
29+ , read
30+ , readChunks
2731 )
2832where
2933
30−import Streamly.Internal.Console.Stdio
34+import Control.Monad.IO.Class (MonadIO(..))
35+import Data.Word (Word8)
36+import Streamly.Internal.Data.Array.Unboxed.Type (Array)
37+import Streamly.Internal.Data.Unfold (Unfold)
38+
39+import Streamly.Internal.Console.Stdio hiding (read, readChunks)
3140import Prelude hiding (read)
41+
42+-- Same as 'reader'
43+--
44+-- @since 0.8.0
45+{-# DEPRECATED read "Please use 'reader' instead" #-}
46+{-# INLINE read #-}
47+read :: MonadIO m => Unfold m () Word8
48+read = reader
49+
50+-- Same as 'chunkReader'
51+--
52+-- @since 0.8.0
53+{-# DEPRECATED readChunks "Please use 'chunkReader' instead" #-}
54+{-# INLINE readChunks #-}
55+readChunks :: MonadIO m => Unfold m () (Array Word8)
56+readChunks = chunkReader
core/src/Streamly/FileSystem/Handle.hs+38−7
@@ -75,12 +75,19 @@ module Streamly.FileSystem.Handle
7575 -- position of the file handle. The stream ends as soon as EOF is
7676 -- encountered.
7777
78− , read
79− , readWith
80− , readChunks
81− , readChunksWith
78+ -- -- *** Streams
79+ -- , read
80+ -- , readWith
81+ -- , readChunks
82+ -- , readChunksWith
83+
84+ -- -- *** Unfolds
85+ , reader
86+ , readerWith
87+ , chunkReader
88+ , chunkReaderWith
8289
83− -- ** Writing
90+ -- ** Folds
8491 -- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
8592 -- underlying handle are ignored. The write occurs from the current seek
8693 -- position of the file handle. The write behavior depends on the 'IOMode'
@@ -91,12 +98,36 @@ module Streamly.FileSystem.Handle
9198 , writeChunks
9299
93100 -- * Deprecated
94− , readChunksWithBufferOf
101+ , read
95102 , readWithBufferOf
103+ , readChunks
104+ , readChunksWithBufferOf
96105 , writeChunksWithBufferOf
97106 , writeWithBufferOf
98107 )
99108where
100109
101−import Streamly.Internal.FileSystem.Handle
110+import Control.Monad.IO.Class (MonadIO(..))
111+import Data.Word (Word8)
112+import Streamly.Internal.Data.Array.Unboxed.Type (Array)
113+import Streamly.Internal.Data.Unfold.Type (Unfold)
114+import System.IO (Handle)
115+
116+import Streamly.Internal.FileSystem.Handle hiding (read, readChunks)
102117import Prelude hiding (read)
118+
119+-- | Same as 'reader'
120+--
121+-- @since 0.7.0
122+{-# DEPRECATED read "Please use 'reader' instead" #-}
123+{-# INLINE read #-}
124+read :: MonadIO m => Unfold m Handle Word8
125+read = reader
126+
127+-- | Same as 'chunkReader'
128+--
129+-- @since 0.7.0
130+{-# DEPRECATED readChunks "Please use 'chunkReader' instead" #-}
131+{-# INLINE readChunks #-}
132+readChunks :: MonadIO m => Unfold m Handle (Array Word8)
133+readChunks = chunkReader
core/src/Streamly/Internal/Console/Stdio.hs+33−29
@@ -9,25 +9,29 @@
99
1010module Streamly.Internal.Console.Stdio
1111 (
12− -- * Read
12+ -- * Streams
1313 read
14− , getBytes
15− , getChars
14+ , readChars
1615 , readChunks
17− , getChunks
1816 -- , getChunksLn
1917 -- , getStringsWith -- get strings using the supplied decoding
2018 -- , getStrings -- get strings of complete chars,
2119 -- leave any partial chars for next string
2220 -- , getStringsLn -- get lines decoded as char strings
2321
24− -- * Write
22+ -- * Unfolds
23+ , reader
24+ , chunkReader
25+
26+ -- * Folds
2527 , write
28+ , writeChunks
2629 , writeErr
30+ , writeErrChunks
31+
32+ -- * Stream writes
2733 , putBytes -- Buffered (32K)
2834 , putChars
29− , writeChunks
30− , writeErrChunks
3135 , putChunks -- Unbuffered
3236 , putStringsWith
3337 , putStrings
@@ -59,51 +63,51 @@ import qualified Streamly.Internal.Unicode.Stream as Unicode
5963
6064-- | Unfold standard input into a stream of 'Word8'.
6165--
62−-- @since 0.8.0
63−{-# INLINE read #-}
64−read :: MonadIO m => Unfold m () Word8
65−read = Unfold.lmap (\() -> stdin) Handle.read
66+-- @since 0.9.0
67+{-# INLINE reader #-}
68+reader :: MonadIO m => Unfold m () Word8
69+reader = Unfold.lmap (\() -> stdin) Handle.reader
6670
6771-- | Read a byte stream from standard input.
6872--
69−-- > getBytes = Handle.getBytes stdin
70−-- > getBytes = Stream.unfold Stdio.read ()
73+-- > read = Handle.read stdin
74+-- > read = Stream.unfold Stdio.reader ()
7175--
7276-- /Pre-release/
7377--
74−{-# INLINE getBytes #-}
75−getBytes :: MonadIO m => Stream m Word8
76−getBytes = Handle.getBytes stdin
78+{-# INLINE read #-}
79+read :: MonadIO m => Stream m Word8
80+read = Handle.read stdin
7781
7882-- | Read a character stream from Utf8 encoded standard input.
7983--
80−-- > getChars = Unicode.decodeUtf8 Stdio.getBytes
84+-- > readChars = Unicode.decodeUtf8 Stdio.read
8185--
8286-- /Pre-release/
8387--
84−{-# INLINE getChars #-}
85−getChars :: MonadIO m => Stream m Char
86−getChars = Unicode.decodeUtf8 getBytes
88+{-# INLINE readChars #-}
89+readChars :: MonadIO m => Stream m Char
90+readChars = Unicode.decodeUtf8 read
8791
8892-- | Unfolds standard input into a stream of 'Word8' arrays.
8993--
90−-- @since 0.8.0
91−{-# INLINE readChunks #-}
92−readChunks :: MonadIO m => Unfold m () (Array Word8)
93−readChunks = Unfold.lmap (\() -> stdin) Handle.readChunks
94+-- @since 0.9.0
95+{-# INLINE chunkReader #-}
96+chunkReader :: MonadIO m => Unfold m () (Array Word8)
97+chunkReader = Unfold.lmap (\() -> stdin) Handle.chunkReader
9498
9599-- | Read a stream of chunks from standard input. The maximum size of a single
96100-- chunk is limited to @defaultChunkSize@. The actual size read may be less
97101-- than @defaultChunkSize@.
98102--
99−-- > getChunks = Handle.getChunks stdin
100−-- > getChunks = Stream.unfold Stdio.readChunks ()
103+-- > readChunks = Handle.readChunks stdin
104+-- > readChunks = Stream.unfold Stdio.chunkReader ()
101105--
102106-- /Pre-release/
103107--
104−{-# INLINE getChunks #-}
105−getChunks :: MonadIO m => Stream m (Array Word8)
106−getChunks = Handle.getChunks stdin
108+{-# INLINE readChunks #-}
109+readChunks :: MonadIO m => Stream m (Array Word8)
110+readChunks = Handle.readChunks stdin
107111
108112{-
109113-- | Read UTF8 encoded lines from standard input.
core/src/Streamly/Internal/FileSystem/Handle.hs+82−96
@@ -30,41 +30,41 @@ module Streamly.Internal.FileSystem.Handle
3030 , getChunkOf
3131 , putChunk
3232
33− -- * Byte Stream Read
33+ -- * Streams
3434 , read
35− -- , readUtf8
36− -- , readLines
37− -- , readFrames
3835 , readWith
39−
40− , getBytes
41− , getBytesWith
42−
43− -- * Chunked Stream Read
44− , readChunks
4536 , readChunksWith
37+ , readChunks
4638
47− , getChunksWith
48− , getChunks
39+ -- * Unfolds
40+ , reader
41+ -- , readUtf8
42+ -- , readLines
43+ -- , readFrames
44+ , readerWith
45+ , chunkReader
46+ , chunkReaderWith
4947
50− -- * Byte Stream Write
51− -- Byte stream write (Folds)
48+ -- * Folds
5249 , write
53− , consumer
5450 -- , writeUtf8
5551 -- , writeUtf8ByLines
5652 -- , writeByFrames
5753 -- , writeLines
5854 , writeWith
55+ , writeChunks
56+ , writeChunksWith
5957 , writeMaybesWith
6058
59+ -- * Refolds
60+ , writer
61+ , writerWith
62+ , chunkWriter
63+ -- , chunkWriterWith
64+
65+ -- * Stream writes
6166 , putBytes
6267 , putBytesWith
63−
64− -- * Chunked Stream Write
65− , writeChunks
66− , writeChunksWith
67−
6868 , putChunksWith
6969 , putChunks
7070
@@ -86,7 +86,7 @@ module Streamly.Internal.FileSystem.Handle
8686
8787 -- , readChunksFrom
8888 -- , readChunksFromTo
89− , readChunksFromToWith
89+ , chunkReaderFromToWith
9090 -- , readChunksFromThenToWith
9191
9292 -- , writeIndex
@@ -101,6 +101,7 @@ module Streamly.Internal.FileSystem.Handle
101101 -- , writeChunksFromTo
102102 -- , writeChunksFromToWith
103103 -- , writeChunksFromThenToWith
104+
104105 -- * Deprecated
105106 , readChunksWithBufferOf
106107 , readWithBufferOf
@@ -145,9 +146,9 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K (mkStream)
145146-- >>> import qualified Streamly.Data.Array.Unboxed as Array
146147-- >>> import qualified Streamly.Data.Fold as Fold
147148-- >>> import qualified Streamly.Data.Unfold as Unfold
148−-- >>> import qualified Streamly.FileSystem.Handle as Handle
149149-- >>> import qualified Streamly.Prelude as Stream
150150--
151+-- >>> import qualified Streamly.Internal.Data.Array.Unboxed.Type as Array (writeNUnsafe)
151152-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
152153-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
153154-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
@@ -220,16 +221,16 @@ _getChunksWith size h = S.fromStreamK go
220221 then stp
221222 else yld arr go
222223
223−-- | @getChunksWith size handle@ reads a stream of arrays from the file
224+-- | @readChunksWith size handle@ reads a stream of arrays from the file
224225-- handle @handle@. The maximum size of a single array is limited to @size@.
225226-- The actual size read may be less than or equal to @size@.
226227--
227−-- >>> getChunksWith size h = Stream.unfold Handle.readChunksWith (size, h)
228+-- >>> readChunksWith size h = Stream.unfold Handle.chunkReaderWith (size, h)
228229--
229230-- @since 0.9.0
230−{-# INLINE_NORMAL getChunksWith #-}
231−getChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
232−getChunksWith size h = S.fromStreamD (D.Stream step ())
231+{-# INLINE_NORMAL readChunksWith #-}
232+readChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
233+readChunksWith size h = S.fromStreamD (D.Stream step ())
233234 where
234235 {-# INLINE_LATE step #-}
235236 step _ _ = do
@@ -245,20 +246,19 @@ getChunksWith size h = S.fromStreamD (D.Stream step ())
245246-- or equal to @bufsize@.
246247--
247248-- @since 0.9.0
248−{-# INLINE_NORMAL readChunksWith #-}
249−readChunksWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
250−readChunksWith =
249+{-# INLINE_NORMAL chunkReaderWith #-}
250+chunkReaderWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
251+chunkReaderWith =
251252 UF.lmap (uncurry getChunk) UF.repeatM
252253 & UF.takeWhile ((/= 0) . byteLength)
253254
254−
255−-- | Same as 'readChunksWith'
255+-- | Same as 'chunkReaderWith'
256256--
257257-- @since 0.7.0
258−{-# DEPRECATED readChunksWithBufferOf "Please use readChunksWith instead." #-}
258+{-# DEPRECATED readChunksWithBufferOf "Please use chunkReaderWith instead." #-}
259259{-# INLINE_NORMAL readChunksWithBufferOf #-}
260260readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
261−readChunksWithBufferOf = readChunksWith
261+readChunksWithBufferOf = chunkReaderWith
262262
263263-- There are two ways to implement this.
264264--
@@ -267,15 +267,14 @@ readChunksWithBufferOf = readChunksWith
267267-- 2. Simply implement it from scratch like readChunksWith.
268268--
269269-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
270−--
270+
271271-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
272272-- reading from the offset `from` in the file and reads up to the offset `to`.
273273--
274−--
275−{-# INLINE_NORMAL readChunksFromToWith #-}
276−readChunksFromToWith :: MonadIO m =>
274+{-# INLINE_NORMAL chunkReaderFromToWith #-}
275+chunkReaderFromToWith :: MonadIO m =>
277276 Unfold m (Int, Int, Int, Handle) (Array Word8)
278−readChunksFromToWith = Unfold step inject
277+chunkReaderFromToWith = Unfold step inject
279278
280279 where
281280
@@ -297,20 +296,17 @@ readChunksFromToWith = Unfold step inject
297296 assert (len <= remaining)
298297 $ D.Yield arr (remaining - len, bufSize, h)
299298
300−
301−-- XXX read 'Array a' instead of Word8
302−--
303299-- | @getChunks handle@ reads a stream of arrays from the specified file
304300-- handle. The maximum size of a single array is limited to
305301-- @defaultChunkSize@. The actual size read may be less than or equal to
306302-- @defaultChunkSize@.
307303--
308−-- >>> getChunks = Handle.getChunksWith IO.defaultChunkSize
304+-- >>> readChunks = Handle.readChunksWith IO.defaultChunkSize
309305--
310−-- @since 0.9.0
311−{-# INLINE getChunks #-}
312−getChunks :: MonadIO m => Handle -> Stream m (Array Word8)
313−getChunks = getChunksWith defaultChunkSize
306+-- /Pre-release/
307+{-# INLINE readChunks #-}
308+readChunks :: MonadIO m => Handle -> Stream m (Array Word8)
309+readChunks = readChunksWith defaultChunkSize
314310
315311-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
316312-- device are performed using a buffer of size
@@ -318,12 +314,12 @@ getChunks = getChunksWith defaultChunkSize
318314-- size of arrays in the resulting stream are therefore less than or equal to
319315-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
320316--
321−-- >>> readChunks = Unfold.first IO.defaultChunkSize Handle.readChunksWith
317+-- >>> chunkReader = Unfold.first IO.defaultChunkSize Handle.chunkReaderWith
322318--
323−-- @since 0.7.0
324−{-# INLINE readChunks #-}
325−readChunks :: MonadIO m => Unfold m Handle (Array Word8)
326−readChunks = UF.first defaultChunkSize readChunksWith
319+-- @since 0.9.0
320+{-# INLINE chunkReader #-}
321+chunkReader :: MonadIO m => Unfold m Handle (Array Word8)
322+chunkReader = UF.first defaultChunkSize chunkReaderWith
327323
328324-------------------------------------------------------------------------------
329325-- Read File to Stream
@@ -336,54 +332,50 @@ readChunks = UF.first defaultChunkSize readChunksWith
336332-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
337333-- to the IO device are performed using buffers of @bufsize@.
338334--
339−-- >>> readWith = Unfold.many Array.read Handle.readChunksWith
335+-- >>> readerWith = Unfold.many Array.read Handle.chunkReaderWith
340336--
341337-- @since 0.9.0
342−{-# INLINE readWith #-}
343−readWith :: MonadIO m => Unfold m (Int, Handle) Word8
344−readWith = UF.many A.read readChunksWith
338+{-# INLINE readerWith #-}
339+readerWith :: MonadIO m => Unfold m (Int, Handle) Word8
340+readerWith = UF.many A.read chunkReaderWith
345341
346−-- | Same as 'readWith'
342+-- | Same as 'readerWith'
347343--
348344-- @since 0.7.0
349−{-# DEPRECATED readWithBufferOf "Please use readWith instead." #-}
345+{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead." #-}
350346{-# INLINE readWithBufferOf #-}
351347readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
352−readWithBufferOf = readWith
348+readWithBufferOf = readerWith
353349
354−-- | @getBytesWith bufsize handle@ reads a byte stream from a file
350+-- | @readWith bufsize handle@ reads a byte stream from a file
355351-- handle, reads are performed in chunks of up to @bufsize@.
356352--
357−-- >>> getBytesWith size h = Stream.unfoldMany Array.read $ Handle.getChunksWith size h
353+-- >>> readWith size h = Stream.unfoldMany Array.read $ Handle.readChunksWith size h
358354--
359355-- /Pre-release/
360−{-# INLINE getBytesWith #-}
361−getBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8
362−getBytesWith size h = AS.concat $ getChunksWith size h
356+{-# INLINE readWith #-}
357+readWith :: MonadIO m => Int -> Handle -> Stream m Word8
358+readWith size h = AS.concat $ readChunksWith size h
363359
364−-- TODO
365−-- Generate a stream of elements of the given type from a file 'Handle'.
366−-- read :: (MonadIO m, Unboxed a) => Handle -> Stream m a
367−--
368360-- | Unfolds a file handle into a byte stream. IO requests to the device are
369361-- performed in sizes of
370362-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
371363--
372−-- >>> read = Unfold.many Handle.readChunks Array.read
364+-- >>> reader = Unfold.many Array.read chunkReader
373365--
374−-- @since 0.7.0
375−{-# INLINE read #-}
376−read :: MonadIO m => Unfold m Handle Word8
377−read = UF.many A.read readChunks
366+-- @since 0.9.0
367+{-# INLINE reader #-}
368+reader :: MonadIO m => Unfold m Handle Word8
369+reader = UF.many A.read chunkReader
378370
379371-- | Generate a byte stream from a file 'Handle'.
380372--
381−-- >>> getBytes h = Stream.unfoldMany Array.read $ Handle.getChunks h
373+-- >>> read h = Stream.unfoldMany Array.read $ Handle.readChunks h
382374--
383375-- /Pre-release/
384−{-# INLINE getBytes #-}
385−getBytes :: MonadIO m => Handle -> Stream m Word8
386−getBytes = AS.concat . getChunks
376+{-# INLINE read #-}
377+read :: MonadIO m => Handle -> Stream m Word8
378+read = AS.concat . readChunks
387379
388380-------------------------------------------------------------------------------
389381-- Writing
@@ -479,12 +471,12 @@ writeChunks h = FL.drainBy (putChunk h)
479471-- | Like writeChunks but uses the experimental 'Refold' API.
480472--
481473-- /Internal/
482−{-# INLINE consumeChunks #-}
483−consumeChunks :: MonadIO m => Refold m Handle (Array a) ()
484−consumeChunks = Refold.drainBy putChunk
474+{-# INLINE chunkWriter #-}
475+chunkWriter :: MonadIO m => Refold m Handle (Array a) ()
476+chunkWriter = Refold.drainBy putChunk
485477
486478-- XXX lpackArraysChunksOf should be written idiomatically
487−--
479+
488480-- | @writeChunksWith bufsize handle@ writes a stream of arrays
489481-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
490482-- We never split an array, if a single array is bigger than the specified size
@@ -515,7 +507,7 @@ writeChunksWithBufferOf = writeChunksWith
515507-- writes as well.
516508
517509-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
518−--
510+
519511-- | @writeWith reqSize handle@ writes the input stream to @handle@.
520512-- Bytes in the input stream are collected into a buffer until we have a chunk
521513-- of @reqSize@ and then written to the IO device.
@@ -548,13 +540,13 @@ writeMaybesWith n h =
548540 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
549541 in FL.many writeOnNothing (writeChunks h)
550542
551−-- | Like 'writeWith' but uses the experimental 'Refold' API.
543+-- | Like 'writeWith' but uses the experimental 'Refold' API.
552544--
553545-- /Internal/
554−{-# INLINE consumerWith #-}
555−consumerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
556−consumerWith n =
557− FL.refoldMany (FL.take n $ writeNUnsafe n) consumeChunks
546+{-# INLINE writerWith #-}
547+writerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
548+writerWith n =
549+ FL.refoldMany (FL.take n $ writeNUnsafe n) chunkWriter
558550
559551-- | Write a byte stream to a file handle. Accumulates the input in chunks of
560552-- up to 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize' before writing
@@ -570,15 +562,9 @@ write = writeWith defaultChunkSize
570562-- | Like 'write' but uses the experimental 'Refold' API.
571563--
572564-- /Internal/
573−{-# INLINE consumer #-}
574−consumer :: MonadIO m => Refold m Handle Word8 ()
575−consumer = consumerWith defaultChunkSize
576−
577−{-
578−{-# INLINE write #-}
579−write :: (MonadIO m, Unboxed a) => Handle -> Stream m a -> m ()
580−write = toHandleWith A.defaultChunkSize
581−-}
565+{-# INLINE writer #-}
566+writer :: MonadIO m => Refold m Handle Word8 ()
567+writer = writerWith defaultChunkSize
582568
583569-- XXX mmap a file into an array. This could be useful for in-place operations
584570-- on a file. For example, we can quicksort the contents of a file by mmapping
hie.yaml+3−1
@@ -34,13 +34,15 @@ cradle:
3434 component: "bench:Data.Stream"
3535 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Expand.hs"
3636 component: "bench:Data.Stream"
37+ - path: "./benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs"
38+ component: "bench:Data.Stream"
3739 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Generate.hs"
3840 component: "bench:Data.Stream"
3941 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Reduce.hs"
4042 component: "bench:Data.Stream"
4143 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Transform.hs"
4244 component: "bench:Data.Stream"
43− - path: "./benchmark/Streamly/Benchmark/Data/Stream/Async.hs"
45+ - path: "./benchmark/Streamly/Benchmark/Data/Stream/Concurrent.hs"
4446 component: "bench:Data.Stream.Concurrent"
4547 - path: "./benchmark/Streamly/Benchmark/Data/Stream/ConcurrentCommon.hs"
4648 component: "bench:Data.Stream.Concurrent"
src/Streamly/Internal/FileSystem/Event/Darwin.hs+1−1
@@ -512,7 +512,7 @@ readOneEvent = do
512512
513513watchToStream :: Watch -> Stream IO Event
514514watchToStream (Watch handle _ _) =
515− S.parseMany readOneEvent $ S.unfold FH.read handle
515+ S.parseMany readOneEvent $ S.unfold FH.reader handle
516516
517517-- XXX Write tests for all the points in macOS specific behavior.
518518--
src/Streamly/Internal/FileSystem/Event/Linux.hs+1−1
@@ -879,7 +879,7 @@ watchToStream cfg wt@(Watch handle _) = do
879879 -- sizeof(struct inotify_event) + NAME_MAX + 1
880880 --
881881 -- will be sufficient to read at least one event.
882− S.parseMany (readOneEvent cfg wt) $ S.unfold FH.read handle
882+ S.parseMany (readOneEvent cfg wt) $ S.unfold FH.reader handle
883883
884884-- XXX We should not go across the mount points of network file systems or file
885885-- systems that are known to not generate any events.
src/Streamly/Internal/FileSystem/File.hs+45−46
@@ -43,37 +43,36 @@ module Streamly.Internal.FileSystem.File
4343 -- ** File IO Using Handle
4444 withFile
4545
46− -- ** Read From File
47− , readWith
46+ -- ** Streams
4847 , read
48+ , readChunksWith
49+ , readChunks
50+
51+ -- ** Unfolds
52+ , readerWith
53+ , reader
4954 -- , readShared
5055 -- , readUtf8
5156 -- , readLines
5257 -- , readFrames
53−
54− , toBytes
55−
56− -- -- * Array Read
57− , readChunksWith
58− , readChunksFromToWith
59− , readChunks
60−
61− , toChunksWith
62− , toChunks
58+ , chunkReaderWith
59+ , chunkReaderFromToWith
60+ , chunkReader
6361
6462 -- ** Write To File
63+ , putChunk -- writeChunk?
64+
65+ -- ** Folds
6566 , write
6667 -- , writeUtf8
6768 -- , writeUtf8ByLines
6869 -- , writeByFrames
6970 , writeWith
71+ , writeChunks
7072
71− , fromBytes
73+ -- ** Writing Streams
74+ , fromBytes -- putBytes?
7275 , fromBytesWith
73−
74− -- -- * Array Write
75− , putChunk
76− , writeChunks
7776 , fromChunks
7877
7978 -- ** Append To File
@@ -212,30 +211,30 @@ appendArray file arr = SIO.withFile file AppendMode (`FH.putChunk` arr)
212211-- Stream of Arrays IO
213212-------------------------------------------------------------------------------
214213
215−-- | @toChunksWith size file@ reads a stream of arrays from file @file@.
214+-- | @readChunksWith size file@ reads a stream of arrays from file @file@.
216215-- The maximum size of a single array is specified by @size@. The actual size
217216-- read may be less than or equal to @size@.
218217--
219218-- @since 0.9.0
220−{-# INLINE toChunksWith #-}
221−toChunksWith :: (MonadCatch m, MonadAsync m)
219+{-# INLINE readChunksWith #-}
220+readChunksWith :: (MonadCatch m, MonadAsync m)
222221 => Int -> FilePath -> Stream m (Array Word8)
223−toChunksWith size file =
224− withFile file ReadMode (FH.getChunksWith size)
222+readChunksWith size file =
223+ withFile file ReadMode (FH.readChunksWith size)
225224
226225-- XXX read 'Array a' instead of Word8
227226--
228−-- | @toChunks file@ reads a stream of arrays from file @file@.
227+-- | @readChunks file@ reads a stream of arrays from file @file@.
229228-- The maximum size of a single array is limited to @defaultChunkSize@. The
230229-- actual size read may be less than @defaultChunkSize@.
231230--
232−-- > toChunks = toChunksWith defaultChunkSize
231+-- > readChunks = readChunksWith defaultChunkSize
233232--
234233-- @since 0.7.0
235−{-# INLINE toChunks #-}
236−toChunks :: (MonadCatch m, MonadAsync m)
234+{-# INLINE readChunks #-}
235+readChunks :: (MonadCatch m, MonadAsync m)
237236 => FilePath -> Stream m (Array Word8)
238−toChunks = toChunksWith defaultChunkSize
237+readChunks = readChunksWith defaultChunkSize
239238
240239-------------------------------------------------------------------------------
241240-- Read File to Stream
@@ -252,10 +251,10 @@ toChunks = toChunksWith defaultChunkSize
252251--
253252-- /Pre-release/
254253--
255−{-# INLINE readChunksWith #-}
256−readChunksWith :: (MonadCatch m, MonadAsync m)
254+{-# INLINE chunkReaderWith #-}
255+chunkReaderWith :: (MonadCatch m, MonadAsync m)
257256 => Unfold m (Int, FilePath) (Array Word8)
258−readChunksWith = usingFile2 FH.readChunksWith
257+chunkReaderWith = usingFile2 FH.chunkReaderWith
259258
260259-- | Unfold the tuple @(from, to, bufsize, filepath)@ into a stream
261260-- of 'Word8' arrays.
@@ -265,10 +264,10 @@ readChunksWith = usingFile2 FH.readChunksWith
265264-- less than or equal to @bufsize@.
266265--
267266-- /Pre-release/
268−{-# INLINE readChunksFromToWith #-}
269−readChunksFromToWith :: (MonadCatch m, MonadAsync m) =>
267+{-# INLINE chunkReaderFromToWith #-}
268+chunkReaderFromToWith :: (MonadCatch m, MonadAsync m) =>
270269 Unfold m (Int, Int, Int, FilePath) (Array Word8)
271−readChunksFromToWith = usingFile3 FH.readChunksFromToWith
270+chunkReaderFromToWith = usingFile3 FH.chunkReaderFromToWith
272271
273272-- | Unfolds a 'FilePath' into a stream of 'Word8' arrays. Requests to the IO
274273-- device are performed using a buffer of size
@@ -277,26 +276,26 @@ readChunksFromToWith = usingFile3 FH.readChunksFromToWith
277276-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
278277--
279278-- /Pre-release/
280−{-# INLINE readChunks #-}
281−readChunks :: (MonadCatch m, MonadAsync m) => Unfold m FilePath (Array Word8)
282−readChunks = usingFile FH.readChunks
279+{-# INLINE chunkReader #-}
280+chunkReader :: (MonadCatch m, MonadAsync m) => Unfold m FilePath (Array Word8)
281+chunkReader = usingFile FH.chunkReader
283282
284283-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
285284-- to the IO device are performed using buffers of @bufsize@.
286285--
287286-- /Pre-release/
288−{-# INLINE readWith #-}
289−readWith :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) Word8
290−readWith = usingFile2 FH.readWith
287+{-# INLINE readerWith #-}
288+readerWith :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) Word8
289+readerWith = usingFile2 FH.readerWith
291290
292291-- | Unfolds a file path into a byte stream. IO requests to the device are
293292-- performed in sizes of
294293-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
295294--
296−-- @since 0.7.0
297−{-# INLINE read #-}
298−read :: (MonadCatch m, MonadAsync m) => Unfold m FilePath Word8
299−read = UF.many A.read (usingFile FH.readChunks)
295+-- /Pre-release/
296+{-# INLINE reader #-}
297+reader :: (MonadCatch m, MonadAsync m) => Unfold m FilePath Word8
298+reader = UF.many A.read (usingFile FH.chunkReader)
300299
301300-- | Generate a stream of bytes from a file specified by path. The stream ends
302301-- when EOF is encountered. File is locked using multiple reader and single
@@ -304,9 +303,9 @@ read = UF.many A.read (usingFile FH.readChunks)
304303--
305304-- /Pre-release/
306305--
307−{-# INLINE toBytes #-}
308−toBytes :: (MonadCatch m, MonadAsync m) => FilePath -> Stream m Word8
309−toBytes file = AS.concat $ withFile file ReadMode FH.getChunks
306+{-# INLINE read #-}
307+read :: (MonadCatch m, MonadAsync m) => FilePath -> Stream m Word8
308+read file = AS.concat $ withFile file ReadMode FH.readChunks
310309
311310{-
312311-- | Generate a stream of elements of the given type from a file 'Handle'. The
test/Streamly/Test/FileSystem/Handle.hs+8−8
@@ -77,20 +77,20 @@ executor f =
7777
7878readFromHandle :: IO (Stream IO Char)
7979readFromHandle =
80− let f = Unicode.decodeUtf8 . Stream.unfold Handle.read
80+ let f = Unicode.decodeUtf8 . Stream.unfold Handle.reader
8181 in executor f
8282
8383readWithBufferFromHandle :: IO (Stream IO Char)
8484readWithBufferFromHandle =
8585 let f1 = (\h -> (1024, h))
86− f2 = Unicode.decodeUtf8 . Stream.unfold Handle.readWith . f1
86+ f2 = Unicode.decodeUtf8 . Stream.unfold Handle.readerWith . f1
8787 in executor f2
8888
8989readChunksFromHandle :: IO (Stream IO Char)
9090readChunksFromHandle =
9191 let f = Unicode.decodeUtf8
9292 . Stream.concatMap Array.toStream
93− . Stream.unfold Handle.readChunks
93+ . Stream.unfold Handle.chunkReader
9494 in executor f
9595
9696readChunksWithBuffer :: IO (Stream IO Char)
@@ -99,7 +99,7 @@ readChunksWithBuffer =
9999 f2 =
100100 Unicode.decodeUtf8
101101 . Stream.concatMap Array.toStream
102− . Stream.unfold Handle.readChunksWith
102+ . Stream.unfold Handle.chunkReaderWith
103103 . f1
104104 in executor f2
105105
@@ -129,7 +129,7 @@ testWrite hfold =
129129 _ <- Stream.fold (hfold h) $ Stream.fromList list
130130 hFlush h
131131 hSeek h AbsoluteSeek 0
132− ls <- toList $ Stream.unfold Handle.read h
132+ ls <- toList $ Stream.unfold Handle.reader h
133133 hClose h
134134 return (ls == list)
135135
@@ -151,10 +151,10 @@ testWriteWithChunk =
151151 hw <- openFile fpathWrite ReadWriteMode
152152 hSeek hw AbsoluteSeek 0
153153 _ <- Stream.fold (Handle.writeChunks hw)
154− $ Stream.unfold Handle.readChunksWith (1024, hr)
154+ $ Stream.unfold Handle.chunkReaderWith (1024, hr)
155155 hFlush hw
156156 hSeek hw AbsoluteSeek 0
157− ls <- toList $ Stream.unfold Handle.read hw
157+ ls <- toList $ Stream.unfold Handle.reader hw
158158 let arr = Array.fromList ls
159159 return (testDataLarge == utf8ToString arr)
160160
@@ -171,7 +171,7 @@ testReadChunksFromToWith from to buffSize res = monadicIO $ run go
171171 ls <-
172172 toList
173173 $ Stream.unfold
174− Handle.readChunksFromToWith (from, to, buffSize, h)
174+ Handle.chunkReaderFromToWith (from, to, buffSize, h)
175175 return (res `shouldBe` fmap Array.toList ls)
176176
177177-- Test for first byte