 import Unsafe.Coerce
 
 import Control.Monad                            (liftM)
+import Control.Exception.Extensible             (throwIO)
 import Data.ByteString.Char8                    (ByteString)
 import qualified Data.ByteString.Char8 as B
 

-- Return a handle to a texture reference
--
getTex :: Module -> String -> IO Texture
getTex mdl name = resultIfOk =<< cuModuleGetTexRef mdl name

{# fun unsafe cuModuleGetTexRef
  { alloca-      `Texture' peekTex*

-- Internal
--------------------------------------------------------------------------------

resultIfFound :: String -> String -> (Status, a) -> IO a
resultIfFound kind name (status,result) =
  case status of
       Success  -> return result
       NotFound -> cudaError (kind ++ ' ' : describe status ++ ": " ++ name)
       _        -> throwIO (ExitCode status)

peekMod :: Ptr {# type CUmodule #} -> IO Module
peekMod = liftM Module . peek

