Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added documentation

  • Loading branch information...
commit 03165fd96df7f6d562b4f1d8af79dbe4bf7dab2a 1 parent 992ac7a
Matthew Maurer authored
10 Foreign/CStorable.hs
View
@@ -1,5 +1,9 @@
+-- | This primarily exports the CStorable typeclass, which may have its
+-- methods automatically defaulted if it has a Generic instance.
+-- Then, this instance can be transfered via the `Storable' constructor.
module Foreign.CStorable
-(CStorable(..)
+(CStorable(..),
+ StorableWrap(..)
) where
import Foreign.CStorable.TypeClass
@@ -7,7 +11,11 @@ import Foreign.CStorable.BaseInstances
import Foreign.Storable
import Foreign.Ptr
+-- | Applying the `Storable' constructor to something which is Storable
+-- gives it a corresponding CStorable instance.
newtype StorableWrap a = Storable a
+
+-- | Translates a Storable instance to a CStorable instance
instance (Storable a) => CStorable (StorableWrap a) where
cPeek p = fmap Storable $ peek (castPtr p)
cPoke p (Storable x) = poke (castPtr p) x
1  Foreign/CStorable/BaseInstances.hs
View
@@ -1,3 +1,4 @@
+-- | Provides lots of bas instances, pulled over from `Storable'.
module Foreign.CStorable.BaseInstances where
import Data.Word
12 Foreign/CStorable/TypeClass.hs
View
@@ -1,10 +1,15 @@
{-# LANGUAGE DefaultSignatures #-}
+-- | This module provides the mechanical deriving
+-- mechanism for `CStorable'.
module Foreign.CStorable.TypeClass where
import Foreign.Ptr
import Foreign.Storable
import GHC.Generics
+-- | A wrapper class for the raw autoderivation functions,
+-- representing twhat is necessary for the defaulted
+-- `CStorable' methods.
class GCStorable a where
gcPeek :: Ptr (a x)-> IO (a x)
gcPoke :: Ptr (a x) -> a x -> IO ()
@@ -17,15 +22,20 @@ instance GCStorable U1 where
gcAlignment _ = 0
gcSizeOf _ = 0
+-- | Calculates extra space between two items based on alignment
+-- and size.
padding :: (GCStorable a, GCStorable b) => a x -> b y -> Int
padding a b = let
sizeA = gcSizeOf a
alignB = gcAlignment b
in ((alignB - sizeA) `mod` alignB)
+-- | Calculates the total space consumed by a given element, including
+-- alignment padding.
offset :: (GCStorable a, GCStorable b) => a x -> b y -> Int
offset a b = padding a b + gcSizeOf a
+-- | Test
instance (GCStorable a, GCStorable b) => GCStorable (a :*: b) where
gcPeek p = do
a <- gcPeek $ castPtr p
@@ -53,6 +63,8 @@ instance (CStorable a) => GCStorable (K1 i a) where
gcAlignment (K1 x) = cAlignment x
gcSizeOf (K1 x) = cSizeOf x
+-- | This typeclass is basically just a duplicate of `Storable'. It exists
+-- because I can't easily modify `Storable', as it is part of base.
class CStorable a where
cPeek :: Ptr a -> IO a
default cPeek :: (Generic a, GCStorable (Rep a)) => Ptr a -> IO a
Please sign in to comment.
Something went wrong with that request. Please try again.