haskell / c2hs

c2hs is a pre-processor for Haskell FFI bindings to C libraries
http://hackage.haskell.org/package/c2hs
Other
197 stars 50 forks source link

Automatically generate Storable instances for enums #248

Open poscat0x04 opened 4 years ago

poscat0x04 commented 4 years ago

Enums are CInts in memory so this should work for all types

instance Storable SomeType where
    sizeOf _ = 4
    alignment _ = 4
    peek ptr = do
        val <- peek (castPtr ptr)
        return $ toEnum $ fromEnum (val :: CInt)
    poke ptr v = poke (castPtr ptr) (toEnum $ fromEnum v :: CInt)

It would be nice if we add syntactic sugar for automatically generating storables instances.