on master help sign in
Add stream read ops for Array/Handle/Socket/TCP
commit3fdf18350273a55c87da36dc3115d149c39762c7 authorHarendra Kumar <harendra@composewell.com> dateSun 26 Nov 2023, 06:38 (3 years ago) parent8b84d54
4 files changed+31−21
core/src/Streamly/Data/Array.hs+7−3 core/src/Streamly/FileSystem/Handle.hs+7−6 src/Streamly/Network/Inet/TCP.hs+6−6 src/Streamly/Network/Socket.hs+11−6
core/src/Streamly/Data/Array.hs+7−3
@@ -66,6 +66,10 @@ module Streamly.Data.Array
6666 , unpin
6767 , isPinned
6868
69+ -- * Streams
70+ , read
71+ , readRev
72+
6973 -- * Unfolds
7074 , reader
7175 , readerRev
@@ -87,7 +91,7 @@ where
8791
8892#include "inline.hs"
8993
90−import Streamly.Internal.Data.Array hiding (read, readRev)
94+import Streamly.Internal.Data.Array
9195import Streamly.Internal.Data.Unbox (Unbox (..))
9296
9397import Prelude hiding (read, length)
@@ -106,7 +110,7 @@ import Prelude hiding (read, length)
106110--
107111-- Convert array to stream, and fold the stream:
108112--
109−-- >>> fold f arr = Stream.unfold Array.reader arr & Stream.fold f
113+-- >>> fold f arr = Array.read arr & Stream.fold f
110114-- >>> fold Fold.sum (Array.fromList [1,2,3::Int])
111115-- 6
112116--
@@ -114,7 +118,7 @@ import Prelude hiding (read, length)
114118--
115119-- Convert array to stream, transform, and fold back to array:
116120--
117−-- >>> amap f arr = Stream.unfold Array.reader arr & fmap f & Stream.fold Array.write
121+-- >>> amap f arr = Array.read arr & fmap f & Stream.fold Array.write
118122-- >>> amap (+1) (Array.fromList [1,2,3::Int])
119123-- fromList [2,3,4]
120124--
core/src/Streamly/FileSystem/Handle.hs+7−6
@@ -81,13 +81,13 @@ module Streamly.FileSystem.Handle
8181 -- position of the file handle. The stream ends as soon as EOF is
8282 -- encountered.
8383
84− -- -- *** Streams
85− -- , read
86− -- , readWith
87− -- , readChunks
88− -- , readChunksWith
84+ -- *** Streams
85+ , read
86+ , readWith
87+ , readChunks
88+ , readChunksWith
8989
90− -- -- *** Unfolds
90+ -- *** Unfolds
9191 , reader
9292 , readerWith
9393 , chunkReader
@@ -112,5 +112,6 @@ module Streamly.FileSystem.Handle
112112where
113113
114114import Streamly.Internal.FileSystem.Handle
115+import Prelude hiding (read)
115116
116117#include "DocTestFileSystemHandle.hs"
src/Streamly/Network/Inet/TCP.hs+6−6
@@ -15,8 +15,13 @@
1515module Streamly.Network.Inet.TCP
1616 (
1717 -- * Accept Connections
18+ -- ** Streams
19+ acceptOnAddr
20+ , acceptOnPort
21+ , acceptOnPortLocal
22+
1823 -- ** Unfolds
19− acceptorOnAddr
24+ , acceptorOnAddr
2025 , acceptorOnPort
2126 , acceptorOnPortLocal
2227
@@ -51,9 +56,4 @@ module Streamly.Network.Inet.TCP
5156 )
5257where
5358
54−import Control.Monad.IO.Class (MonadIO(..))
55−import Data.Word (Word8)
56−import Network.Socket (Socket, PortNumber)
57−import Streamly.Internal.Data.Unfold (Unfold(..))
58−
5959import Streamly.Internal.Network.Inet.TCP
src/Streamly/Network/Socket.hs+11−6
@@ -50,11 +50,11 @@
5050-- in server spec addr
5151-- where
5252-- server spec addr =
53−-- Stream.unfold Socket.acceptor (maxListenQueue, spec, addr)
53+-- Socket.accept maxListenQueue spec addr
5454-- & Stream.parMapM (Stream.eager True) (Socket.forSocketM echo)
5555-- & Stream.fold Fold.drain
5656-- echo sk =
57−-- Stream.unfold Socket.chunkReader sk -- Stream IO (Array Word8)
57+-- Socket.readChunks sk -- Stream IO (Array Word8)
5858-- & Stream.fold (Socket.writeChunks sk) -- IO ()
5959-- :}
6060--
@@ -95,12 +95,19 @@ module Streamly.Network.Socket
9595 SockSpec(..)
9696
9797 -- * Accept Connections
98+ , accept
9899 , acceptor
99100
100101 -- * Reads
101102 -- ** Singleton
102103 , getChunk
103104
105+ -- ** Streams
106+ , read
107+ , readWith
108+ , readChunks
109+ , readChunksWith
110+
104111 -- ** Unfolds
105112 , reader
106113 , readerWith
@@ -130,11 +137,9 @@ module Streamly.Network.Socket
130137 )
131138where
132139
133−import Control.Monad.IO.Class (MonadIO(..))
134140import Data.Word (Word8)
135−import Network.Socket (Socket, SockAddr)
136−import Streamly.Internal.Data.Unfold (Unfold(..))
137−import Streamly.Internal.Data.Array (Array(..))
141+import Network.Socket (Socket)
142+import Streamly.Data.Array (Array)
138143import Streamly.Data.MutByteArray (Unbox)
139144
140145import Streamly.Internal.Network.Socket