storable-record 0.0.4.1 → 0.0.5
raw patch · 3 files changed
+119/−3 lines, 3 filesdep ~utility-ht
Dependency ranges changed: utility-ht
Files
- src/Foreign/Storable/Record/Tuple.hs +103/−0
- src/Foreign/Storable/Traversable.hs +12/−0
- storable-record.cabal +4/−3
+ src/Foreign/Storable/Record/Tuple.hs view
@@ -0,0 +1,103 @@+{- |+Custom class for storing tuples+and wrapper for storing tuples in standard 'Foreign.Storable' class.+These two solutions do not need orphan instances.+The package @storable-tuple@ makes use of this implementation.+-}+module Foreign.Storable.Record.Tuple (+ Storable(..),+ Tuple(..),+ ) where++import qualified Foreign.Storable.Record as Record+import qualified Foreign.Storable as Store+import Foreign.Ptr (Ptr, castPtr)++import qualified Control.Applicative.HT as App+import Data.Tuple.HT (fst3, snd3, thd3)+++newtype Tuple a = Tuple {getTuple :: a}+ deriving (Eq, Show)++instance Storable a => Store.Storable (Tuple a) where+ sizeOf = sizeOf . getTuple+ alignment = alignment . getTuple+ peek = fmap Tuple . peek . castPtr+ poke ptr = poke (castPtr ptr) . getTuple+++class Storable a where+ sizeOf :: a -> Int+ alignment :: a -> Int+ peek :: Ptr a -> IO a+ poke :: Ptr a -> a -> IO ()++instance (Store.Storable a, Store.Storable b) => Storable (a,b) where+ sizeOf = Record.sizeOf storePair+ alignment = Record.alignment storePair+ peek = Record.peek storePair+ poke = Record.poke storePair++{-# INLINE storePair #-}+storePair ::+ (Store.Storable a, Store.Storable b) =>+ Record.Dictionary (a,b)+storePair =+ Record.run $+ App.lift2 (,)+ (Record.element fst)+ (Record.element snd)+++instance+ (Store.Storable a, Store.Storable b, Store.Storable c) =>+ Storable (a,b,c) where+ sizeOf = Record.sizeOf storeTriple+ alignment = Record.alignment storeTriple+ peek = Record.peek storeTriple+ poke = Record.poke storeTriple++{-# INLINE storeTriple #-}+storeTriple ::+ (Store.Storable a, Store.Storable b, Store.Storable c) =>+ Record.Dictionary (a,b,c)+storeTriple =+ Record.run $+ App.lift3 (,,)+ (Record.element fst3)+ (Record.element snd3)+ (Record.element thd3)++instance+ (Store.Storable a, Store.Storable b, Store.Storable c, Store.Storable d) =>+ Storable (a,b,c,d) where+ sizeOf = Record.sizeOf storeQuadruple+ alignment = Record.alignment storeQuadruple+ peek = Record.peek storeQuadruple+ poke = Record.poke storeQuadruple++{-# INLINE storeQuadruple #-}+storeQuadruple ::+ (Store.Storable a, Store.Storable b, Store.Storable c, Store.Storable d) =>+ Record.Dictionary (a,b,c,d)+storeQuadruple =+ Record.run $+ App.lift4 (,,,)+ (Record.element $ \(x,_,_,_) -> x)+ (Record.element $ \(_,x,_,_) -> x)+ (Record.element $ \(_,_,x,_) -> x)+ (Record.element $ \(_,_,_,x) -> x)+++{-+{- Why is this allowed? -}+test :: Char+test = const 'a' undefined++{- Why is type defaulting applied here? The type of 'c' should be fixed. -}+test1 :: (Integral a, RealField.C a) => a+test1 =+ let c = undefined+ in asTypeOf (round c) c+-}
src/Foreign/Storable/Traversable.hs view
@@ -66,6 +66,18 @@ alignment = St.alignment . elementType {-# INLINE sizeOf #-}+{- |+Warning:+It uses Foldable class and will certainly access the data structure+and thus will fail on 'undefined'.++You may call it on the record, after re-constructing it lazily:++> sizeOf . lazy+>+> lazy :: Complex a -> Complex a+> lazy ~(r:+i) = r:+i+-} sizeOf :: (Fold.Foldable f, Storable a) => f a -> Int
storable-record.cabal view
@@ -1,5 +1,5 @@ Name: storable-record-Version: 0.0.4.1+Version: 0.0.5 Category: Data, Foreign Synopsis: Elegant definition of Storable instances for records Description:@@ -63,7 +63,7 @@ Source-Repository this Type: darcs Location: http://code.haskell.org/~thielema/storable-record/- Tag: 0.0.4.1+ Tag: 0.0.5 Flag splitBase description: Choose the new smaller, split-up base package.@@ -76,7 +76,7 @@ Build-Depends: transformers >=0.2 && <0.6, semigroups >=0.1 && <1.0,- utility-ht >=0.0.1 && <0.1+ utility-ht >=0.0.14 && <0.1 If flag(splitBase) Build-Depends: base >= 3 && < 6@@ -90,6 +90,7 @@ Exposed-Modules: Foreign.Storable.Record+ Foreign.Storable.Record.Tuple Foreign.Storable.Newtype Foreign.Storable.Traversable Foreign.Storable.FixedArray