Add tree example

This commit is contained in:
George Thomas 2026-02-19 20:44:04 +00:00
parent 06767e17ea
commit 55a781eeb8
8 changed files with 588 additions and 12 deletions

View File

@ -3,10 +3,12 @@
module GarnetRs.Wrapped (
T (..),
Shape (..),
BTree (..),
hello,
helloStruct,
helloShape,
add,
sumTree,
) where
import Data.ByteString
@ -32,6 +34,25 @@ convertShape = \case
Circle r -> Raw.Shape Raw.Circle $ Raw.set_shape_body_circle $ Raw.Circle_Body r
Rectangle w h -> Raw.Shape Raw.Rectangle $ Raw.set_shape_body_rectangle $ Raw.Rectangle_Body w h
data BTree a
= Leaf a
| Fork (BTree a) (BTree a)
withBTree :: BTree Int64 -> (Raw.BTree -> IO a) -> IO a
withBTree = flip \f -> \case
Leaf v ->
f $ Raw.BTree Raw.Leaf $ Raw.set_bTree_body_leaf $ Raw.Leaf_Body v
Fork l r ->
withBTree l \lRaw ->
withBTree r \rRaw ->
alloca \lPtr ->
alloca \rPtr -> do
poke lPtr lRaw
poke rPtr rRaw
f
. Raw.BTree Raw.Fork
. Raw.set_bTree_body_fork
$ Raw.Fork_Body (unsafeFromPtr lPtr) (unsafeFromPtr rPtr)
hello :: ByteString -> IO ()
hello s = useAsCString s $ Raw.hello . unsafeFromPtr
@ -43,3 +64,6 @@ helloShape = Raw.hello_shape . convertShape
add :: Int64 -> Int64 -> Int64
add = Raw.add
sumTree :: BTree Int64 -> IO Int64
sumTree = flip withBTree Raw.sum_tree

View File

@ -10,3 +10,4 @@ main = do
helloShape $ Circle 3.14
helloShape $ Rectangle 10.0 5.0
putStrLn $ "3 + 4 = " <> show (add 3 4)
putStrLn . ("Tree sum: " <>) . show =<< sumTree (Fork (Fork (Leaf 1) (Fork (Leaf 2) (Leaf 3))) (Leaf 4))

View File

@ -34,6 +34,7 @@ import qualified HsBindgen.Runtime.Internal.HasFFIType
import qualified HsBindgen.Runtime.Internal.SizedByteArray
import qualified HsBindgen.Runtime.LibC
import qualified HsBindgen.Runtime.Marshal
import qualified HsBindgen.Runtime.PtrConst
import qualified Text.Read
import HsBindgen.Runtime.Internal.TypeEquality (TyEq)
import Prelude ((<*>), (>>), Eq, Int, Ord, Read, Show, pure, showsPrec)
@ -524,3 +525,417 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Shape) "body")
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"body")
{-| __C declaration:__ @enum BTree_Tag@
__defined at:__ @garnet_rs.h 33:6@
__exported by:__ @garnet_rs.h@
-}
newtype BTree_Tag = BTree_Tag
{ unwrap :: FC.CUInt
}
deriving stock (GHC.Generics.Generic)
deriving stock (Eq, Ord)
deriving newtype (HsBindgen.Runtime.Internal.HasFFIType.HasFFIType)
instance HsBindgen.Runtime.Marshal.StaticSize BTree_Tag where
staticSizeOf = \_ -> (4 :: Int)
staticAlignment = \_ -> (4 :: Int)
instance HsBindgen.Runtime.Marshal.ReadRaw BTree_Tag where
readRaw =
\ptr0 ->
pure BTree_Tag
<*> HsBindgen.Runtime.Marshal.readRawByteOff ptr0 (0 :: Int)
instance HsBindgen.Runtime.Marshal.WriteRaw BTree_Tag where
writeRaw =
\ptr0 ->
\s1 ->
case s1 of
BTree_Tag unwrap2 ->
HsBindgen.Runtime.Marshal.writeRawByteOff ptr0 (0 :: Int) unwrap2
deriving via HsBindgen.Runtime.Marshal.EquivStorable BTree_Tag instance F.Storable BTree_Tag
deriving via FC.CUInt instance Data.Primitive.Types.Prim BTree_Tag
instance HsBindgen.Runtime.CEnum.CEnum BTree_Tag where
type CEnumZ BTree_Tag = FC.CUInt
toCEnum = BTree_Tag
fromCEnum = GHC.Records.getField @"unwrap"
declaredValues =
\_ ->
HsBindgen.Runtime.CEnum.declaredValuesFromList [ (0, Data.List.NonEmpty.singleton "Leaf")
, (1, Data.List.NonEmpty.singleton "Fork")
]
showsUndeclared =
HsBindgen.Runtime.CEnum.showsWrappedUndeclared "BTree_Tag"
readPrecUndeclared =
HsBindgen.Runtime.CEnum.readPrecWrappedUndeclared "BTree_Tag"
isDeclared = HsBindgen.Runtime.CEnum.seqIsDeclared
mkDeclared = HsBindgen.Runtime.CEnum.seqMkDeclared
instance HsBindgen.Runtime.CEnum.SequentialCEnum BTree_Tag where
minDeclaredValue = Leaf
maxDeclaredValue = Fork
instance Show BTree_Tag where
showsPrec = HsBindgen.Runtime.CEnum.shows
instance Read BTree_Tag where
readPrec = HsBindgen.Runtime.CEnum.readPrec
readList = Text.Read.readListDefault
readListPrec = Text.Read.readListPrecDefault
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree_Tag) "unwrap")
) => GHC.Records.HasField "unwrap" (Ptr.Ptr BTree_Tag) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"unwrap")
instance HsBindgen.Runtime.HasCField.HasCField BTree_Tag "unwrap" where
type CFieldType BTree_Tag "unwrap" = FC.CUInt
offset# = \_ -> \_ -> 0
{-| __C declaration:__ @Leaf@
__defined at:__ @garnet_rs.h 34:3@
__exported by:__ @garnet_rs.h@
-}
pattern Leaf :: BTree_Tag
pattern Leaf = BTree_Tag 0
{-| __C declaration:__ @Fork@
__defined at:__ @garnet_rs.h 35:3@
__exported by:__ @garnet_rs.h@
-}
pattern Fork :: BTree_Tag
pattern Fork = BTree_Tag 1
{-| __C declaration:__ @struct Leaf_Body@
__defined at:__ @garnet_rs.h 38:8@
__exported by:__ @garnet_rs.h@
-}
data Leaf_Body = Leaf_Body
{ value :: HsBindgen.Runtime.LibC.Int64
{- ^ __C declaration:__ @value@
__defined at:__ @garnet_rs.h 39:11@
__exported by:__ @garnet_rs.h@
-}
}
deriving stock (GHC.Generics.Generic)
deriving stock (Eq, Show)
instance HsBindgen.Runtime.Marshal.StaticSize Leaf_Body where
staticSizeOf = \_ -> (8 :: Int)
staticAlignment = \_ -> (8 :: Int)
instance HsBindgen.Runtime.Marshal.ReadRaw Leaf_Body where
readRaw =
\ptr0 ->
pure Leaf_Body
<*> HsBindgen.Runtime.HasCField.readRaw (Data.Proxy.Proxy @"value") ptr0
instance HsBindgen.Runtime.Marshal.WriteRaw Leaf_Body where
writeRaw =
\ptr0 ->
\s1 ->
case s1 of
Leaf_Body value2 ->
HsBindgen.Runtime.HasCField.writeRaw (Data.Proxy.Proxy @"value") ptr0 value2
deriving via HsBindgen.Runtime.Marshal.EquivStorable Leaf_Body instance F.Storable Leaf_Body
instance HsBindgen.Runtime.HasCField.HasCField Leaf_Body "value" where
type CFieldType Leaf_Body "value" =
HsBindgen.Runtime.LibC.Int64
offset# = \_ -> \_ -> 0
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Leaf_Body) "value")
) => GHC.Records.HasField "value" (Ptr.Ptr Leaf_Body) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"value")
{-| __C declaration:__ @struct Fork_Body@
__defined at:__ @garnet_rs.h 42:8@
__exported by:__ @garnet_rs.h@
-}
data Fork_Body = Fork_Body
{ left :: HsBindgen.Runtime.PtrConst.PtrConst BTree
{- ^ __C declaration:__ @left@
__defined at:__ @garnet_rs.h 43:23@
__exported by:__ @garnet_rs.h@
-}
, right :: HsBindgen.Runtime.PtrConst.PtrConst BTree
{- ^ __C declaration:__ @right@
__defined at:__ @garnet_rs.h 44:23@
__exported by:__ @garnet_rs.h@
-}
}
deriving stock (GHC.Generics.Generic)
deriving stock (Eq, Show)
instance HsBindgen.Runtime.Marshal.StaticSize Fork_Body where
staticSizeOf = \_ -> (16 :: Int)
staticAlignment = \_ -> (8 :: Int)
instance HsBindgen.Runtime.Marshal.ReadRaw Fork_Body where
readRaw =
\ptr0 ->
pure Fork_Body
<*> HsBindgen.Runtime.HasCField.readRaw (Data.Proxy.Proxy @"left") ptr0
<*> HsBindgen.Runtime.HasCField.readRaw (Data.Proxy.Proxy @"right") ptr0
instance HsBindgen.Runtime.Marshal.WriteRaw Fork_Body where
writeRaw =
\ptr0 ->
\s1 ->
case s1 of
Fork_Body left2 right3 ->
HsBindgen.Runtime.HasCField.writeRaw (Data.Proxy.Proxy @"left") ptr0 left2
>> HsBindgen.Runtime.HasCField.writeRaw (Data.Proxy.Proxy @"right") ptr0 right3
deriving via HsBindgen.Runtime.Marshal.EquivStorable Fork_Body instance F.Storable Fork_Body
instance HsBindgen.Runtime.HasCField.HasCField Fork_Body "left" where
type CFieldType Fork_Body "left" =
HsBindgen.Runtime.PtrConst.PtrConst BTree
offset# = \_ -> \_ -> 0
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Fork_Body) "left")
) => GHC.Records.HasField "left" (Ptr.Ptr Fork_Body) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"left")
instance HsBindgen.Runtime.HasCField.HasCField Fork_Body "right" where
type CFieldType Fork_Body "right" =
HsBindgen.Runtime.PtrConst.PtrConst BTree
offset# = \_ -> \_ -> 8
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Fork_Body) "right")
) => GHC.Records.HasField "right" (Ptr.Ptr Fork_Body) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"right")
{-| __C declaration:__ @union \@BTree_body@
__defined at:__ @garnet_rs.h 49:3@
__exported by:__ @garnet_rs.h@
-}
newtype BTree_body = BTree_body
{ unwrap :: Data.Array.Byte.ByteArray
}
deriving stock (GHC.Generics.Generic)
deriving via (HsBindgen.Runtime.Internal.SizedByteArray.SizedByteArray 16) 8 instance HsBindgen.Runtime.Marshal.StaticSize BTree_body
deriving via (HsBindgen.Runtime.Internal.SizedByteArray.SizedByteArray 16) 8 instance HsBindgen.Runtime.Marshal.ReadRaw BTree_body
deriving via (HsBindgen.Runtime.Internal.SizedByteArray.SizedByteArray 16) 8 instance HsBindgen.Runtime.Marshal.WriteRaw BTree_body
deriving via HsBindgen.Runtime.Marshal.EquivStorable BTree_body instance F.Storable BTree_body
{-|
__See:__ 'set_bTree_body_leaf'
__C declaration:__ @leaf@
__defined at:__ @garnet_rs.h 50:22@
__exported by:__ @garnet_rs.h@
-}
get_bTree_body_leaf ::
BTree_body
-> Leaf_Body
get_bTree_body_leaf =
HsBindgen.Runtime.Internal.ByteArray.getUnionPayload
{-|
__See:__ 'get_bTree_body_leaf'
-}
set_bTree_body_leaf ::
Leaf_Body
-> BTree_body
set_bTree_body_leaf =
HsBindgen.Runtime.Internal.ByteArray.setUnionPayload
{-|
__See:__ 'set_bTree_body_fork'
__C declaration:__ @fork@
__defined at:__ @garnet_rs.h 51:22@
__exported by:__ @garnet_rs.h@
-}
get_bTree_body_fork ::
BTree_body
-> Fork_Body
get_bTree_body_fork =
HsBindgen.Runtime.Internal.ByteArray.getUnionPayload
{-|
__See:__ 'get_bTree_body_fork'
-}
set_bTree_body_fork ::
Fork_Body
-> BTree_body
set_bTree_body_fork =
HsBindgen.Runtime.Internal.ByteArray.setUnionPayload
instance HsBindgen.Runtime.HasCField.HasCField BTree_body "leaf" where
type CFieldType BTree_body "leaf" = Leaf_Body
offset# = \_ -> \_ -> 0
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree_body) "leaf")
) => GHC.Records.HasField "leaf" (Ptr.Ptr BTree_body) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"leaf")
instance HsBindgen.Runtime.HasCField.HasCField BTree_body "fork" where
type CFieldType BTree_body "fork" = Fork_Body
offset# = \_ -> \_ -> 0
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree_body) "fork")
) => GHC.Records.HasField "fork" (Ptr.Ptr BTree_body) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"fork")
{-| __C declaration:__ @struct BTree@
__defined at:__ @garnet_rs.h 47:8@
__exported by:__ @garnet_rs.h@
-}
data BTree = BTree
{ tag :: BTree_Tag
{- ^ __C declaration:__ @tag@
__defined at:__ @garnet_rs.h 48:18@
__exported by:__ @garnet_rs.h@
-}
, body :: BTree_body
{- ^ __C declaration:__ @body@
__defined at:__ @garnet_rs.h 52:5@
__exported by:__ @garnet_rs.h@
-}
}
deriving stock (GHC.Generics.Generic)
instance HsBindgen.Runtime.Marshal.StaticSize BTree where
staticSizeOf = \_ -> (24 :: Int)
staticAlignment = \_ -> (8 :: Int)
instance HsBindgen.Runtime.Marshal.ReadRaw BTree where
readRaw =
\ptr0 ->
pure BTree
<*> HsBindgen.Runtime.HasCField.readRaw (Data.Proxy.Proxy @"tag") ptr0
<*> HsBindgen.Runtime.HasCField.readRaw (Data.Proxy.Proxy @"body") ptr0
instance HsBindgen.Runtime.Marshal.WriteRaw BTree where
writeRaw =
\ptr0 ->
\s1 ->
case s1 of
BTree tag2 body3 ->
HsBindgen.Runtime.HasCField.writeRaw (Data.Proxy.Proxy @"tag") ptr0 tag2
>> HsBindgen.Runtime.HasCField.writeRaw (Data.Proxy.Proxy @"body") ptr0 body3
deriving via HsBindgen.Runtime.Marshal.EquivStorable BTree instance F.Storable BTree
instance HsBindgen.Runtime.HasCField.HasCField BTree "tag" where
type CFieldType BTree "tag" = BTree_Tag
offset# = \_ -> \_ -> 0
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree) "tag")
) => GHC.Records.HasField "tag" (Ptr.Ptr BTree) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"tag")
instance HsBindgen.Runtime.HasCField.HasCField BTree "body" where
type CFieldType BTree "body" = BTree_body
offset# = \_ -> \_ -> 8
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree) "body")
) => GHC.Records.HasField "body" (Ptr.Ptr BTree) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"body")

View File

@ -51,6 +51,14 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "{"
, " return &add;"
, "}"
, "/* com_garnet_GarnetRs_get_sum_tree */"
, "__attribute__ ((const))"
, "int64_t (*hs_bindgen_473a3e791275b06d (void)) ("
, " struct BTree arg1"
, ")"
, "{"
, " return &sum_tree;"
, "}"
]))
-- __unique:__ @com_garnet_GarnetRs_get_hello@
@ -65,7 +73,7 @@ hs_bindgen_faf62265b53521d3 =
{-# NOINLINE hello #-}
{-| __C declaration:__ @hello@
__defined at:__ @garnet_rs.h 33:6@
__defined at:__ @garnet_rs.h 55:6@
__exported by:__ @garnet_rs.h@
-}
@ -85,7 +93,7 @@ hs_bindgen_0f8c37ef19b17a6d =
{-# NOINLINE hello_struct #-}
{-| __C declaration:__ @hello_struct@
__defined at:__ @garnet_rs.h 35:6@
__defined at:__ @garnet_rs.h 57:6@
__exported by:__ @garnet_rs.h@
-}
@ -105,7 +113,7 @@ hs_bindgen_287ff3ac660f333b =
{-# NOINLINE hello_shape #-}
{-| __C declaration:__ @hello_shape@
__defined at:__ @garnet_rs.h 37:6@
__defined at:__ @garnet_rs.h 59:6@
__exported by:__ @garnet_rs.h@
-}
@ -125,10 +133,30 @@ hs_bindgen_bbabdbe61cd1eeb2 =
{-# NOINLINE add #-}
{-| __C declaration:__ @add@
__defined at:__ @garnet_rs.h 39:32@
__defined at:__ @garnet_rs.h 61:32@
__exported by:__ @garnet_rs.h@
-}
add :: Ptr.FunPtr (HsBindgen.Runtime.LibC.Int64 -> HsBindgen.Runtime.LibC.Int64 -> IO HsBindgen.Runtime.LibC.Int64)
add =
GHC.IO.Unsafe.unsafePerformIO hs_bindgen_bbabdbe61cd1eeb2
-- __unique:__ @com_garnet_GarnetRs_get_sum_tree@
foreign import ccall unsafe "hs_bindgen_473a3e791275b06d" hs_bindgen_473a3e791275b06d_base ::
IO (Ptr.FunPtr Void)
-- __unique:__ @com_garnet_GarnetRs_get_sum_tree@
hs_bindgen_473a3e791275b06d :: IO (Ptr.FunPtr (BTree -> IO HsBindgen.Runtime.LibC.Int64))
hs_bindgen_473a3e791275b06d =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_473a3e791275b06d_base
{-# NOINLINE sum_tree #-}
{-| __C declaration:__ @sum_tree@
__defined at:__ @garnet_rs.h 63:9@
__exported by:__ @garnet_rs.h@
-}
sum_tree :: Ptr.FunPtr (BTree -> IO HsBindgen.Runtime.LibC.Int64)
sum_tree =
GHC.IO.Unsafe.unsafePerformIO hs_bindgen_473a3e791275b06d

View File

@ -44,6 +44,12 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "{"
, " return add(arg1, arg2);"
, "}"
, "int64_t hs_bindgen_9602640c06f0c62f ("
, " struct BTree *arg1"
, ")"
, "{"
, " return sum_tree(*arg1);"
, "}"
]))
-- __unique:__ @com_garnet_GarnetRs_Safe_hello@
@ -60,7 +66,7 @@ hs_bindgen_433ea2a26af4e593 =
{-| __C declaration:__ @hello@
__defined at:__ @garnet_rs.h 33:6@
__defined at:__ @garnet_rs.h 55:6@
__exported by:__ @garnet_rs.h@
-}
@ -84,7 +90,7 @@ hs_bindgen_51157946af5519c9 =
{-| __C declaration:__ @hello_struct@
__defined at:__ @garnet_rs.h 35:6@
__defined at:__ @garnet_rs.h 57:6@
__exported by:__ @garnet_rs.h@
-}
@ -110,7 +116,7 @@ hs_bindgen_7de06f1fd827ca60 =
{-| __C declaration:__ @hello_shape@
__defined at:__ @garnet_rs.h 37:6@
__defined at:__ @garnet_rs.h 59:6@
__exported by:__ @garnet_rs.h@
-}
@ -142,7 +148,7 @@ hs_bindgen_1c0c71fa74c428a9 =
__C declaration:__ @add@
__defined at:__ @garnet_rs.h 39:32@
__defined at:__ @garnet_rs.h 61:32@
__exported by:__ @garnet_rs.h@
-}
@ -153,3 +159,29 @@ add ::
-- ^ __C declaration:__ @b@
-> HsBindgen.Runtime.LibC.Int64
add = hs_bindgen_1c0c71fa74c428a9
-- __unique:__ @com_garnet_GarnetRs_Safe_sum_tree@
foreign import ccall safe "hs_bindgen_9602640c06f0c62f" hs_bindgen_9602640c06f0c62f_base ::
Ptr.Ptr Void
-> IO GHC.Int.Int64
-- __unique:__ @com_garnet_GarnetRs_Safe_sum_tree@
hs_bindgen_9602640c06f0c62f ::
Ptr.Ptr BTree
-> IO HsBindgen.Runtime.LibC.Int64
hs_bindgen_9602640c06f0c62f =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_9602640c06f0c62f_base
{-| __C declaration:__ @sum_tree@
__defined at:__ @garnet_rs.h 63:9@
__exported by:__ @garnet_rs.h@
-}
sum_tree ::
BTree
-- ^ __C declaration:__ @t@
-> IO HsBindgen.Runtime.LibC.Int64
sum_tree =
\t0 ->
F.with t0 (\t1 -> hs_bindgen_9602640c06f0c62f t1)

View File

@ -44,6 +44,12 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "{"
, " return add(arg1, arg2);"
, "}"
, "int64_t hs_bindgen_c2ced4f3ba39c8ff ("
, " struct BTree *arg1"
, ")"
, "{"
, " return sum_tree(*arg1);"
, "}"
]))
-- __unique:__ @com_garnet_GarnetRs_Unsafe_hello@
@ -60,7 +66,7 @@ hs_bindgen_2dfe97662a4d6377 =
{-| __C declaration:__ @hello@
__defined at:__ @garnet_rs.h 33:6@
__defined at:__ @garnet_rs.h 55:6@
__exported by:__ @garnet_rs.h@
-}
@ -84,7 +90,7 @@ hs_bindgen_29d823ada2bc7302 =
{-| __C declaration:__ @hello_struct@
__defined at:__ @garnet_rs.h 35:6@
__defined at:__ @garnet_rs.h 57:6@
__exported by:__ @garnet_rs.h@
-}
@ -110,7 +116,7 @@ hs_bindgen_b3f40a03f07eaa85 =
{-| __C declaration:__ @hello_shape@
__defined at:__ @garnet_rs.h 37:6@
__defined at:__ @garnet_rs.h 59:6@
__exported by:__ @garnet_rs.h@
-}
@ -142,7 +148,7 @@ hs_bindgen_20eb651f0a8faf48 =
__C declaration:__ @add@
__defined at:__ @garnet_rs.h 39:32@
__defined at:__ @garnet_rs.h 61:32@
__exported by:__ @garnet_rs.h@
-}
@ -153,3 +159,29 @@ add ::
-- ^ __C declaration:__ @b@
-> HsBindgen.Runtime.LibC.Int64
add = hs_bindgen_20eb651f0a8faf48
-- __unique:__ @com_garnet_GarnetRs_Unsafe_sum_tree@
foreign import ccall unsafe "hs_bindgen_c2ced4f3ba39c8ff" hs_bindgen_c2ced4f3ba39c8ff_base ::
Ptr.Ptr Void
-> IO GHC.Int.Int64
-- __unique:__ @com_garnet_GarnetRs_Unsafe_sum_tree@
hs_bindgen_c2ced4f3ba39c8ff ::
Ptr.Ptr BTree
-> IO HsBindgen.Runtime.LibC.Int64
hs_bindgen_c2ced4f3ba39c8ff =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_c2ced4f3ba39c8ff_base
{-| __C declaration:__ @sum_tree@
__defined at:__ @garnet_rs.h 63:9@
__exported by:__ @garnet_rs.h@
-}
sum_tree ::
BTree
-- ^ __C declaration:__ @t@
-> IO HsBindgen.Runtime.LibC.Int64
sum_tree =
\t0 ->
F.with t0 (\t1 -> hs_bindgen_c2ced4f3ba39c8ff t1)

View File

@ -30,6 +30,28 @@ struct Shape {
} body;
};
enum BTree_Tag {
Leaf,
Fork,
};
struct Leaf_Body {
int64_t value;
};
struct Fork_Body {
const struct BTree *left;
const struct BTree *right;
};
struct BTree {
enum BTree_Tag tag;
union {
struct Leaf_Body leaf;
struct Fork_Body fork;
} body;
};
void hello(const char *c);
void hello_struct(struct T t);
@ -37,3 +59,5 @@ void hello_struct(struct T t);
void hello_shape(struct Shape s);
__attribute__((const)) int64_t add(int64_t a, int64_t b);
int64_t sum_tree(struct BTree t);

View File

@ -40,3 +40,23 @@ extern "C" fn hello_shape(s: Shape) -> () {
extern "C" fn add(a: i64, b: i64) -> i64 {
a + b
}
#[repr(C)]
#[derive(Clone, Copy)]
enum BTree {
Leaf {
value: i64,
},
Fork {
left: *const BTree,
right: *const BTree,
},
}
#[unsafe(no_mangle)]
extern "C" fn sum_tree(t: BTree) -> i64 {
match t {
BTree::Leaf { value } => value,
BTree::Fork { left, right } => unsafe { sum_tree(*left) + sum_tree(*right) },
}
}