derive-storable: Derive Storable instances with help of GHC.Generics.

[ foreign, library, mit ] [ Propose Tags ]

The package allows for automatic derivation of Storable instances with C-like memory layout.


[Skip to Readme]
Versions [faq] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4, 0.1.0.5, 0.1.0.6, 0.1.1.0, 0.1.1.1, 0.1.2.0
Change log ChangeLog.md
Dependencies base (>=4.8 && <4.10) [details]
License MIT
Author Mateusz Kloczko
Maintainer mateusz.p.kloczko@gmail.com
Category FFI
Home page https://www.github.com/mkloczko/derive-storable/
Uploaded by mkloczko at Sun Sep 11 13:03:34 UTC 2016
Distributions NixOS:0.1.2.0
Downloads 3387 total (178 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs uploaded by user
Build status unknown [no reports yet]

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for derive-storable-0.1.0.2

[back to package description]

Introduction

The derive-storable package allows you to automatically generate Storable instances for your datatypes. It uses GHC.Generics, which allows the coders to derive certain instances automatically. To derive a (G)Storable instance, the data-type has to:

  • have only one constructor.
  • all fields of the constructor need to be GStorable.
  • implement a Generic instance (derive (Generic))

In order to achieve the same performance as for hand-written instances, take a look at derive-storable-plugin.

Usage

Here's an example:

{-# LANGUAGE DeriveGeneric #-}

import Foreign.Storable
import Foreign.Storable.Generic
import Foreign.Ptr
import Foreign.Marshal.Alloc

import Generics.Deriving

data Position = Position {
   x :: Double, 
   y :: Double
} deriving (Show,Read, Generic)

instance GStorable Position

updatePosition :: Ptr Position -> Position -> IO ()
updatePosition ptr pos = poke ptr pos


main = do
    let val = Position 0.0 10.0
    ptr <- malloc :: IO (Ptr Position)      
    putStrLn "Created a ptr with value of"
    putStrLn =<< show <$> peek ptr
    updatePosition ptr val
    putStrLn "And now the value of ptr is:"   
    putStrLn =<< show <$> peek ptr