Skip to content

Commit 357749b

Browse files
committed
define MonadThrow and MonadCatch instances for Stream
These are equivalent to the code in the existing MonadError instance.
1 parent 608c4cf commit 357749b

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

src/Streaming/Internal.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module Streaming.Internal (
8686
import Control.Applicative
8787
import Control.Concurrent (threadDelay)
8888
import Control.Monad
89+
import Control.Monad.Catch (MonadThrow (..), MonadCatch (..))
8990
import Control.Monad.Error.Class
9091
import Control.Monad.Fail as Fail
9192
import Control.Monad.Morph
@@ -381,6 +382,19 @@ instance (Functor f, MonadState s m) => MonadState s (Stream f m) where
381382
{-# INLINE state #-}
382383
#endif
383384

385+
instance (Functor f, MonadThrow m) => MonadThrow (Stream f m) where
386+
throwM = lift . throwM
387+
{-# INLINE throwM #-}
388+
389+
instance (Functor f, MonadCatch m) => MonadCatch (Stream f m) where
390+
catch str f = loop str
391+
where
392+
loop x = case x of
393+
Return r -> Return r
394+
Effect m -> Effect $ fmap loop m `catch` (return . f)
395+
Step g -> Step (fmap loop g)
396+
{-# INLINABLE catch #-}
397+
384398
instance (Functor f, MonadError e m) => MonadError e (Stream f m) where
385399
throwError = lift . throwError
386400
{-# INLINE throwError #-}

streaming.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ library
212212
, transformers-base < 0.5
213213
, ghc-prim
214214
, containers
215+
, exceptions >=0.6
215216

216217
if !impl(ghc >= 8.0)
217218
build-depends:

0 commit comments

Comments
 (0)