on master help sign in
Add api to generate Stream from memory Address
64512ceRanjeet Kumar Ranjan
core/src/Streamly/Internal/Data/Stream/Generate.hs+16−2
@@ -27,13 +27,19 @@ module Streamly.Internal.Data.Stream.Generate
27 -- * From Containers
27 -- * From Containers
28 , fromFoldable
28 , fromFoldable
29 , fromFoldableM
29 , fromFoldableM
30+
31+ -- * From memory
32+ , fromPtr
30 )
33 )
31where
34where
32
35
36+import Control.Monad.IO.Class (MonadIO)
37+import Foreign.Storable (Storable)
38+import Foreign.Ptr (Ptr)
33import Streamly.Internal.Data.Stream.Type (Stream, fromStreamK, toStreamK)
39import Streamly.Internal.Data.Stream.Type (Stream, fromStreamK, toStreamK)
34−import Streamly.Internal.Data.Unfold (Unfold)
40+import Streamly.Internal.Data.Unfold.Type (Unfold)
35
41
36−import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
42+import qualified Streamly.Internal.Data.Stream.StreamD.Generate as D
37import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
43import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
38import qualified Streamly.Internal.Data.Stream.Type as Stream
44import qualified Streamly.Internal.Data.Stream.Type as Stream
39
45
@@ -140,3 +146,11 @@ fromFoldable = fromStreamK . K.fromFoldable
140{-# INLINE fromFoldableM #-}
146{-# INLINE fromFoldableM #-}
141fromFoldableM :: (Monad m, Foldable f) => f (m a) -> Stream m a
147fromFoldableM :: (Monad m, Foldable f) => f (m a) -> Stream m a
142fromFoldableM = Prelude.foldr Stream.consM Stream.nil
148fromFoldableM = Prelude.foldr Stream.consM Stream.nil
149+
150+------------------------------------------------------------------------------
151+-- From pointers
152+------------------------------------------------------------------------------
153+
154+{-# INLINE fromPtr #-}
155+fromPtr :: (MonadIO m, Storable a) => Int -> Ptr a -> Stream m a
156+fromPtr n = Stream.fromStreamD . D.fromPtr n
core/src/Streamly/Internal/Data/Stream/StreamD/Generate.hs+22
@@ -79,6 +79,9 @@ module Streamly.Internal.Data.Stream.StreamD.Generate
79 , fromList
79 , fromList
80 , fromListM
80 , fromListM
81
81
82+ -- * From Pointers
83+ , fromPtr
84+
82 -- * Conversions
85 -- * Conversions
83 , fromStreamK
86 , fromStreamK
84 , toStreamK
87 , toStreamK
@@ -86,8 +89,11 @@ module Streamly.Internal.Data.Stream.StreamD.Generate
86where
89where
87
90
88#include "inline.hs"
91#include "inline.hs"
92+#include "ArrayMacros.h"
89
93
90import Control.Monad.IO.Class (MonadIO(..))
94import Control.Monad.IO.Class (MonadIO(..))
95+import Foreign.Ptr (Ptr, plusPtr)
96+import Foreign.Storable (Storable (peek), sizeOf)
91import Streamly.Internal.Control.Concurrent (MonadAsync)
97import Streamly.Internal.Control.Concurrent (MonadAsync)
92import Streamly.Internal.Data.Time.Clock
98import Streamly.Internal.Data.Time.Clock
93 (Clock(Monotonic), asyncClock, readClock)
99 (Clock(Monotonic), asyncClock, readClock)
@@ -459,3 +465,19 @@ fromListM = Stream step
459 step _ (m:ms) = m >>= \x -> return $ Yield x ms
465 step _ (m:ms) = m >>= \x -> return $ Yield x ms
460 step _ [] = return Stop
466 step _ [] = return Stop
461#endif
467#endif
468+
469+-------------------------------------------------------------------------------
470+-- From pointers
471+-------------------------------------------------------------------------------
472+
473+{-# INLINE fromPtr #-}
474+fromPtr :: forall m a. (MonadIO m, Storable a) => Int -> Ptr a -> Stream m a
475+fromPtr count ptr = Stream step (count, ptr)
476+
477+ where
478+
479+ {-# INLINE_LATE step #-}
480+ step _ (0, _) = return Stop
481+ step _ (n, p) = do
482+ x <- liftIO $ peek p
483+ return $ Yield x (n - 1, PTR_NEXT(p, a))
core/src/Streamly/Internal/Data/Unfold.hs+20
@@ -177,6 +177,9 @@ module Streamly.Internal.Data.Unfold
177 , fromList
177 , fromList
178 , fromListM
178 , fromListM
179
179
180+ -- ** From Pointer
181+ , fromPtr
182+
180 , fromStreamK
183 , fromStreamK
181 , fromStreamD
184 , fromStreamD
182
185
@@ -260,6 +263,8 @@ module Streamly.Internal.Data.Unfold
260 )
263 )
261where
264where
262
265
266+#include "ArrayMacros.h"
267+
263import Control.Exception (Exception, mask_)
268import Control.Exception (Exception, mask_)
264import Control.Monad.Catch (MonadCatch)
269import Control.Monad.Catch (MonadCatch)
265import Data.Functor (($>))
270import Data.Functor (($>))
@@ -282,6 +287,9 @@ import Streamly.Internal.Data.Unfold.Type
282import Prelude
287import Prelude
283 hiding (map, mapM, takeWhile, take, filter, const, zipWith
288 hiding (map, mapM, takeWhile, take, filter, const, zipWith
284 , drop, dropWhile, either)
289 , drop, dropWhile, either)
290+import Control.Monad.IO.Class (MonadIO (liftIO))
291+import Foreign (Storable, peek, sizeOf)
292+import Foreign.Ptr
285
293
286-- $setup
294-- $setup
287-- >>> import qualified Streamly.Data.Fold as Fold
295-- >>> import qualified Streamly.Data.Fold as Fold
@@ -627,6 +635,18 @@ fromListM = Unfold step pure
627 step (x:xs) = (`Yield` xs) <$> x
635 step (x:xs) = (`Yield` xs) <$> x
628 step [] = pure Stop
636 step [] = pure Stop
629
637
638+{-# INLINE fromPtr #-}
639+fromPtr :: forall m a. (MonadIO m, Storable a) => Unfold m (Int, Ptr a) a
640+fromPtr = Unfold step return
641+
642+ where
643+
644+ {-# INLINE_LATE step #-}
645+ step (0, _) = return Stop
646+ step (n, p) = do
647+ x <- liftIO $ peek p
648+ return $ Yield x (n - 1, PTR_NEXT(p, a))
649+
630------------------------------------------------------------------------------
650------------------------------------------------------------------------------
631-- Specialized Generation
651-- Specialized Generation
632------------------------------------------------------------------------------
652------------------------------------------------------------------------------