{-# LINE 1 "System/Posix/Handle/Internals.hsc" #-}
{-# OPTIONS_HADDOCK hide #-}
{-# LINE 2 "System/Posix/Handle/Internals.hsc" #-}

module System.Posix.Handle.Internals (
    withHandleFd
  ) where


{-# LINE 8 "System/Posix/Handle/Internals.hsc" #-}

{-# LINE 9 "System/Posix/Handle/Internals.hsc" #-}
import GHC.IO.Exception (IOErrorType(..))
import GHC.IO.FD (fdFD)
import Data.Dynamic (cast)
import System.IO.Error
import GHC.IO.Handle.Types (Handle__(..))
import GHC.IO.Handle.Internals (withHandle_)

{-# LINE 19 "System/Posix/Handle/Internals.hsc" #-}

{-# LINE 20 "System/Posix/Handle/Internals.hsc" #-}

import System.IO
import System.Posix.Types

withHandleFd :: String -> Handle -> (Fd -> IO a) -> IO a


{-# LINE 27 "System/Posix/Handle/Internals.hsc" #-}

{-# LINE 28 "System/Posix/Handle/Internals.hsc" #-}
withHandleFd s h f = withHandle_ s h $ \(Handle__{haDevice=dev}) ->
    case cast dev of
      Nothing -> ioError (ioeSetErrorString (mkIOError IllegalOperation
                                             s (Just h) Nothing)
                          "handle is not a file descriptor")
      Just fd -> f (Fd (fromIntegral (fdFD fd)))

{-# LINE 38 "System/Posix/Handle/Internals.hsc" #-}

{-# LINE 39 "System/Posix/Handle/Internals.hsc" #-}