on master help sign in
Rename some filesystem APIs
af68c08Ranjeet Kumar Ranjan
benchmark/NanoBenchmarks.hs+1−1
@@ -114,7 +114,7 @@ main = do
114 defaultMain [mkBenchText "splitOn abc...xyz" inText $ do
114 defaultMain [mkBenchText "splitOn abc...xyz" inText $ do
115 (S.length $ Internal.splitOnSeq (A.fromList $ map (fromIntegral . ord)
115 (S.length $ Internal.splitOnSeq (A.fromList $ map (fromIntegral . ord)
116 "abcdefghijklmnopqrstuvwxyz") FL.drain
116 "abcdefghijklmnopqrstuvwxyz") FL.drain
117− $ IFH.toBytes inText) >>= print
117+ $ IFH.getBytes inText) >>= print
118 ]
118 ]
119 where
119 where
120
120
benchmark/Streamly/Benchmark/Data/Array/Stream/Foreign.hs+8−8
@@ -81,7 +81,7 @@ benchIO name src sink =
81-- | Get the last byte from a file bytestream.
81-- | Get the last byte from a file bytestream.
82toChunksLast :: Handle -> IO (Maybe Word8)
82toChunksLast :: Handle -> IO (Maybe Word8)
83toChunksLast inh = do
83toChunksLast inh = do
84− let s = Handle.toChunks inh
84+ let s = Handle.getChunks inh
85 larr <- Stream.last s
85 larr <- Stream.last s
86 return $ case larr of
86 return $ case larr of
87 Nothing -> Nothing
87 Nothing -> Nothing
@@ -95,7 +95,7 @@ inspect $ 'toChunksLast `hasNoType` ''Step
95-- | Count the number of bytes in a file.
95-- | Count the number of bytes in a file.
96toChunksSumLengths :: Handle -> IO Int
96toChunksSumLengths :: Handle -> IO Int
97toChunksSumLengths inh =
97toChunksSumLengths inh =
98− let s = Handle.toChunks inh
98+ let s = Handle.getChunks inh
99 in Stream.sum (Stream.map Array.length s)
99 in Stream.sum (Stream.map Array.length s)
100
100
101#ifdef INSPECTION
101#ifdef INSPECTION
@@ -107,7 +107,7 @@ inspect $ 'toChunksSumLengths `hasNoType` ''Step
107toChunksCountBytes :: Handle -> IO Word8
107toChunksCountBytes :: Handle -> IO Word8
108toChunksCountBytes inh = do
108toChunksCountBytes inh = do
109 let foldlArr' f z = runIdentity . Stream.foldl' f z . Array.toStream
109 let foldlArr' f z = runIdentity . Stream.foldl' f z . Array.toStream
110− let s = Handle.toChunks inh
110+ let s = Handle.getChunks inh
111 Stream.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
111 Stream.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
112
112
113#ifdef INSPECTION
113#ifdef INSPECTION
@@ -117,7 +117,7 @@ inspect $ 'toChunksCountBytes `hasNoType` ''Step
117
117
118toChunksDecodeUtf8Arrays :: Handle -> IO ()
118toChunksDecodeUtf8Arrays :: Handle -> IO ()
119toChunksDecodeUtf8Arrays =
119toChunksDecodeUtf8Arrays =
120− Stream.drain . Unicode.decodeUtf8Arrays . Handle.toChunks
120+ Stream.drain . Unicode.decodeUtf8Arrays . Handle.getChunks
121
121
122#ifdef INSPECTION
122#ifdef INSPECTION
123inspect $ hasNoTypeClasses 'toChunksDecodeUtf8Arrays
123inspect $ hasNoTypeClasses 'toChunksDecodeUtf8Arrays
@@ -131,7 +131,7 @@ inspect $ hasNoTypeClasses 'toChunksDecodeUtf8Arrays
131-- | Count the number of lines in a file.
131-- | Count the number of lines in a file.
132toChunksSplitOnSuffix :: Handle -> IO Int
132toChunksSplitOnSuffix :: Handle -> IO Int
133toChunksSplitOnSuffix =
133toChunksSplitOnSuffix =
134− Stream.length . ArrayStream.splitOnSuffix 10 . Handle.toChunks
134+ Stream.length . ArrayStream.splitOnSuffix 10 . Handle.getChunks
135
135
136#ifdef INSPECTION
136#ifdef INSPECTION
137inspect $ hasNoTypeClasses 'toChunksSplitOnSuffix
137inspect $ hasNoTypeClasses 'toChunksSplitOnSuffix
@@ -141,7 +141,7 @@ inspect $ 'toChunksSplitOnSuffix `hasNoType` ''Step
141-- XXX use a word splitting combinator instead of splitOn and test it.
141-- XXX use a word splitting combinator instead of splitOn and test it.
142-- | Count the number of words in a file.
142-- | Count the number of words in a file.
143toChunksSplitOn :: Handle -> IO Int
143toChunksSplitOn :: Handle -> IO Int
144−toChunksSplitOn = Stream.length . ArrayStream.splitOn 32 . Handle.toChunks
144+toChunksSplitOn = Stream.length . ArrayStream.splitOn 32 . Handle.getChunks
145
145
146#ifdef INSPECTION
146#ifdef INSPECTION
147inspect $ hasNoTypeClasses 'toChunksSplitOn
147inspect $ hasNoTypeClasses 'toChunksSplitOn
@@ -181,7 +181,7 @@ copyChunksSplitInterposeSuffix inh outh =
181 Stream.fold (Handle.write outh)
181 Stream.fold (Handle.write outh)
182 $ ArrayStream.interposeSuffix 10
182 $ ArrayStream.interposeSuffix 10
183 $ ArrayStream.splitOnSuffix 10
183 $ ArrayStream.splitOnSuffix 10
184− $ Handle.toChunks inh
184+ $ Handle.getChunks inh
185
185
186#ifdef INSPECTION
186#ifdef INSPECTION
187inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterposeSuffix [''Storable]
187inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterposeSuffix [''Storable]
@@ -196,7 +196,7 @@ copyChunksSplitInterpose inh outh =
196 $ ArrayStream.interpose 32
196 $ ArrayStream.interpose 32
197 -- XXX this is not correct word splitting combinator
197 -- XXX this is not correct word splitting combinator
198 $ ArrayStream.splitOn 32
198 $ ArrayStream.splitOn 32
199− $ Handle.toChunks inh
199+ $ Handle.getChunks inh
200
200
201#ifdef INSPECTION
201#ifdef INSPECTION
202inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterpose [''Storable]
202inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterpose [''Storable]
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+7−7
@@ -183,25 +183,25 @@ o_1_space_reduce_read env =
183-------------------------------------------------------------------------------
183-------------------------------------------------------------------------------
184
184
185-- | Count the number of lines in a file.
185-- | Count the number of lines in a file.
186−toChunksConcatUnfoldCountLines :: Handle -> IO Int
186+getChunksConcatUnfoldCountLines :: Handle -> IO Int
187−toChunksConcatUnfoldCountLines inh =
187+getChunksConcatUnfoldCountLines inh =
188 S.length
188 S.length
189 $ IUS.lines FL.drain
189 $ IUS.lines FL.drain
190 $ SS.decodeLatin1
190 $ SS.decodeLatin1
191 -- XXX replace with toBytes
191 -- XXX replace with toBytes
192− $ S.unfoldMany A.read (IFH.toChunks inh)
192+ $ S.unfoldMany A.read (IFH.getChunks inh)
193
193
194#ifdef INSPECTION
194#ifdef INSPECTION
195−inspect $ hasNoTypeClasses 'toChunksConcatUnfoldCountLines
195+inspect $ hasNoTypeClasses 'getChunksConcatUnfoldCountLines
196−inspect $ 'toChunksConcatUnfoldCountLines `hasNoType` ''Step
196+inspect $ 'getChunksConcatUnfoldCountLines `hasNoType` ''Step
197−inspect $ 'toChunksConcatUnfoldCountLines `hasNoType` ''D.ConcatMapUState
197+inspect $ 'getChunksConcatUnfoldCountLines `hasNoType` ''D.ConcatMapUState
198#endif
198#endif
199
199
200o_1_space_reduce_toBytes :: BenchEnv -> [Benchmark]
200o_1_space_reduce_toBytes :: BenchEnv -> [Benchmark]
201o_1_space_reduce_toBytes env =
201o_1_space_reduce_toBytes env =
202 [ bgroup "reduce/toBytes"
202 [ bgroup "reduce/toBytes"
203 [ mkBench "US.lines . SS.decodeLatin1" env $ \inh _ ->
203 [ mkBench "US.lines . SS.decodeLatin1" env $ \inh _ ->
204− toChunksConcatUnfoldCountLines inh
204+ getChunksConcatUnfoldCountLines inh
205 ]
205 ]
206 ]
206 ]
207
207
benchmark/Streamly/Benchmark/FileSystem/Handle/ReadWrite.hs+27−28
@@ -53,7 +53,7 @@ import Test.Inspection
53
53
54-- | Copy file
54-- | Copy file
55copyChunks :: Handle -> Handle -> IO ()
55copyChunks :: Handle -> Handle -> IO ()
56−copyChunks inh outh = S.fold (IFH.writeChunks outh) $ IFH.toChunks inh
56+copyChunks inh outh = S.fold (IFH.writeChunks outh) $ IFH.getChunks inh
57
57
58#ifdef INSPECTION
58#ifdef INSPECTION
59inspect $ hasNoTypeClasses 'copyChunks
59inspect $ hasNoTypeClasses 'copyChunks
@@ -62,7 +62,7 @@ inspect $ 'copyChunks `hasNoType` ''Step
62
62
63o_1_space_copy_chunked :: BenchEnv -> [Benchmark]
63o_1_space_copy_chunked :: BenchEnv -> [Benchmark]
64o_1_space_copy_chunked env =
64o_1_space_copy_chunked env =
65− [ bgroup "copy/toChunks"
65+ [ bgroup "copy/getChunks"
66 [ mkBench "toNull" env $ \inH _ ->
66 [ mkBench "toNull" env $ \inH _ ->
67 copyChunks inH (nullH env)
67 copyChunks inH (nullH env)
68 , mkBench "raw" env $ \inH outH ->
68 , mkBench "raw" env $ \inH outH ->
@@ -114,17 +114,17 @@ inspect $ 'readFromBytesNull `hasNoType` ''D.FoldMany
114#endif
114#endif
115
115
116-- | Send the file contents ('defaultChunkSize') to /dev/null
116-- | Send the file contents ('defaultChunkSize') to /dev/null
117−readWithBufferOfFromBytesNull :: Handle -> Handle -> IO ()
117+readWithFromBytesNull :: Handle -> Handle -> IO ()
118−readWithBufferOfFromBytesNull inh devNull =
118+readWithFromBytesNull inh devNull =
119 IFH.putBytes devNull
119 IFH.putBytes devNull
120− $ S.unfold FH.readWithBufferOf (defaultChunkSize, inh)
120+ $ S.unfold FH.readWith (defaultChunkSize, inh)
121
121
122#ifdef INSPECTION
122#ifdef INSPECTION
123−inspect $ hasNoTypeClasses 'readWithBufferOfFromBytesNull
123+inspect $ hasNoTypeClasses 'readWithFromBytesNull
124−inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''Step
124+inspect $ 'readWithFromBytesNull `hasNoType` ''Step
125−inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''MAS.SpliceState
125+inspect $ 'readWithFromBytesNull `hasNoType` ''MAS.SpliceState
126−inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.arraysOf
126+inspect $ 'readWithFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.arraysOf
127−inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''D.FoldMany
127+inspect $ 'readWithFromBytesNull `hasNoType` ''D.FoldMany
128#endif
128#endif
129
129
130-- | Send the chunk content ('defaultChunkSize') to /dev/null
130-- | Send the chunk content ('defaultChunkSize') to /dev/null
@@ -138,41 +138,40 @@ _readChunks inh devNull = IUF.fold fld unf inh
138 unf = IUF.many A.read FH.readChunks
138 unf = IUF.many A.read FH.readChunks
139
139
140-- | Send the chunk content to /dev/null
140-- | Send the chunk content to /dev/null
141−-- Implicitly benchmarked via 'readWithBufferOfFromBytesNull'
141+-- Implicitly benchmarked via 'readWithFromBytesNull'
142−_readChunksWithBufferOf :: Handle -> Handle -> IO ()
142+_readChunksWith :: Handle -> Handle -> IO ()
143−_readChunksWithBufferOf inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
143+_readChunksWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
144
144
145 where
145 where
146
146
147 fld = FH.write devNull
147 fld = FH.write devNull
148− unf = IUF.many A.read FH.readChunksWithBufferOf
148+ unf = IUF.many A.read FH.readChunksWith
149−
150
149
151o_1_space_copy_fromBytes :: BenchEnv -> [Benchmark]
150o_1_space_copy_fromBytes :: BenchEnv -> [Benchmark]
152o_1_space_copy_fromBytes env =
151o_1_space_copy_fromBytes env =
153 [ bgroup "copy/putBytes"
152 [ bgroup "copy/putBytes"
154 [ mkBench "rawToNull" env $ \inh _ ->
153 [ mkBench "rawToNull" env $ \inh _ ->
155 readFromBytesNull inh (nullH env)
154 readFromBytesNull inh (nullH env)
156− , mkBench "FH.readWithBufferOf" env $ \inh _ ->
155+ , mkBench "FH.readWith" env $ \inh _ ->
157− readWithBufferOfFromBytesNull inh (nullH env)
156+ readWithFromBytesNull inh (nullH env)
158 ]
157 ]
159 ]
158 ]
160
159
161-- | Send the file contents ('defaultChunkSize') to /dev/null
160-- | Send the file contents ('defaultChunkSize') to /dev/null
162−writeReadWithBufferOf :: Handle -> Handle -> IO ()
161+writeReadWith :: Handle -> Handle -> IO ()
163−writeReadWithBufferOf inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
162+writeReadWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
164
163
165 where
164 where
166
165
167− fld = FH.writeWithBufferOf defaultChunkSize devNull
166+ fld = FH.writeWith defaultChunkSize devNull
168− unf = FH.readWithBufferOf
167+ unf = FH.readWith
169
168
170#ifdef INSPECTION
169#ifdef INSPECTION
171−inspect $ hasNoTypeClasses 'writeReadWithBufferOf
170+inspect $ hasNoTypeClasses 'writeReadWith
172−inspect $ 'writeReadWithBufferOf `hasNoType` ''Step
171+inspect $ 'writeReadWith `hasNoType` ''Step
173−inspect $ 'writeReadWithBufferOf `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
172+inspect $ 'writeReadWith `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
174−inspect $ 'writeReadWithBufferOf `hasNoType` ''MA.ReadUState -- FH.read/A.read
173+inspect $ 'writeReadWith `hasNoType` ''MA.ReadUState -- FH.read/A.read
175−inspect $ 'writeReadWithBufferOf `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
174+inspect $ 'writeReadWith `hasNoType` ''AT.ArrayUnsafe -- FH.write/writeNUnsafe
176#endif
175#endif
177
176
178-- | Send the file contents ('AT.defaultChunkSize') to /dev/null
177-- | Send the file contents ('AT.defaultChunkSize') to /dev/null
@@ -197,8 +196,8 @@ o_1_space_copy env =
197 [ bgroup "copy"
196 [ bgroup "copy"
198 [ mkBench "FH.write . FH.read" env $ \inh _ ->
197 [ mkBench "FH.write . FH.read" env $ \inh _ ->
199 writeRead inh (nullH env)
198 writeRead inh (nullH env)
200− , mkBench "FH.writeWithBufferOf . FH.readWithBufferOf" env $ \inh _ ->
199+ , mkBench "FH.writeWith . FH.readWith" env $ \inh _ ->
201− writeReadWithBufferOf inh (nullH env)
200+ writeReadWith inh (nullH env)
202 ]
201 ]
203 ]
202 ]
204
203
benchmark/Streamly/Benchmark/Prelude/Serial/Exceptions.hs+4−4
@@ -160,7 +160,7 @@ readWriteFinallyStream inh devNull =
160fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
160fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
161fromToBytesBracket_Stream inh devNull =
161fromToBytesBracket_Stream inh devNull =
162 let readEx = IP.bracket_ (return ()) (\_ -> hClose inh)
162 let readEx = IP.bracket_ (return ()) (\_ -> hClose inh)
163− (\_ -> IFH.toBytes inh)
163+ (\_ -> IFH.getBytes inh)
164 in IFH.putBytes devNull $ readEx
164 in IFH.putBytes devNull $ readEx
165
165
166#ifdef INSPECTION
166#ifdef INSPECTION
@@ -170,7 +170,7 @@ inspect $ hasNoTypeClasses 'fromToBytesBracket_Stream
170fromToBytesBracketStream :: Handle -> Handle -> IO ()
170fromToBytesBracketStream :: Handle -> Handle -> IO ()
171fromToBytesBracketStream inh devNull =
171fromToBytesBracketStream inh devNull =
172 let readEx = S.bracket (return ()) (\_ -> hClose inh)
172 let readEx = S.bracket (return ()) (\_ -> hClose inh)
173− (\_ -> IFH.toBytes inh)
173+ (\_ -> IFH.getBytes inh)
174 in IFH.putBytes devNull $ readEx
174 in IFH.putBytes devNull $ readEx
175
175
176readWriteBeforeAfterStream :: Handle -> Handle -> IO ()
176readWriteBeforeAfterStream :: Handle -> Handle -> IO ()
@@ -280,7 +280,7 @@ toChunksBracket_ inh devNull =
280 let readEx = IP.bracket_
280 let readEx = IP.bracket_
281 (return ())
281 (return ())
282 (\_ -> hClose inh)
282 (\_ -> hClose inh)
283− (\_ -> IFH.toChunks inh)
283+ (\_ -> IFH.getChunks inh)
284 in S.fold (IFH.writeChunks devNull) $ readEx
284 in S.fold (IFH.writeChunks devNull) $ readEx
285
285
286#ifdef INSPECTION
286#ifdef INSPECTION
@@ -292,7 +292,7 @@ toChunksBracket inh devNull =
292 let readEx = S.bracket
292 let readEx = S.bracket
293 (return ())
293 (return ())
294 (\_ -> hClose inh)
294 (\_ -> hClose inh)
295− (\_ -> IFH.toChunks inh)
295+ (\_ -> IFH.getChunks inh)
296 in S.fold (IFH.writeChunks devNull) $ readEx
296 in S.fold (IFH.writeChunks devNull) $ readEx
297
297
298o_1_space_copy_exceptions_toChunks :: BenchEnv -> [Benchmark]
298o_1_space_copy_exceptions_toChunks :: BenchEnv -> [Benchmark]
benchmark/Streamly/Benchmark/Prelude/Serial/Split.hs+1−1
@@ -209,7 +209,7 @@ splitOnSeqUtf8 :: String -> Handle -> IO Int
209splitOnSeqUtf8 str inh =
209splitOnSeqUtf8 str inh =
210 (S.length $ IP.splitOnSeq (A.fromList str) FL.drain
210 (S.length $ IP.splitOnSeq (A.fromList str) FL.drain
211 $ IUS.decodeUtf8Arrays
211 $ IUS.decodeUtf8Arrays
212− $ IFH.toChunks inh) -- >>= print
212+ $ IFH.getChunks inh) -- >>= print
213
213
214o_1_space_reduce_toChunks_split :: BenchEnv -> [Benchmark]
214o_1_space_reduce_toChunks_split :: BenchEnv -> [Benchmark]
215o_1_space_reduce_toChunks_split env =
215o_1_space_reduce_toChunks_split env =
benchmark/Streamly/Benchmark/Unicode/Stream.hs+1−1
@@ -55,7 +55,7 @@ copyCodecUtf8ArraysLenient inh outh =
55 Stream.fold (Handle.write outh)
55 Stream.fold (Handle.write outh)
56 $ Unicode.encodeUtf8'
56 $ Unicode.encodeUtf8'
57 $ Unicode.decodeUtf8Arrays
57 $ Unicode.decodeUtf8Arrays
58− $ Handle.toChunks inh
58+ $ Handle.getChunks inh
59
59
60#ifdef INSPECTION
60#ifdef INSPECTION
61inspect $ hasNoTypeClasses 'copyCodecUtf8ArraysLenient
61inspect $ hasNoTypeClasses 'copyCodecUtf8ArraysLenient
src/Streamly/FileSystem/Handle.hs+9−3
@@ -76,9 +76,9 @@ module Streamly.FileSystem.Handle
76 -- encountered.
76 -- encountered.
77
77
78 , read
78 , read
79− , readWithBufferOf
79+ , readWith
80 , readChunks
80 , readChunks
81− , readChunksWithBufferOf
81+ , readChunksWith
82
82
83 -- ** Writing
83 -- ** Writing
84 -- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
84 -- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
@@ -87,8 +87,14 @@ module Streamly.FileSystem.Handle
87 -- of the handle.
87 -- of the handle.
88
88
89 , write
89 , write
90− , writeWithBufferOf
90+ , writeWith
91 , writeChunks
91 , writeChunks
92+
93+ -- * Deprecated
94+ , readChunksWithBufferOf
95+ , readWithBufferOf
96+ , writeChunksWithBufferOf
97+ , writeWithBufferOf
92 )
98 )
93where
99where
94
100
src/Streamly/Internal/Console/Stdio.hs+4−4
@@ -66,14 +66,14 @@ read = Unfold.lmap (\() -> stdin) Handle.read
66
66
67-- | Read a byte stream from standard input.
67-- | Read a byte stream from standard input.
68--
68--
69−-- > getBytes = Handle.toBytes stdin
69+-- > getBytes = Handle.getBytes stdin
70-- > getBytes = Stream.unfold Stdio.read ()
70-- > getBytes = Stream.unfold Stdio.read ()
71--
71--
72-- /Pre-release/
72-- /Pre-release/
73--
73--
74{-# INLINE getBytes #-}
74{-# INLINE getBytes #-}
75getBytes :: MonadIO m => SerialT m Word8
75getBytes :: MonadIO m => SerialT m Word8
76−getBytes = Handle.toBytes stdin
76+getBytes = Handle.getBytes stdin
77
77
78-- | Read a character stream from Utf8 encoded standard input.
78-- | Read a character stream from Utf8 encoded standard input.
79--
79--
@@ -96,14 +96,14 @@ readChunks = Unfold.lmap (\() -> stdin) Handle.readChunks
96-- chunk is limited to @defaultChunkSize@. The actual size read may be less
96-- chunk is limited to @defaultChunkSize@. The actual size read may be less
97-- than @defaultChunkSize@.
97-- than @defaultChunkSize@.
98--
98--
99−-- > getChunks = Handle.toChunks stdin
99+-- > getChunks = Handle.getChunks stdin
100-- > getChunks = Stream.unfold Stdio.readChunks ()
100-- > getChunks = Stream.unfold Stdio.readChunks ()
101--
101--
102-- /Pre-release/
102-- /Pre-release/
103--
103--
104{-# INLINE getChunks #-}
104{-# INLINE getChunks #-}
105getChunks :: MonadIO m => SerialT m (Array Word8)
105getChunks :: MonadIO m => SerialT m (Array Word8)
106−getChunks = Handle.toChunks stdin
106+getChunks = Handle.getChunks stdin
107
107
108{-
108{-
109-- | Read UTF8 encoded lines from standard input.
109-- | Read UTF8 encoded lines from standard input.
src/Streamly/Internal/FileSystem/File.hs+39−37
@@ -44,7 +44,7 @@ module Streamly.Internal.FileSystem.File
44 withFile
44 withFile
45
45
46 -- ** Read From File
46 -- ** Read From File
47− , readWithBufferOf
47+ , readWith
48 , read
48 , read
49 -- , readShared
49 -- , readShared
50 -- , readUtf8
50 -- , readUtf8
@@ -54,11 +54,11 @@ module Streamly.Internal.FileSystem.File
54 , toBytes
54 , toBytes
55
55
56 -- -- * Array Read
56 -- -- * Array Read
57− , readChunksWithBufferOf
57+ , readChunksWith
58 , readChunksFromToWith
58 , readChunksFromToWith
59 , readChunks
59 , readChunks
60
60
61− , toChunksWithBufferOf
61+ , toChunksWith
62 , toChunks
62 , toChunks
63
63
64 -- ** Write To File
64 -- ** Write To File
@@ -66,10 +66,10 @@ module Streamly.Internal.FileSystem.File
66 -- , writeUtf8
66 -- , writeUtf8
67 -- , writeUtf8ByLines
67 -- , writeUtf8ByLines
68 -- , writeByFrames
68 -- , writeByFrames
69− , writeWithBufferOf
69+ , writeWith
70
70
71 , fromBytes
71 , fromBytes
72− , fromBytesWithBufferOf
72+ , fromBytesWith
73
73
74 -- -- * Array Write
74 -- -- * Array Write
75 , putChunk
75 , putChunk
@@ -78,7 +78,7 @@ module Streamly.Internal.FileSystem.File
78
78
79 -- ** Append To File
79 -- ** Append To File
80 , append
80 , append
81− , appendWithBufferOf
81+ , appendWith
82 -- , appendShared
82 -- , appendShared
83 , appendArray
83 , appendArray
84 , appendChunks
84 , appendChunks
@@ -211,14 +211,16 @@ appendArray file arr = SIO.withFile file AppendMode (`FH.putChunk` arr)
211-- Stream of Arrays IO
211-- Stream of Arrays IO
212-------------------------------------------------------------------------------
212-------------------------------------------------------------------------------
213
213
214−-- | @toChunksWithBufferOf size file@ reads a stream of arrays from file @file@.
214+-- | @toChunksWith size file@ reads a stream of arrays from file @file@.
215-- The maximum size of a single array is specified by @size@. The actual size
215-- The maximum size of a single array is specified by @size@. The actual size
216-- read may be less than or equal to @size@.
216-- read may be less than or equal to @size@.
217−{-# INLINE toChunksWithBufferOf #-}
217+--
218+-- @since 0.9.0
219+{-# INLINE toChunksWith #-}
218−toChunksWithBufferOf :: (IsStream t, MonadCatch m, MonadAsync m)
220+toChunksWith :: (IsStream t, MonadCatch m, MonadAsync m)
219 => Int -> FilePath -> t m (Array Word8)
221 => Int -> FilePath -> t m (Array Word8)
220−toChunksWithBufferOf size file =
222+toChunksWith size file =
221− withFile file ReadMode (FH.toChunksWithBufferOf size)
223+ withFile file ReadMode (FH.getChunksWith size)
222
224
223-- XXX read 'Array a' instead of Word8
225-- XXX read 'Array a' instead of Word8
224--
226--
@@ -226,13 +228,13 @@ toChunksWithBufferOf size file =
226-- The maximum size of a single array is limited to @defaultChunkSize@. The
228-- The maximum size of a single array is limited to @defaultChunkSize@. The
227-- actual size read may be less than @defaultChunkSize@.
229-- actual size read may be less than @defaultChunkSize@.
228--
230--
229−-- > toChunks = toChunksWithBufferOf defaultChunkSize
231+-- > toChunks = toChunksWith defaultChunkSize
230--
232--
231-- @since 0.7.0
233-- @since 0.7.0
232{-# INLINE toChunks #-}
234{-# INLINE toChunks #-}
233toChunks :: (IsStream t, MonadCatch m, MonadAsync m)
235toChunks :: (IsStream t, MonadCatch m, MonadAsync m)
234 => FilePath -> t m (Array Word8)
236 => FilePath -> t m (Array Word8)
235−toChunks = toChunksWithBufferOf defaultChunkSize
237+toChunks = toChunksWith defaultChunkSize
236
238
237-------------------------------------------------------------------------------
239-------------------------------------------------------------------------------
238-- Read File to Stream
240-- Read File to Stream
@@ -249,10 +251,10 @@ toChunks = toChunksWithBufferOf defaultChunkSize
249--
251--
250-- /Pre-release/
252-- /Pre-release/
251--
253--
252−{-# INLINE readChunksWithBufferOf #-}
254+{-# INLINE readChunksWith #-}
253−readChunksWithBufferOf :: (MonadCatch m, MonadAsync m)
255+readChunksWith :: (MonadCatch m, MonadAsync m)
254 => Unfold m (Int, FilePath) (Array Word8)
256 => Unfold m (Int, FilePath) (Array Word8)
255−readChunksWithBufferOf = usingFile2 FH.readChunksWithBufferOf
257+readChunksWith = usingFile2 FH.readChunksWith
256
258
257-- | Unfold the tuple @(from, to, bufsize, filepath)@ into a stream
259-- | Unfold the tuple @(from, to, bufsize, filepath)@ into a stream
258-- of 'Word8' arrays.
260-- of 'Word8' arrays.
@@ -282,9 +284,9 @@ readChunks = usingFile FH.readChunks
282-- to the IO device are performed using buffers of @bufsize@.
284-- to the IO device are performed using buffers of @bufsize@.
283--
285--
284-- /Pre-release/
286-- /Pre-release/
285−{-# INLINE readWithBufferOf #-}
287+{-# INLINE readWith #-}
286−readWithBufferOf :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) Word8
288+readWith :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) Word8
287−readWithBufferOf = usingFile2 FH.readWithBufferOf
289+readWith = usingFile2 FH.readWith
288
290
289-- | Unfolds a file path into a byte stream. IO requests to the device are
291-- | Unfolds a file path into a byte stream. IO requests to the device are
290-- performed in sizes of
292-- performed in sizes of
@@ -303,7 +305,7 @@ read = UF.many A.read (usingFile FH.readChunks)
303--
305--
304{-# INLINE toBytes #-}
306{-# INLINE toBytes #-}
305toBytes :: (IsStream t, MonadCatch m, MonadAsync m) => FilePath -> t m Word8
307toBytes :: (IsStream t, MonadCatch m, MonadAsync m) => FilePath -> t m Word8
306−toBytes file = AS.concat $ withFile file ReadMode FH.toChunks
308+toBytes file = AS.concat $ withFile file ReadMode FH.getChunks
307
309
308{-
310{-
309-- | Generate a stream of elements of the given type from a file 'Handle'. The
311-- | Generate a stream of elements of the given type from a file 'Handle'. The
@@ -346,13 +348,13 @@ fromChunks = fromChunksMode WriteMode
346-- be written to the IO device as soon as we collect the specified number of
348-- be written to the IO device as soon as we collect the specified number of
347-- input elements.
349-- input elements.
348--
350--
349−-- @since 0.7.0
351+-- @since 0.9.0
350−{-# INLINE fromBytesWithBufferOf #-}
352+{-# INLINE fromBytesWith #-}
351−fromBytesWithBufferOf :: (MonadAsync m, MonadCatch m)
353+fromBytesWith :: (MonadAsync m, MonadCatch m)
352 => Int -> FilePath -> SerialT m Word8 -> m ()
354 => Int -> FilePath -> SerialT m Word8 -> m ()
353−fromBytesWithBufferOf n file xs = fromChunks file $ AS.arraysOf n xs
355+fromBytesWith n file xs = fromChunks file $ AS.arraysOf n xs
354
356
355−-- > write = 'writeWithBufferOf' defaultChunkSize
357+-- > write = 'writeWith' defaultChunkSize
356--
358--
357-- | Write a byte stream to a file. Combines the bytes in chunks of size
359-- | Write a byte stream to a file. Combines the bytes in chunks of size
358-- up to 'A.defaultChunkSize' before writing. If the file exists it is
360-- up to 'A.defaultChunkSize' before writing. If the file exists it is
@@ -362,7 +364,7 @@ fromBytesWithBufferOf n file xs = fromChunks file $ AS.arraysOf n xs
362-- /Pre-release/
364-- /Pre-release/
363{-# INLINE fromBytes #-}
365{-# INLINE fromBytes #-}
364fromBytes :: (MonadAsync m, MonadCatch m) => FilePath -> SerialT m Word8 -> m ()
366fromBytes :: (MonadAsync m, MonadCatch m) => FilePath -> SerialT m Word8 -> m ()
365−fromBytes = fromBytesWithBufferOf defaultChunkSize
367+fromBytes = fromBytesWith defaultChunkSize
366
368
367{-
369{-
368{-# INLINE write #-}
370{-# INLINE write #-}
@@ -394,18 +396,18 @@ writeChunks path = Fold step initial extract
394 FL.Partial fs -> extract1 fs
396 FL.Partial fs -> extract1 fs
395 FL.Done fb -> return fb
397 FL.Done fb -> return fb
396
398
397−-- | @writeWithBufferOf chunkSize handle@ writes the input stream to @handle@.
399+-- | @writeWith chunkSize handle@ writes the input stream to @handle@.
398-- Bytes in the input stream are collected into a buffer until we have a chunk
400-- Bytes in the input stream are collected into a buffer until we have a chunk
399-- of size @chunkSize@ and then written to the IO device.
401-- of size @chunkSize@ and then written to the IO device.
400--
402--
401-- /Pre-release/
403-- /Pre-release/
402−{-# INLINE writeWithBufferOf #-}
404+{-# INLINE writeWith #-}
403−writeWithBufferOf :: (MonadIO m, MonadCatch m)
405+writeWith :: (MonadIO m, MonadCatch m)
404 => Int -> FilePath -> Fold m Word8 ()
406 => Int -> FilePath -> Fold m Word8 ()
405−writeWithBufferOf n path =
407+writeWith n path =
406 FL.chunksOf n (writeNUnsafe n) (writeChunks path)
408 FL.chunksOf n (writeNUnsafe n) (writeChunks path)
407
409
408−-- > write = 'writeWithBufferOf' A.defaultChunkSize
410+-- > write = 'writeWith' A.defaultChunkSize
409--
411--
410-- | Write a byte stream to a file. Accumulates the input in chunks of up to
412-- | Write a byte stream to a file. Accumulates the input in chunks of up to
411-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing to
413-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing to
@@ -415,7 +417,7 @@ writeWithBufferOf n path =
415--
417--
416{-# INLINE write #-}
418{-# INLINE write #-}
417write :: (MonadIO m, MonadCatch m) => FilePath -> Fold m Word8 ()
419write :: (MonadIO m, MonadCatch m) => FilePath -> Fold m Word8 ()
418−write = writeWithBufferOf defaultChunkSize
420+write = writeWith defaultChunkSize
419
421
420-- | Append a stream of arrays to a file.
422-- | Append a stream of arrays to a file.
421--
423--
@@ -429,11 +431,11 @@ appendChunks = fromChunksMode AppendMode
429-- be written to the IO device as soon as we collect the specified number of
431-- be written to the IO device as soon as we collect the specified number of
430-- input elements.
432-- input elements.
431--
433--
432−-- @since 0.7.0
434+-- @since 0.9.0
433−{-# INLINE appendWithBufferOf #-}
435+{-# INLINE appendWith #-}
434−appendWithBufferOf :: (MonadAsync m, MonadCatch m)
436+appendWith :: (MonadAsync m, MonadCatch m)
435 => Int -> FilePath -> SerialT m Word8 -> m ()
437 => Int -> FilePath -> SerialT m Word8 -> m ()
436−appendWithBufferOf n file xs = appendChunks file $ AS.arraysOf n xs
438+appendWith n file xs = appendChunks file $ AS.arraysOf n xs
437
439
438-- | Append a byte stream to a file. Combines the bytes in chunks of size up to
440-- | Append a byte stream to a file. Combines the bytes in chunks of size up to
439-- 'A.defaultChunkSize' before writing. If the file exists then the new data
441-- 'A.defaultChunkSize' before writing. If the file exists then the new data
@@ -443,7 +445,7 @@ appendWithBufferOf n file xs = appendChunks file $ AS.arraysOf n xs
443-- @since 0.7.0
445-- @since 0.7.0
444{-# INLINE append #-}
446{-# INLINE append #-}
445append :: (MonadAsync m, MonadCatch m) => FilePath -> SerialT m Word8 -> m ()
447append :: (MonadAsync m, MonadCatch m) => FilePath -> SerialT m Word8 -> m ()
446−append = appendWithBufferOf defaultChunkSize
448+append = appendWith defaultChunkSize
447
449
448{-
450{-
449-- | Like 'append' but the file is not locked for exclusive writes.
451-- | Like 'append' but the file is not locked for exclusive writes.
src/Streamly/Internal/FileSystem/Handle.hs+117−93
@@ -11,13 +11,13 @@
11--
11--
12-- The fundamental singleton IO APIs are 'getChunk' and 'putChunk' and the
12-- The fundamental singleton IO APIs are 'getChunk' and 'putChunk' and the
13-- fundamental stream IO APIs built on top of those are
13-- fundamental stream IO APIs built on top of those are
14−-- 'readChunksWithBufferOf' and 'writeChunks'. Rest of this module is just
14+-- 'readChunksWith' and 'writeChunks'. Rest of this module is just
15-- combinatorial programming using these.
15-- combinatorial programming using these.
16--
16--
17-- We can achieve line buffering by folding lines in the input stream into a
17-- We can achieve line buffering by folding lines in the input stream into a
18-- stream of arrays using Stream.splitOn or Fold.takeEndBy_ and similar
18-- stream of arrays using Stream.splitOn or Fold.takeEndBy_ and similar
19-- operations. One can wrap the input stream in 'Maybe' type and then use
19-- operations. One can wrap the input stream in 'Maybe' type and then use
20−-- 'writeMaybesWithBufferOf' to achieve user controlled buffering.
20+-- 'writeMaybesWith' to achieve user controlled buffering.
21
21
22-- TODO: Need a separate module for pread/pwrite based reading writing for
22-- TODO: Need a separate module for pread/pwrite based reading writing for
23-- seekable devices. Stateless read/write can be helpful in multithreaded
23-- seekable devices. Stateless read/write can be helpful in multithreaded
@@ -35,17 +35,17 @@ module Streamly.Internal.FileSystem.Handle
35 -- , readUtf8
35 -- , readUtf8
36 -- , readLines
36 -- , readLines
37 -- , readFrames
37 -- , readFrames
38− , readWithBufferOf
38+ , readWith
39
39
40− , toBytes
40+ , getBytes
41− , toBytesWithBufferOf
41+ , getBytesWith
42
42
43 -- * Chunked Stream Read
43 -- * Chunked Stream Read
44 , readChunks
44 , readChunks
45− , readChunksWithBufferOf
45+ , readChunksWith
46
46
47− , toChunksWithBufferOf
47+ , getChunksWith
48− , toChunks
48+ , getChunks
49
49
50 -- * Byte Stream Write
50 -- * Byte Stream Write
51 -- Byte stream write (Folds)
51 -- Byte stream write (Folds)
@@ -55,17 +55,17 @@ module Streamly.Internal.FileSystem.Handle
55 -- , writeUtf8ByLines
55 -- , writeUtf8ByLines
56 -- , writeByFrames
56 -- , writeByFrames
57 -- , writeLines
57 -- , writeLines
58− , writeWithBufferOf
58+ , writeWith
59− , writeMaybesWithBufferOf
59+ , writeMaybesWith
60
60
61 , putBytes
61 , putBytes
62− , putBytesWithBufferOf
62+ , putBytesWith
63
63
64 -- * Chunked Stream Write
64 -- * Chunked Stream Write
65 , writeChunks
65 , writeChunks
66− , writeChunksWithBufferOf
66+ , writeChunksWith
67
67
68− , putChunksWithBufferOf
68+ , putChunksWith
69 , putChunks
69 , putChunks
70
70
71 -- * Random Access (Seek)
71 -- * Random Access (Seek)
@@ -101,6 +101,11 @@ module Streamly.Internal.FileSystem.Handle
101 -- , writeChunksFromTo
101 -- , writeChunksFromTo
102 -- , writeChunksFromToWith
102 -- , writeChunksFromToWith
103 -- , writeChunksFromThenToWith
103 -- , writeChunksFromThenToWith
104+ -- * Deprecated
105+ , readChunksWithBufferOf
106+ , readWithBufferOf
107+ , writeChunksWithBufferOf
108+ , writeWithBufferOf
104 )
109 )
105where
110where
106
111
@@ -203,13 +208,14 @@ getChunkOf = undefined
203-- Stream of Arrays IO
208-- Stream of Arrays IO
204-------------------------------------------------------------------------------
209-------------------------------------------------------------------------------
205
210
206−-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
211+-- | @getChunksWith size h@ reads a stream of arrays from file handle @h@.
207-- The maximum size of a single array is specified by @size@. The actual size
212-- The maximum size of a single array is specified by @size@. The actual size
208-- read may be less than or equal to @size@.
213-- read may be less than or equal to @size@.
209−{-# INLINE _toChunksWithBufferOf #-}
214+-- @since 0.9.0
215+{-# INLINE _getChunksWith #-}
210−_toChunksWithBufferOf :: (IsStream t, MonadIO m)
216+_getChunksWith :: (IsStream t, MonadIO m)
211 => Int -> Handle -> t m (Array Word8)
217 => Int -> Handle -> t m (Array Word8)
212−_toChunksWithBufferOf size h = go
218+_getChunksWith size h = go
213 where
219 where
214 -- XXX use cons/nil instead
220 -- XXX use cons/nil instead
215 go = mkStream $ \_ yld _ stp -> do
221 go = mkStream $ \_ yld _ stp -> do
@@ -218,22 +224,17 @@ _toChunksWithBufferOf size h = go
218 then stp
224 then stp
219 else yld arr go
225 else yld arr go
220
226
221−-- | @toChunksWithBufferOf size handle@ reads a stream of arrays from the file
227+-- | @getChunksWith size handle@ reads a stream of arrays from the file
222-- handle @handle@. The maximum size of a single array is limited to @size@.
228-- handle @handle@. The maximum size of a single array is limited to @size@.
223-- The actual size read may be less than or equal to @size@.
229-- The actual size read may be less than or equal to @size@.
224--
230--
225−-- >>> toChunksWithBufferOf size h = Stream.unfold Handle.readChunksWithBufferOf (size, h)
231+-- >>> getChunksWith size h = Stream.unfold Handle.readChunksWith (size, h)
226--
232--
227−-- @since 0.7.0
233+-- @since 0.9.0
228−{-# INLINE_NORMAL toChunksWithBufferOf #-}
234+{-# INLINE_NORMAL getChunksWith #-}
229−toChunksWithBufferOf :: (IsStream t, MonadIO m) =>
235+getChunksWith :: (IsStream t, MonadIO m) =>
230 Int -> Handle -> t m (Array Word8)
236 Int -> Handle -> t m (Array Word8)
231−{-
232−toChunksWithBufferOf size h =
233− S.repeatM (getChunk size h)
234− & S.takeWhile ((/= 0) . byteLength)
235−-}
236−toChunksWithBufferOf size h = fromStreamD (D.Stream step ())
237+getChunksWith size h = fromStreamD (D.Stream step ())
237 where
238 where
238 {-# INLINE_LATE step #-}
239 {-# INLINE_LATE step #-}
239 step _ _ = do
240 step _ _ = do
@@ -248,31 +249,27 @@ toChunksWithBufferOf size h = fromStreamD (D.Stream step ())
248-- @bufsize@. The size of an array in the resulting stream is always less than
249-- @bufsize@. The size of an array in the resulting stream is always less than
249-- or equal to @bufsize@.
250-- or equal to @bufsize@.
250--
251--
251−-- @since 0.7.0
252+-- @since 0.9.0
252−{-# INLINE_NORMAL readChunksWithBufferOf #-}
253+{-# INLINE_NORMAL readChunksWith #-}
253−readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
254+readChunksWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
254−readChunksWithBufferOf =
255+readChunksWith =
255 UF.lmap (uncurry getChunk) UF.repeatM
256 UF.lmap (uncurry getChunk) UF.repeatM
256 & UF.takeWhile ((/= 0) . byteLength)
257 & UF.takeWhile ((/= 0) . byteLength)
257−{-
258−readChunksWithBufferOf = Unfold step return
259
258
260− where
261
259
262− {-# INLINE_LATE step #-}
260+-- | Same as 'readChunksWith'
263− step (size, h) = do
261+--
264− arr <- getChunk size h
262+-- @since 0.7.0
265− return $
263+{-# DEPRECATED readChunksWithBufferOf "Please use readChunksWith instead." #-}
266− case byteLength arr of
264+{-# INLINE_NORMAL readChunksWithBufferOf #-}
267− 0 -> D.Stop
265+readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
268− _ -> D.Yield arr (size, h)
266+readChunksWithBufferOf = readChunksWith
269−-}
270
267
271-- There are two ways to implement this.
268-- There are two ways to implement this.
272--
269--
273−-- 1. Idiomatic: use a scan on the output of readChunksWithBufferOf to total
270+-- 1. Idiomatic: use a scan on the output of readChunksWith to total
274-- the array lengths and trim the last array to correct size.
271-- the array lengths and trim the last array to correct size.
275−-- 2. Simply implement it from scratch like readChunksWithBufferOf.
272+-- 2. Simply implement it from scratch like readChunksWith.
276--
273--
277-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
274-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
278--
275--
@@ -305,19 +302,20 @@ readChunksFromToWith = Unfold step inject
305 assert (len <= remaining)
302 assert (len <= remaining)
306 $ D.Yield arr (remaining - len, bufSize, h)
303 $ D.Yield arr (remaining - len, bufSize, h)
307
304
305+
308-- XXX read 'Array a' instead of Word8
306-- XXX read 'Array a' instead of Word8
309--
307--
310−-- | @toChunks handle@ reads a stream of arrays from the specified file
308+-- | @getChunks handle@ reads a stream of arrays from the specified file
311-- handle. The maximum size of a single array is limited to
309-- handle. The maximum size of a single array is limited to
312-- @defaultChunkSize@. The actual size read may be less than or equal to
310-- @defaultChunkSize@. The actual size read may be less than or equal to
313-- @defaultChunkSize@.
311-- @defaultChunkSize@.
314--
312--
315−-- >>> toChunks = Handle.toChunksWithBufferOf IO.defaultChunkSize
313+-- >>> getChunks = Handle.getChunksWith IO.defaultChunkSize
316--
314--
317−-- @since 0.7.0
315+-- @since 0.9.0
318−{-# INLINE toChunks #-}
316+{-# INLINE getChunks #-}
319−toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
317+getChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
320−toChunks = toChunksWithBufferOf defaultChunkSize
318+getChunks = getChunksWith defaultChunkSize
321
319
322-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
320-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
323-- device are performed using a buffer of size
321-- device are performed using a buffer of size
@@ -325,12 +323,12 @@ toChunks = toChunksWithBufferOf defaultChunkSize
325-- size of arrays in the resulting stream are therefore less than or equal to
323-- size of arrays in the resulting stream are therefore less than or equal to
326-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'.
324-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'.
327--
325--
328−-- >>> readChunks = Unfold.supplyFirst IO.defaultChunkSize Handle.readChunksWithBufferOf
326+-- >>> readChunks = Unfold.supplyFirst IO.defaultChunkSize Handle.readChunksWith
329--
327--
330-- @since 0.7.0
328-- @since 0.7.0
331{-# INLINE readChunks #-}
329{-# INLINE readChunks #-}
332readChunks :: MonadIO m => Unfold m Handle (Array Word8)
330readChunks :: MonadIO m => Unfold m Handle (Array Word8)
333−readChunks = UF.supplyFirst defaultChunkSize readChunksWithBufferOf
331+readChunks = UF.supplyFirst defaultChunkSize readChunksWith
334
332
335-------------------------------------------------------------------------------
333-------------------------------------------------------------------------------
336-- Read File to Stream
334-- Read File to Stream
@@ -343,22 +341,30 @@ readChunks = UF.supplyFirst defaultChunkSize readChunksWithBufferOf
343-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
341-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
344-- to the IO device are performed using buffers of @bufsize@.
342-- to the IO device are performed using buffers of @bufsize@.
345--
343--
346−-- >>> readWithBufferOf = Unfold.many Array.read Handle.readChunksWithBufferOf
344+-- >>> readWith = Unfold.many Array.read Handle.readChunksWith
345+--
346+-- @since 0.9.0
347+{-# INLINE readWith #-}
348+readWith :: MonadIO m => Unfold m (Int, Handle) Word8
349+readWith = UF.many A.read readChunksWith
350+
351+-- | Same as 'readWith'
347--
352--
348-- @since 0.7.0
353-- @since 0.7.0
354+{-# DEPRECATED readWithBufferOf "Please use readWith instead." #-}
349{-# INLINE readWithBufferOf #-}
355{-# INLINE readWithBufferOf #-}
350readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
356readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
351−readWithBufferOf = UF.many A.read readChunksWithBufferOf
357+readWithBufferOf = readWith
352
358
353−-- | @toBytesWithBufferOf bufsize handle@ reads a byte stream from a file
359+-- | @getBytesWith bufsize handle@ reads a byte stream from a file
354-- handle, reads are performed in chunks of up to @bufsize@.
360-- handle, reads are performed in chunks of up to @bufsize@.
355--
361--
356−-- >>> toBytesWithBufferOf size h = Stream.unfoldMany Array.read $ Handle.toChunksWithBufferOf size h
362+-- >>> getBytesWith size h = Stream.unfoldMany Array.read $ Handle.getChunksWith size h
357--
363--
358-- /Pre-release/
364-- /Pre-release/
359−{-# INLINE toBytesWithBufferOf #-}
365+{-# INLINE getBytesWith #-}
360−toBytesWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
366+getBytesWith :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
361−toBytesWithBufferOf size h = AS.concat $ toChunksWithBufferOf size h
367+getBytesWith size h = AS.concat $ getChunksWith size h
362
368
363-- TODO
369-- TODO
364-- Generate a stream of elements of the given type from a file 'Handle'.
370-- Generate a stream of elements of the given type from a file 'Handle'.
@@ -377,12 +383,12 @@ read = UF.many A.read readChunks
377
383
378-- | Generate a byte stream from a file 'Handle'.
384-- | Generate a byte stream from a file 'Handle'.
379--
385--
380−-- >>> toBytes h = Stream.unfoldMany Array.read $ Handle.toChunks h
386+-- >>> getBytes h = Stream.unfoldMany Array.read $ Handle.getChunks h
381--
387--
382-- /Pre-release/
388-- /Pre-release/
383−{-# INLINE toBytes #-}
389+{-# INLINE getBytes #-}
384−toBytes :: (IsStream t, MonadIO m) => Handle -> t m Word8
390+getBytes :: (IsStream t, MonadIO m) => Handle -> t m Word8
385−toBytes = AS.concat . toChunks
391+getBytes = AS.concat . getChunks
386
392
387-------------------------------------------------------------------------------
393-------------------------------------------------------------------------------
388-- Writing
394-- Writing
@@ -427,28 +433,29 @@ putChunks h = S.mapM_ (putChunk h)
427-- AS.concat is written in terms of foldMany. Once that is done we can write
433-- AS.concat is written in terms of foldMany. Once that is done we can write
428-- idiomatic def in the docs.
434-- idiomatic def in the docs.
429--
435--
430−-- | @putChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
436+-- | @putChunksWith bufsize handle stream@ writes a stream of arrays
431-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
437-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
432-- The chunk size is only a maximum and the actual writes could be smaller as
438-- The chunk size is only a maximum and the actual writes could be smaller as
433-- we do not split the arrays to fit exactly to the specified size.
439-- we do not split the arrays to fit exactly to the specified size.
434--
440--
435−-- @since 0.7.0
441+-- @since 0.9.0
436−{-# INLINE putChunksWithBufferOf #-}
442+{-# INLINE putChunksWith #-}
437−putChunksWithBufferOf :: (MonadIO m, Storable a)
443+putChunksWith :: (MonadIO m, Storable a)
438 => Int -> Handle -> SerialT m (Array a) -> m ()
444 => Int -> Handle -> SerialT m (Array a) -> m ()
439−putChunksWithBufferOf n h xs = putChunks h $ AS.compact n xs
445+putChunksWith n h xs = putChunks h $ AS.compact n xs
440
446
441−-- | @putBytesWithBufferOf bufsize handle stream@ writes @stream@ to @handle@
447+-- | @putBytesWith bufsize handle stream@ writes @stream@ to @handle@
442-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
448-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
443-- collect the required input size.
449-- collect the required input size.
444--
450--
445−-- >>> putBytesWithBufferOf n h m = Handle.putChunks h $ Stream.arraysOf n m
451+-- >>> putBytesWith n h m = Handle.putChunks h $ Stream.arraysOf n m
446--
452--
447−-- @since 0.7.0
453+-- @since 0.9.0
448−{-# INLINE putBytesWithBufferOf #-}
454+{-# INLINE putBytesWith #-}
449−putBytesWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
455+putBytesWith :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
450−putBytesWithBufferOf n h m = putChunks h $ S.arraysOf n m
456+putBytesWith n h m = putChunks h $ S.arraysOf n m
457+
451−-- putBytesWithBufferOf n h m = putChunks h $ AS.arraysOf n m
458+-- putBytesWith n h m = putChunks h $ AS.arraysOf n m
452
459
453-- | Write a byte stream to a file handle. Accumulates the input in chunks of
460-- | Write a byte stream to a file handle. Accumulates the input in chunks of
454-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing.
461-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing.
@@ -456,12 +463,12 @@ putBytesWithBufferOf n h m = putChunks h $ S.arraysOf n m
456-- NOTE: This may perform better than the 'write' fold, you can try this if you
463-- NOTE: This may perform better than the 'write' fold, you can try this if you
457-- need some extra perf boost.
464-- need some extra perf boost.
458--
465--
459−-- >>> putBytes = Handle.putBytesWithBufferOf IO.defaultChunkSize
466+-- >>> putBytes = Handle.putBytesWith IO.defaultChunkSize
460--
467--
461-- @since 0.7.0
468-- @since 0.7.0
462{-# INLINE putBytes #-}
469{-# INLINE putBytes #-}
463putBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
470putBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
464−putBytes = putBytesWithBufferOf defaultChunkSize
471+putBytes = putBytesWith defaultChunkSize
465
472
466-- | Write a stream of arrays to a handle. Each array in the stream is written
473-- | Write a stream of arrays to a handle. Each array in the stream is written
467-- to the device as a separate IO request.
474-- to the device as a separate IO request.
@@ -482,17 +489,26 @@ consumeChunks = Refold.drainBy putChunk
482
489
483-- XXX lpackArraysChunksOf should be written idiomatically
490-- XXX lpackArraysChunksOf should be written idiomatically
484--
491--
485−-- | @writeChunksWithBufferOf bufsize handle@ writes a stream of arrays
492+-- | @writeChunksWith bufsize handle@ writes a stream of arrays
486-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
493-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
487-- We never split an array, if a single array is bigger than the specified size
494-- We never split an array, if a single array is bigger than the specified size
488-- it emitted as it is. Multiple arrays are coalesed as long as the total size
495-- it emitted as it is. Multiple arrays are coalesed as long as the total size
489-- remains below the specified size.
496-- remains below the specified size.
490--
497--
498+-- @since 0.9.0
499+{-# INLINE writeChunksWith #-}
500+writeChunksWith :: (MonadIO m, Storable a)
501+ => Int -> Handle -> Fold m (Array a) ()
502+writeChunksWith n h = lpackArraysChunksOf n (writeChunks h)
503+
504+-- | Same as 'writeChunksWith'
505+--
491-- @since 0.7.0
506-- @since 0.7.0
507+{-# DEPRECATED writeChunksWithBufferOf "Please use writeChunksWith instead." #-}
492{-# INLINE writeChunksWithBufferOf #-}
508{-# INLINE writeChunksWithBufferOf #-}
493writeChunksWithBufferOf :: (MonadIO m, Storable a)
509writeChunksWithBufferOf :: (MonadIO m, Storable a)
494 => Int -> Handle -> Fold m (Array a) ()
510 => Int -> Handle -> Fold m (Array a) ()
495−writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
511+writeChunksWithBufferOf = writeChunksWith
496
512
497-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
513-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
498--
514--
@@ -504,55 +520,63 @@ writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
504
520
505-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
521-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
506--
522--
507−-- | @writeWithBufferOf reqSize handle@ writes the input stream to @handle@.
523+-- | @writeWith reqSize handle@ writes the input stream to @handle@.
508-- Bytes in the input stream are collected into a buffer until we have a chunk
524-- Bytes in the input stream are collected into a buffer until we have a chunk
509-- of @reqSize@ and then written to the IO device.
525-- of @reqSize@ and then written to the IO device.
510--
526--
511−-- >>> writeWithBufferOf n h = Fold.chunksOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
527+-- >>> writeWith n h = Fold.chunksOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
528+--
529+-- @since 0.7.0
530+{-# INLINE writeWith #-}
531+writeWith :: MonadIO m => Int -> Handle -> Fold m Word8 ()
532+writeWith n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
533+
534+-- | Same as 'writeWith'
512--
535--
513-- @since 0.7.0
536-- @since 0.7.0
537+{-# DEPRECATED writeWithBufferOf "Please use writeWith instead." #-}
514{-# INLINE writeWithBufferOf #-}
538{-# INLINE writeWithBufferOf #-}
515writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
539writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
516−writeWithBufferOf n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
540+writeWithBufferOf = writeWith
517
541
518-- | Write a stream of 'Maybe' values. Keep buffering the just values in an
542-- | Write a stream of 'Maybe' values. Keep buffering the just values in an
519-- array until a 'Nothing' is encountered or the buffer size exceeds the
543-- array until a 'Nothing' is encountered or the buffer size exceeds the
520-- specified limit, at that point flush the buffer to the handle.
544-- specified limit, at that point flush the buffer to the handle.
521--
545--
522-- /Pre-release/
546-- /Pre-release/
523−{-# INLINE writeMaybesWithBufferOf #-}
547+{-# INLINE writeMaybesWith #-}
524−writeMaybesWithBufferOf :: (MonadIO m )
548+writeMaybesWith :: (MonadIO m )
525 => Int -> Handle -> Fold m (Maybe Word8) ()
549 => Int -> Handle -> Fold m (Maybe Word8) ()
526−writeMaybesWithBufferOf n h =
550+writeMaybesWith n h =
527 let writeNJusts = FL.lmap fromJust $ A.writeN n
551 let writeNJusts = FL.lmap fromJust $ A.writeN n
528 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
552 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
529 in FL.many writeOnNothing (writeChunks h)
553 in FL.many writeOnNothing (writeChunks h)
530
554
531−-- | Like 'writeWithBufferOf' but uses the experimental 'Refold' API.
555+-- | Like 'writeWith' but uses the experimental 'Refold' API.
532--
556--
533-- /Internal/
557-- /Internal/
534−{-# INLINE consumerWithBufferOf #-}
558+{-# INLINE consumerWith #-}
535−consumerWithBufferOf :: MonadIO m => Int -> Refold m Handle Word8 ()
559+consumerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
536−consumerWithBufferOf n =
560+consumerWith n =
537 FL.refoldMany (FL.take n $ writeNUnsafe n) consumeChunks
561 FL.refoldMany (FL.take n $ writeNUnsafe n) consumeChunks
538
562
539-- | Write a byte stream to a file handle. Accumulates the input in chunks of
563-- | Write a byte stream to a file handle. Accumulates the input in chunks of
540-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing
564-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing
541-- to the IO device.
565-- to the IO device.
542--
566--
543−-- >>> write = Handle.writeWithBufferOf IO.defaultChunkSize
567+-- >>> write = Handle.writeWith IO.defaultChunkSize
544--
568--
545-- @since 0.7.0
569-- @since 0.7.0
546{-# INLINE write #-}
570{-# INLINE write #-}
547write :: MonadIO m => Handle -> Fold m Word8 ()
571write :: MonadIO m => Handle -> Fold m Word8 ()
548−write = writeWithBufferOf defaultChunkSize
572+write = writeWith defaultChunkSize
549
573
550-- | Like 'write' but uses the experimental 'Refold' API.
574-- | Like 'write' but uses the experimental 'Refold' API.
551--
575--
552-- /Internal/
576-- /Internal/
553{-# INLINE consumer #-}
577{-# INLINE consumer #-}
554consumer :: MonadIO m => Refold m Handle Word8 ()
578consumer :: MonadIO m => Refold m Handle Word8 ()
555−consumer = consumerWithBufferOf defaultChunkSize
579+consumer = consumerWith defaultChunkSize
556
580
557{-
581{-
558{-# INLINE write #-}
582{-# INLINE write #-}
test/Streamly/Test/FileSystem/Handle.hs+7−7
@@ -80,7 +80,7 @@ readFromHandle =
80readWithBufferFromHandle :: IO (SerialT IO Char)
80readWithBufferFromHandle :: IO (SerialT IO Char)
81readWithBufferFromHandle =
81readWithBufferFromHandle =
82 let f1 = (\h -> (1024, h))
82 let f1 = (\h -> (1024, h))
83− f2 = Unicode.decodeUtf8 . Stream.unfold Handle.readWithBufferOf . f1
83+ f2 = Unicode.decodeUtf8 . Stream.unfold Handle.readWith . f1
84 in executor f2
84 in executor f2
85
85
86readChunksFromHandle :: IO (SerialT IO Char)
86readChunksFromHandle :: IO (SerialT IO Char)
@@ -96,7 +96,7 @@ readChunksWithBuffer =
96 f2 =
96 f2 =
97 Unicode.decodeUtf8
97 Unicode.decodeUtf8
98 . Stream.concatMap Array.toStream
98 . Stream.concatMap Array.toStream
99− . Stream.unfold Handle.readChunksWithBufferOf
99+ . Stream.unfold Handle.readChunksWith
100 . f1
100 . f1
101 in executor f2
101 in executor f2
102
102
@@ -148,7 +148,7 @@ testWriteWithChunk =
148 hw <- openFile fpathWrite ReadWriteMode
148 hw <- openFile fpathWrite ReadWriteMode
149 hSeek hw AbsoluteSeek 0
149 hSeek hw AbsoluteSeek 0
150 _ <- Stream.fold (Handle.writeChunks hw)
150 _ <- Stream.fold (Handle.writeChunks hw)
151− $ Stream.unfold Handle.readChunksWithBufferOf (1024, hr)
151+ $ Stream.unfold Handle.readChunksWith (1024, hr)
152 hFlush hw
152 hFlush hw
153 hSeek hw AbsoluteSeek 0
153 hSeek hw AbsoluteSeek 0
154 ls <- Stream.toList $ Stream.unfold Handle.read hw
154 ls <- Stream.toList $ Stream.unfold Handle.read hw
@@ -209,9 +209,9 @@ main =
209 describe moduleName $ do
209 describe moduleName $ do
210 describe "Read From Handle" $ do
210 describe "Read From Handle" $ do
211 prop "read" $ testRead readFromHandle
211 prop "read" $ testRead readFromHandle
212− prop "readWithBufferOf" $ testRead readWithBufferFromHandle
212+ prop "readWith" $ testRead readWithBufferFromHandle
213 prop "readChunks" $ testRead readChunksFromHandle
213 prop "readChunks" $ testRead readChunksFromHandle
214− prop "readChunksWithBufferOf" $ testRead readChunksWithBuffer
214+ prop "readChunksWith" $ testRead readChunksWithBuffer
215 prop "readChunksFromToWith (0,0,n)"
215 prop "readChunksFromToWith (0,0,n)"
216 testReadChunksFromToWithFirstByte
216 testReadChunksFromToWithFirstByte
217 prop "readChunksFromToWith (1,1,n)"
217 prop "readChunksFromToWith (1,1,n)"
@@ -226,7 +226,7 @@ main =
226 testReadChunksFromToWithRangeInvalid
226 testReadChunksFromToWithRangeInvalid
227 describe "Write To Handle" $ do
227 describe "Write To Handle" $ do
228 prop "write" $ testWrite Handle.write
228 prop "write" $ testWrite Handle.write
229− prop "writeWithBufferOf"
229+ prop "writeWith"
230− $ testWrite $ Handle.writeWithBufferOf 1024
230+ $ testWrite $ Handle.writeWith 1024
231 -- XXX This test needs a lot of stack when built with -O0
231 -- XXX This test needs a lot of stack when built with -O0
232 prop "writeChunks" testWriteWithChunk
232 prop "writeChunks" testWriteWithChunk