[C2hs] timespec and c2hs

Duncan Coutts duncan.coutts at googlemail.com
Thu Apr 21 12:05:07 CEST 2011


On Sat, 2010-12-11 at 16:23 +0100, Erik Hesselink wrote:
> Hi,
> 
> I'm trying to write a Storable instance for timespec using c2hs. Since
> timespec is defined something like:
> 
> struct timespec {
>   time_t tv_sec;
>   long tv_nsec;
> };
> 
> I've defined the following in Haskell:
> 
> #include <sys/time.h>
> 
> #c
> typedef struct timespec timespec_t;
> #endc
> 
> data TimeSpec = TimeSpec
>   { seconds :: CTime
>   , nanoseconds :: CLong
>   }
> 
> instance Storable TimeSpec where
>   peek t = TimeSpec <$> {#get timespec_t->tv_sec #} t <*> {#get
> timespec_t->tv_nsec#} t
>   ...
> 
> However, this fails to compile. For tv_sec, c2hs generates
> "peekByteOff ptr 0 :: IO CLong" instead of "IO CTime". Why is this? Am
> I doing something wrong, or is there a bug/missing feature in c2hs?

It's because c2hs can see that time_t is in fact a typedef for long, and
long translates to CLong.

What is missing is a way to tell c2hs that we want to translate certain
C typedefs to specific Haskell types, and not to chase the typedef chain
all the way down to a primitive type.

Duncan




More information about the C2hs mailing list