{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE QualifiedDo #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# OPTIONS_HADDOCK hide #-}

-- | This module contains functions for interoperating with other
-- streaming libraries.
module Streaming.Linear.Internal.Interop
  ( -- * Interoperating with other streaming libraries
    reread,
  )
where

import qualified Control.Functor.Linear as Control
import Data.Unrestricted.Linear
import Prelude.Linear (($))
import Streaming.Linear.Internal.Produce
import Streaming.Linear.Internal.Type
import Prelude (Maybe (..))

-- | Read an @IORef (Maybe a)@ or a similar device until it reads @Nothing@.
--    @reread@ provides convenient exit from the @io-streams@ library
--
-- > reread readIORef    :: IORef (Maybe a) -> Stream (Of a) IO ()
-- > reread Streams.read :: System.IO.Streams.InputStream a -> Stream (Of a) IO ()
reread ::
  (Control.Monad m) =>
  (s -> m (Ur (Maybe a))) ->
  s ->
  Stream (Of a) m ()
reread :: forall (m :: * -> *) s a.
Monad m =>
(s -> m (Ur (Maybe a))) -> s -> Stream (Of a) m ()
reread s -> m (Ur (Maybe a))
f s
s = forall (m :: * -> *) s a.
Monad m =>
(s -> m (Ur (Maybe a))) -> s -> Stream (Of a) m ()
reread' s -> m (Ur (Maybe a))
f s
s
  where
    reread' ::
      (Control.Monad m) =>
      (s -> m (Ur (Maybe a))) ->
      s ->
      Stream (Of a) m ()
    reread' :: forall (m :: * -> *) s a.
Monad m =>
(s -> m (Ur (Maybe a))) -> s -> Stream (Of a) m ()
reread' s -> m (Ur (Maybe a))
f s
s = forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ Control.do
      Ur Maybe a
maybeA <- s -> m (Ur (Maybe a))
f s
s
      case Maybe a
maybeA of
        Maybe a
Nothing -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return ()
        Just a
a -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ (forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a forall (m :: * -> *) a. Monad m => m () %1 -> m a %1 -> m a
Control.>> forall (m :: * -> *) s a.
Monad m =>
(s -> m (Ur (Maybe a))) -> s -> Stream (Of a) m ()
reread s -> m (Ur (Maybe a))
f s
s)
{-# INLINEABLE reread #-}