better MonadUnliftIO instance for ExceptT; fix tests

This commit is contained in:
Evgeny Poberezkin
2021-01-09 20:35:19 +00:00
parent d1c5e9df7b
commit ea6496ca24
2 changed files with 14 additions and 3 deletions
+13 -3
View File
@@ -1,3 +1,6 @@
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE RankNTypes #-}
module Simplex.Messaging.Util where
import Control.Monad.Except
@@ -6,10 +9,17 @@ import UnliftIO.Async
import UnliftIO.Exception (Exception)
import qualified UnliftIO.Exception as E
newtype InternalException e = InternalException {unInternalException :: e}
deriving (Eq, Show)
instance Exception e => Exception (InternalException e)
instance (MonadUnliftIO m, Exception e) => MonadUnliftIO (ExceptT e m) where
withRunInIO inner = ExceptT . E.try $
withRunInIO $ \run ->
inner (run . (either E.throwIO pure <=< runExceptT))
withRunInIO :: ((forall a. ExceptT e m a -> IO a) -> IO b) -> ExceptT e m b
withRunInIO exceptToIO =
withExceptT unInternalException . ExceptT . E.try $
withRunInIO $ \run ->
exceptToIO $ run . (either (E.throwIO . InternalException) return <=< runExceptT)
raceAny_ :: MonadUnliftIO m => [m a] -> m ()
raceAny_ = r []
+1
View File
@@ -8,6 +8,7 @@ import Control.Monad.Except
import qualified Database.SQLite.Simple as DB
import Simplex.Messaging.Agent.Store
import Simplex.Messaging.Agent.Store.SQLite
import Simplex.Messaging.Agent.Store.Types
import Simplex.Messaging.Agent.Transmission
import Test.Hspec
import UnliftIO.Directory