derive-storable

Derive Storable instances with GHC.Generics. https://www.github.com/mkloczko/derive-storable/

Latest on Hackage:0.1.0.6

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

MIT licensed by Mateusz Kloczko

Introduction

Build Status

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))

Note on performance

There are some problems with performance of derived Storable instances. For now there exists a solution in form of GHC Core plugin - 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

Changes

Revision history for derive-storable

0.1.0.5 -- 2016-12-07

  • Made the information about performance problems more visible in README.md

0.1.0.4 -- 2016-11-29

  • Fixed the bug with Foreign.Storable.Generic.Internal.Instances module.
  • Removed two tests related to numbering of fields.

0.1.0.3 -- 2016-09-19

  • Changed the link in README to hackage repository.

0.1.0.2 -- 2016-09-11

  • Changed generic-storable to derive-storable in README.md
  • Added README.md to the package

0.1.0.0 -- 2016-09-08

  • First version. Released on an unsuspecting world.
Depends on:
Used by 1 package:
comments powered byDisqus