Skip to content

Instantly share code, notes, and snippets.

@cblp
Last active December 14, 2019 09:40
Show Gist options
  • Save cblp/7fef98ac1fff0eaf97339e905691db2c to your computer and use it in GitHub Desktop.
Save cblp/7fef98ac1fff0eaf97339e905691db2c to your computer and use it in GitHub Desktop.
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module A where
import Data.ByteString
import Language.C.Inline as C
C.context $ baseCtx <> bsCtx
C.include "<string.h>"
newtype Char20 = Char20 ByteString
data MyStruct = MyStruct
{ id :: Char20
, a :: Int
}
swing :: MyStruct -> IO ()
swing MyStruct{id = Char20 id_bs, a} =
[C.block| void {
typedef struct MyStruct {
unsigned char id[20];
int a;
} MyStruct;
void swing(MyStruct ms);
MyStruct ms = {.a = $(int a_c)};
strncpy((char*) ms.id, $bs-cstr:id_bs, sizeof ms.id);
swing(ms);
} |]
where
a_c = fromIntegral a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment