on master help sign in
Implement read unfold for ring buffers
0e739d7Harendra Kumar
core/src/Streamly/Internal/Data/Ring/Foreign.hs+24−5
@@ -84,6 +84,7 @@ import GHC.Ptr (Ptr(..))
84import Streamly.Internal.Data.Array.Foreign.Mut.Type (Array, memcmp)
84import Streamly.Internal.Data.Array.Foreign.Mut.Type (Array, memcmp)
85import Streamly.Internal.Data.Fold.Type (Fold(..), Step(..), lmap)
85import Streamly.Internal.Data.Fold.Type (Fold(..), Step(..), lmap)
86import Streamly.Internal.Data.Stream.Serial (SerialT(..))
86import Streamly.Internal.Data.Stream.Serial (SerialT(..))
87+import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
87import Streamly.Internal.Data.Unfold.Type (Unfold(..))
88import Streamly.Internal.Data.Unfold.Type (Unfold(..))
88import Streamly.Internal.System.IO (unsafeInlineIO)
89import Streamly.Internal.System.IO (unsafeInlineIO)
89
90
@@ -304,13 +305,31 @@ bytesFree = undefined
304-- Unfolds
305-- Unfolds
305-------------------------------------------------------------------------------
306-------------------------------------------------------------------------------
306
307
307−-- | Unfold a ring array into a stream.
308+-- XXX We can read the ring in a loop and use "take" to restrict the number of
309+-- elements to be taken.
308--
310--
309−-- /Unimplemented/
311+-- | Read n elements from the ring starting at the supplied ring head. If n is
312+-- more than the ring size it keeps reading the ring in a circular fashion.
313+--
314+-- If the ring is not full the user must ensure than n is less than or equal to
315+-- the number of valid elements in the ring.
316+--
317+-- /Internal/
310{-# INLINE_NORMAL read #-}
318{-# INLINE_NORMAL read #-}
311−read :: -- forall m a. (MonadIO m, Storable a) =>
319+read :: forall m a. (MonadIO m, Storable a) => Unfold m (Ring a, Ptr a, Int) a
312− Unfold m (Ring a) a
320+read = Unfold step return
313−read = undefined
321+
322+ where
323+
324+ step (rb, rh, n) = do
325+ if n <= 0
326+ then do
327+ liftIO $ touchForeignPtr (ringStart rb)
328+ return Stop
329+ else do
330+ x <- liftIO $ peek rh
331+ let rh1 = advance rb rh
332+ return $ Yield x (rb, rh1, n - 1)
314
333
315-- | Unfold a ring array into a stream in reverse order.
334-- | Unfold a ring array into a stream in reverse order.
316--
335--