{-# LANGUAGE ForeignFunctionInterface #-}
#include "foo.h"
module Foo where
import Foreign.C.Types
import Foreign.Marshal
import Foreign.Ptr
import Foreign.Storable
data MyStringUtf8 = MyStringUtf8
{ str'MyStringUtf8 :: Ptr CChar
, length'MyStringUtf8 :: CSize
}
{#pointer *my_string_utf8_t as MyStringUtf8Ptr -> MyStringUtf8#}
instance Storable MyStringUtf8 where
sizeOf _ = {#sizeof my_string_utf8_t#}
alignment _ = {#alignof my_string_utf8_t#}
peek p = MyStringUtf8
<$> {#get my_string_utf8_t->str#} p
<*> {#get my_string_utf8_t->length#} p
poke p x = do
{#set my_string_utf8_t->str#} p (str'MyStringUtf8 x)
{#set my_string_utf8_t->length#} p (length'MyStringUtf8 x)
type MyString = MyStringUtf8
{#pointer *my_string_t as MyStringPtr -> MyString#}
{#fun unsafe my_string_free as ^ {id `MyStringPtr'} -> `()' #}
{#fun unsafe my_string_utf8_free as ^ {id `MyStringUtf8Ptr'} -> `()' #}
I created a Haskell type alias MyString and used its pointer type, MyStringPtr in defining the function hook for my_string_free. However, the binding for my_string_free fails to recognize my_string_t type and used Ptr () instead of MyStringPtr. It seems c2hs cannot recognize the C type alias typedef my_string_utf8_t my_string_t;.
Here is a snippet of the C header file that I want to create a binding for:
my_string_t
is a type alias formy_string_utf8_t
.I created a Haskell type alias
MyString
and used its pointer type,MyStringPtr
in defining the function hook formy_string_free
. However, the binding formy_string_free
fails to recognizemy_string_t
type and usedPtr ()
instead ofMyStringPtr
. It seems c2hs cannot recognize the C type aliastypedef my_string_utf8_t my_string_t;
.The binding
for my_string_utf8_free
is created as I expected:I am not sure why c2hs used
MyStringPtr
instead ofMyStringUtf8Ptr
though.