@@ -98,9 +98,14 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
98 -- ** To streams
98 -- ** To streams
99 , ReadUState(..)
99 , ReadUState(..)
101 , readRev
102 , readRev
103 -- ** To containers
104 -- ** To containers
104 , toStreamD
109 , toStreamD
105 , toStreamDRev
110 , toStreamDRev
106 , toStreamK
111 , toStreamK
@@ -1536,12 +1541,11 @@ producer = Producer step (return . toReadUState) extract
1536read :: forall m a. (MonadIO m, Storable a) => Unfold m (Array a) a
1541read :: forall m a. (MonadIO m, Storable a) => Unfold m (Array a) a
1537read = Producer.simplify producer
1542read = Producer.simplify producer
1539−-- | Unfold an array into a stream in reverse order.
1542−{-# INLINE_NORMAL readRev #-}
1544+{-# INLINE_NORMAL readRev_ #-}
1543−readRev :: forall m a. (MonadIO m, Storable a) => Unfold m (Array a) a
1546+ forall m a. (Monad m, Storable a)
1547+ => (forall b. IO b -> m b) -> Unfold m (Array a) a
1544−readRev = Unfold step inject
1548+readRev_ liftio = Unfold step inject
1547 inject (Array contents start end _) =
1551 inject (Array contents start end _) =
@@ -1550,12 +1554,24 @@ readRev = Unfold step inject
1551 {-# INLINE_LATE step #-}
1555 {-# INLINE_LATE step #-}
1552 step (ReadUState contents start p) | p < start = do
1556 step (ReadUState contents start p) | p < start = do
1557+ -- Is the following replicated properly?
1558+ -- let x = unsafeInlineIO $ touch contents
1559+ -- in x `seq` return D.Stop
1553− liftIO $ touch contents
1560+ liftio $ touch contents
1554 return D.Stop
1561 return D.Stop
1562+ -- unit <- liftio $ touch contents
1563+ -- unit `seq` return D.Stop
1555 step (ReadUState contents start p) = do
1564 step (ReadUState contents start p) = do
1556− x <- liftIO $ peek p
1565+ x <- liftio $ peek p
1557 return $ D.Yield x (ReadUState contents start (PTR_PREV(p,a)))
1566 return $ D.Yield x (ReadUState contents start (PTR_PREV(p,a)))
1568+-- | Unfold an array into a stream in reverse order.
1571+{-# INLINE_NORMAL readRev #-}
1572+readRev :: forall m a. (MonadIO m, Storable a) => Unfold m (Array a) a
1573+readRev = readRev_ liftIO
1559-------------------------------------------------------------------------------
1575-------------------------------------------------------------------------------
1560-- to Lists and streams
1576-- to Lists and streams
1561-------------------------------------------------------------------------------
1577-------------------------------------------------------------------------------
@@ -1600,27 +1616,35 @@ toList Array{..} = liftIO $ go arrStart
1600 touch arrContents
1616 touch arrContents
1601 (:) x <$> go (PTR_NEXT(p,a))
1617 (:) x <$> go (PTR_NEXT(p,a))
1603−-- | Use the 'read' unfold instead.
1605−-- @toStreamD = D.unfold read@
1607−-- We can try this if the unfold has any performance issues.
1608−{-# INLINE_NORMAL toStreamD #-}
1619+{-# INLINE_NORMAL toStreamD_ #-}
1609−toStreamD :: forall m a. (MonadIO m, Storable a) => Array a -> D.Stream m a
1621+ forall m a. (Monad m, Storable a)
1622+ => (forall b. IO b -> m b) -> Array a -> D.Stream m a
1610−toStreamD Array{..} = D.Stream step arrStart
1623+toStreamD_ liftio Array{..} = D.Stream step arrStart
1614 {-# INLINE_LATE step #-}
1627 {-# INLINE_LATE step #-}
1615 step _ p | assert (p <= aEnd) (p == aEnd) = return D.Stop
1628 step _ p | assert (p <= aEnd) (p == aEnd) = return D.Stop
1616− step _ p = liftIO $ do
1629+ step _ p = liftio $ do
1617 r <- peek p
1630 r <- peek p
1618 touch arrContents
1631 touch arrContents
1619 return $ D.Yield r (PTR_NEXT(p,a))
1632 return $ D.Yield r (PTR_NEXT(p,a))
1621−{-# INLINE toStreamK #-}
1634+-- | Use the 'read' unfold instead.
1636+-- @toStreamD = D.unfold read@
1638+-- We can try this if the unfold has any performance issues.
1639+{-# INLINE_NORMAL toStreamD #-}
1640+toStreamD :: forall m a. (MonadIO m, Storable a) => Array a -> D.Stream m a
1641+toStreamD arr = toStreamD_ liftIO arr
1643+{-# INLINE toStreamK_ #-}
1622−toStreamK :: forall m a. (MonadIO m, Storable a) => Array a -> K.Stream m a
1645+ forall m a. (Monad m, Storable a)
1646+ => (forall b. IO b -> m b) -> Array a -> K.Stream m a
1623−toStreamK Array{..} = go arrStart
1647+toStreamK_ liftio Array{..} = go arrStart
@@ -1630,16 +1654,17 @@ toStreamK Array{..} = go arrStart
1630 r <- peek p
1654 r <- peek p
1631 touch arrContents
1655 touch arrContents
1632 return r
1656 return r
1633− in liftIO elemM `K.consM` go (PTR_NEXT(p,a))
1657+ in liftio elemM `K.consM` go (PTR_NEXT(p,a))
1635−-- | Use the 'readRev' unfold instead.
1659+{-# INLINE toStreamK #-}
1637−-- @toStreamDRev = D.unfold readRev@
1639−-- We can try this if the unfold has any perf issues.
1640−{-# INLINE_NORMAL toStreamDRev #-}
1641−toStreamDRev :: forall m a. (MonadIO m, Storable a) => Array a -> D.Stream m a
1660+toStreamK :: forall m a. (MonadIO m, Storable a) => Array a -> K.Stream m a
1642−toStreamDRev Array{..} =
1661+toStreamK arr = toStreamK_ liftIO arr
1663+{-# INLINE_NORMAL toStreamDRev_ #-}
1665+ forall m a. (Monad m, Storable a)
1666+ => (forall b. IO b -> m b) -> Array a -> D.Stream m a
1667+toStreamDRev_ liftio Array{..} =
1643 let p = PTR_PREV(aEnd,a)
1668 let p = PTR_PREV(aEnd,a)
1644 in D.Stream step p
1669 in D.Stream step p
@@ -1647,14 +1672,25 @@ toStreamDRev Array{..} =
1648 {-# INLINE_LATE step #-}
1673 {-# INLINE_LATE step #-}
1649 step _ p | p < arrStart = return D.Stop
1674 step _ p | p < arrStart = return D.Stop
1650− step _ p = liftIO $ do
1675+ step _ p = liftio $ do
1651 r <- peek p
1676 r <- peek p
1652 touch arrContents
1677 touch arrContents
1653 return $ D.Yield r (PTR_PREV(p,a))
1678 return $ D.Yield r (PTR_PREV(p,a))
1655−{-# INLINE toStreamKRev #-}
1680+-- | Use the 'readRev' unfold instead.
1682+-- @toStreamDRev = D.unfold readRev@
1684+-- We can try this if the unfold has any perf issues.
1685+{-# INLINE_NORMAL toStreamDRev #-}
1686+toStreamDRev :: forall m a. (MonadIO m, Storable a) => Array a -> D.Stream m a
1687+toStreamDRev arr = toStreamDRev_ liftIO arr
1689+{-# INLINE toStreamKRev_ #-}
1656−toStreamKRev :: forall m a. (MonadIO m, Storable a) => Array a -> K.Stream m a
1691+ forall m a. (Monad m, Storable a)
1657−toStreamKRev Array {..} =
1692+ => (forall b. IO b -> m b) -> Array a -> K.Stream m a
1693+toStreamKRev_ liftio Array {..} =
1658 let p = PTR_PREV(aEnd,a)
1694 let p = PTR_PREV(aEnd,a)
@@ -1666,7 +1702,11 @@ toStreamKRev Array {..} =
1666 r <- peek p
1702 r <- peek p
1667 touch arrContents
1703 touch arrContents
1668 return r
1704 return r
1669− in liftIO elemM `K.consM` go (PTR_PREV(p,a))
1705+ in liftio elemM `K.consM` go (PTR_PREV(p,a))
1707+{-# INLINE toStreamKRev #-}
1708+toStreamKRev :: forall m a. (MonadIO m, Storable a) => Array a -> K.Stream m a
1709+toStreamKRev arr = toStreamKRev_ liftIO arr
1671-------------------------------------------------------------------------------
1711-------------------------------------------------------------------------------
1672-- Folding
1712-- Folding