Last active
December 14, 2019 09:40
-
-
Save cblp/7fef98ac1fff0eaf97339e905691db2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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