{-# OPTIONS_GHC -Wno-orphans #-}

module UnliftIO.Except
    () where

import Control.Monad.Except
import UnliftIO

instance (MonadUnliftIO m, Exception e) => MonadUnliftIO (ExceptT e m) where
    withRunInIO :: ((forall a. ExceptT e m a -> IO a) -> IO b) -> ExceptT e m b
withRunInIO (forall a. ExceptT e m a -> IO a) -> IO b
exceptToIO = m (Either e b) -> ExceptT e m b
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e b) -> ExceptT e m b)
-> m (Either e b) -> ExceptT e m b
forall a b. (a -> b) -> a -> b
$ m b -> m (Either e b)
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
try (m b -> m (Either e b)) -> m b -> m (Either e b)
forall a b. (a -> b) -> a -> b
$ do
        ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO b) -> m b)
-> ((forall a. m a -> IO a) -> IO b) -> m b
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
runInIO ->
            (forall a. ExceptT e m a -> IO a) -> IO b
exceptToIO (m a -> IO a
forall a. m a -> IO a
runInIO (m a -> IO a) -> (ExceptT e m a -> m a) -> ExceptT e m a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((e -> m a) -> (a -> m a) -> Either e a -> m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either e -> m a
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> m a)
-> (ExceptT e m a -> m (Either e a)) -> ExceptT e m a -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ExceptT e m a -> m (Either e a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT))