Getting the file descriptor from a handle, without closing it

Hello! I need to get the file descriptors of some handles, but the handles should not be modified. They should not be closed by the operation. I guess, that the handle gets closed for a reason. But I'm using the revealed file descriptors in a way which should pose no problems for the integrity of the GHC library. In order to make a non-closing handleToFd function, I'm studying GHC's library source code. But there's a lot which I can't make much sense, because it uses non-standard language features (GHC extensions), which I'm not familiar with. I don't mean you to explain these GHC extensions to me. I just want to give an impression of the problems I encounter: 1. data FD = FD { fdFD :: {-# UNPACK #-} !CInt, fdIsNonBlocking :: {-# UNPACK #-} !Int } What is that exclamation mark? And that "{-# UNPACK #-}"? 2. handleToFd' :: Handle -> Handle__ -> IO (Handle__, Fd) handleToFd' h h_@Handle__{haType=_,..} = do case cast haDevice of -- ... haDevice should be a function. How could you cast it? 3. data Handle__ = forall dev enc_state dec_state . (IODevice dev, BufferedIO dev, Typeable dev) => Handle__ { -- ... } deriving Typeable What's that "forall" thing? 4. handleToFd' h h_@Handle__{haType=_,..} = do case cast haDevice of Nothing -> -- ... Just fd -> do -- ... return (Handle__{haType=ClosedHandle,..}, Fd (fromIntegral (FD.fdFD fd))) What's this ".." inside "Handle__{haType=ClosedHandle,..}"? If anyone can point out to me, how this non-blocking handleToFd function should be made, I would be grateful. Greetings Volker Wysk

On 1 October 2011 08:30, Volker Wysk
1.
data FD = FD { fdFD :: {-# UNPACK #-} !CInt, fdIsNonBlocking :: {-# UNPACK #-} !Int }
What is that exclamation mark?
That's a strictness annotation and is haskell98/2010: http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-680004.2 And that "{-# UNPACK #-}"? To quote: http://www.haskell.org/ghc/docs/latest/html/users_guide/pragmas.html#unpack-... "The UNPACK indicates to the compiler that it should unpack the contents of a constructor field into the constructor itself, removing a level of indirection"
2.
handleToFd' :: Handle -> Handle__ -> IO (Handle__, Fd) handleToFd' h h_@Handle__{haType=_,..} = do case cast haDevice of -- ...
haDevice should be a function. How could you cast it?
Note the .. in the record pattern. That is a language extension called RecordWildCards. It replaces each elided field f by the pattern f = f: http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#re...
3.
data Handle__ = forall dev enc_state dec_state . (IODevice dev, BufferedIO dev, Typeable dev) => Handle__ { -- ... } deriving Typeable
What's that "forall" thing?
Handle__ is an existentially quantified data constructors: http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions...
4.
handleToFd' h h_@Handle__{haType=_,..} = do case cast haDevice of Nothing -> -- ... Just fd -> do -- ... return (Handle__{haType=ClosedHandle,..}, Fd (fromIntegral (FD.fdFD fd)))
What's this ".." inside "Handle__{haType=ClosedHandle,..}"?
See answer of 2 Regards, Bas
participants (2)
-
Bas van Dijk
-
Volker Wysk