{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE CPP #-}
module Internal
( module Control.Applicative
, module Control.Monad
, module Control.Exception
, module Data.Word
, unsafeIOToST, ST
, Typeable
, ByteString
, LzEncoder(..)
, LzDecoder(..)
, intCast
, int2cint
, ExceptT(ExceptT), runExceptT, throwE, liftE
) where
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.ST.Strict (ST)
import Control.Monad.ST.Unsafe (unsafeIOToST)
import Data.ByteString (ByteString)
import Data.Typeable (Typeable)
import Data.Word
import Foreign
import Foreign.C
#if defined(MIN_VERSION_int_cast)
import Data.IntCast (intCast)
#else
intCast :: (Integral a, Num b) => a -> b
intCast = fromIntegral
#endif
int2cint :: Int -> CInt
int2cint = fromIntegral . min maxCInt . max 0
where
maxCInt :: Int
maxCInt = intCast (maxBound :: CInt)
newtype LzEncoder = LzEncoder (ForeignPtr LzEncoder)
newtype LzDecoder = LzDecoder (ForeignPtr LzDecoder)
newtype ExceptT e m a = ExceptT (m (Either e a))
runExceptT :: ExceptT e m a -> m (Either e a)
runExceptT (ExceptT m) = m
throwE :: Applicative m => e -> ExceptT e m a
throwE = ExceptT . pure . Left
liftE :: Applicative m => m a -> ExceptT e m a
liftE = ExceptT . liftA Right
instance Functor m => Functor (ExceptT e m) where
fmap f = ExceptT . fmap (fmap f) . runExceptT
instance (Applicative m, Monad m) => Applicative (ExceptT e m) where
pure = ExceptT . pure . Right
(<*>) = ap
m *> k = m >>= \_ -> k
instance (Applicative m, Monad m) => Monad (ExceptT e m) where
m >>= k = ExceptT (either (pure . Left) (runExceptT . k) =<< runExceptT m)
return = pure
(>>) = (*>)