{-# LANGUAGE CPP, NoImplicitPrelude, FlexibleContexts #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
module System.Timeout.Lifted ( timeout ) where
import Prelude ( (.) )
import Data.Int ( Int )
import Data.Maybe ( Maybe(Nothing, Just), maybe )
import Control.Monad ( (>>=), return, liftM )
import System.IO ( IO )
import qualified System.Timeout as T ( timeout )
import Control.Monad.Trans.Control ( MonadBaseControl, restoreM, liftBaseWith )
#include "inlinable.h"
timeout :: MonadBaseControl IO m => Int -> m a -> m (Maybe a)
timeout t m = liftBaseWith (\runInIO -> T.timeout t (runInIO m)) >>=
maybe (return Nothing) (liftM Just . restoreM)