Simplify use sites by passing by value

This commit is contained in:
George Thomas 2026-02-19 11:15:38 +00:00
parent 5f1e49c4ce
commit 13b9324bc4
6 changed files with 43 additions and 41 deletions

View File

@ -1,22 +1,14 @@
module Main (main) where module Main (main) where
import Data.ByteString import Data.ByteString
import Foreign
import Foreign.C import Foreign.C
import GarnetRs import GarnetRs
import GarnetRs.Safe import GarnetRs.Safe
import HsBindgen.Runtime.Marshal
import HsBindgen.Runtime.PtrConst import HsBindgen.Runtime.PtrConst
main :: IO () main :: IO ()
main = do main = do
useAsCString "Haskell" $ hello . unsafeFromPtr useAsCString "Haskell" $ hello . unsafeFromPtr
alloca \ptr -> do hello_struct T{a = CBool 1, b = 42}
writeRaw ptr T{a = CBool 1, b = 42} hello_shape $ Shape (Shape_Tag 0) $ set_shape_body_circle $ Circle_Body 3.14
hello_struct (unsafeFromPtr ptr) hello_shape $ Shape (Shape_Tag 1) $ set_shape_body_rectangle $ Rectangle_Body 10.0 5.0
alloca \ptr -> do
writeRaw ptr (Shape (Shape_Tag 0) (set_shape_body_circle (Circle_Body 3.14)))
hello_shape (unsafeFromPtr ptr)
alloca \ptr -> do
writeRaw ptr (Shape (Shape_Tag 1) (set_shape_body_rectangle (Rectangle_Body 10.0 5.0)))
hello_shape (unsafeFromPtr ptr)

View File

@ -28,7 +28,7 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "/* com_garnet_GarnetRs_get_hello_struct */" , "/* com_garnet_GarnetRs_get_hello_struct */"
, "__attribute__ ((const))" , "__attribute__ ((const))"
, "void (*hs_bindgen_0f8c37ef19b17a6d (void)) (" , "void (*hs_bindgen_0f8c37ef19b17a6d (void)) ("
, " struct T const *arg1" , " struct T arg1"
, ")" , ")"
, "{" , "{"
, " return &hello_struct;" , " return &hello_struct;"
@ -36,7 +36,7 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "/* com_garnet_GarnetRs_get_hello_shape */" , "/* com_garnet_GarnetRs_get_hello_shape */"
, "__attribute__ ((const))" , "__attribute__ ((const))"
, "void (*hs_bindgen_287ff3ac660f333b (void)) (" , "void (*hs_bindgen_287ff3ac660f333b (void)) ("
, " struct Shape const *arg1" , " struct Shape arg1"
, ")" , ")"
, "{" , "{"
, " return &hello_shape;" , " return &hello_shape;"
@ -68,7 +68,7 @@ foreign import ccall unsafe "hs_bindgen_0f8c37ef19b17a6d" hs_bindgen_0f8c37ef19b
IO (Ptr.FunPtr Void) IO (Ptr.FunPtr Void)
-- __unique:__ @com_garnet_GarnetRs_get_hello_struct@ -- __unique:__ @com_garnet_GarnetRs_get_hello_struct@
hs_bindgen_0f8c37ef19b17a6d :: IO (Ptr.FunPtr ((HsBindgen.Runtime.PtrConst.PtrConst T) -> IO ())) hs_bindgen_0f8c37ef19b17a6d :: IO (Ptr.FunPtr (T -> IO ()))
hs_bindgen_0f8c37ef19b17a6d = hs_bindgen_0f8c37ef19b17a6d =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_0f8c37ef19b17a6d_base HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_0f8c37ef19b17a6d_base
@ -79,7 +79,7 @@ hs_bindgen_0f8c37ef19b17a6d =
__exported by:__ @garnet_rs.h@ __exported by:__ @garnet_rs.h@
-} -}
hello_struct :: Ptr.FunPtr ((HsBindgen.Runtime.PtrConst.PtrConst T) -> IO ()) hello_struct :: Ptr.FunPtr (T -> IO ())
hello_struct = hello_struct =
GHC.IO.Unsafe.unsafePerformIO hs_bindgen_0f8c37ef19b17a6d GHC.IO.Unsafe.unsafePerformIO hs_bindgen_0f8c37ef19b17a6d
@ -88,7 +88,7 @@ foreign import ccall unsafe "hs_bindgen_287ff3ac660f333b" hs_bindgen_287ff3ac660
IO (Ptr.FunPtr Void) IO (Ptr.FunPtr Void)
-- __unique:__ @com_garnet_GarnetRs_get_hello_shape@ -- __unique:__ @com_garnet_GarnetRs_get_hello_shape@
hs_bindgen_287ff3ac660f333b :: IO (Ptr.FunPtr ((HsBindgen.Runtime.PtrConst.PtrConst Shape) -> IO ())) hs_bindgen_287ff3ac660f333b :: IO (Ptr.FunPtr (Shape -> IO ()))
hs_bindgen_287ff3ac660f333b = hs_bindgen_287ff3ac660f333b =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_287ff3ac660f333b_base HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_287ff3ac660f333b_base
@ -99,6 +99,6 @@ hs_bindgen_287ff3ac660f333b =
__exported by:__ @garnet_rs.h@ __exported by:__ @garnet_rs.h@
-} -}
hello_shape :: Ptr.FunPtr ((HsBindgen.Runtime.PtrConst.PtrConst Shape) -> IO ()) hello_shape :: Ptr.FunPtr (Shape -> IO ())
hello_shape = hello_shape =
GHC.IO.Unsafe.unsafePerformIO hs_bindgen_287ff3ac660f333b GHC.IO.Unsafe.unsafePerformIO hs_bindgen_287ff3ac660f333b

View File

@ -5,6 +5,7 @@
module GarnetRs.Safe where module GarnetRs.Safe where
import qualified Foreign as F
import qualified Foreign.C as FC import qualified Foreign.C as FC
import qualified GHC.Ptr as Ptr import qualified GHC.Ptr as Ptr
import qualified HsBindgen.Runtime.Internal.CAPI import qualified HsBindgen.Runtime.Internal.CAPI
@ -23,16 +24,16 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, " hello(arg1);" , " hello(arg1);"
, "}" , "}"
, "void hs_bindgen_51157946af5519c9 (" , "void hs_bindgen_51157946af5519c9 ("
, " struct T const *arg1" , " struct T *arg1"
, ")" , ")"
, "{" , "{"
, " hello_struct(arg1);" , " hello_struct(*arg1);"
, "}" , "}"
, "void hs_bindgen_7de06f1fd827ca60 (" , "void hs_bindgen_7de06f1fd827ca60 ("
, " struct Shape const *arg1" , " struct Shape *arg1"
, ")" , ")"
, "{" , "{"
, " hello_shape(arg1);" , " hello_shape(*arg1);"
, "}" , "}"
])) ]))
@ -67,7 +68,7 @@ foreign import ccall safe "hs_bindgen_51157946af5519c9" hs_bindgen_51157946af551
-- __unique:__ @com_garnet_GarnetRs_Safe_hello_struct@ -- __unique:__ @com_garnet_GarnetRs_Safe_hello_struct@
hs_bindgen_51157946af5519c9 :: hs_bindgen_51157946af5519c9 ::
HsBindgen.Runtime.PtrConst.PtrConst T Ptr.Ptr T
-> IO () -> IO ()
hs_bindgen_51157946af5519c9 = hs_bindgen_51157946af5519c9 =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_51157946af5519c9_base HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_51157946af5519c9_base
@ -79,10 +80,12 @@ hs_bindgen_51157946af5519c9 =
__exported by:__ @garnet_rs.h@ __exported by:__ @garnet_rs.h@
-} -}
hello_struct :: hello_struct ::
HsBindgen.Runtime.PtrConst.PtrConst T T
-- ^ __C declaration:__ @t@ -- ^ __C declaration:__ @t@
-> IO () -> IO ()
hello_struct = hs_bindgen_51157946af5519c9 hello_struct =
\t0 ->
F.with t0 (\t1 -> hs_bindgen_51157946af5519c9 t1)
-- __unique:__ @com_garnet_GarnetRs_Safe_hello_shape@ -- __unique:__ @com_garnet_GarnetRs_Safe_hello_shape@
foreign import ccall safe "hs_bindgen_7de06f1fd827ca60" hs_bindgen_7de06f1fd827ca60_base :: foreign import ccall safe "hs_bindgen_7de06f1fd827ca60" hs_bindgen_7de06f1fd827ca60_base ::
@ -91,7 +94,7 @@ foreign import ccall safe "hs_bindgen_7de06f1fd827ca60" hs_bindgen_7de06f1fd827c
-- __unique:__ @com_garnet_GarnetRs_Safe_hello_shape@ -- __unique:__ @com_garnet_GarnetRs_Safe_hello_shape@
hs_bindgen_7de06f1fd827ca60 :: hs_bindgen_7de06f1fd827ca60 ::
HsBindgen.Runtime.PtrConst.PtrConst Shape Ptr.Ptr Shape
-> IO () -> IO ()
hs_bindgen_7de06f1fd827ca60 = hs_bindgen_7de06f1fd827ca60 =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_7de06f1fd827ca60_base HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_7de06f1fd827ca60_base
@ -103,7 +106,9 @@ hs_bindgen_7de06f1fd827ca60 =
__exported by:__ @garnet_rs.h@ __exported by:__ @garnet_rs.h@
-} -}
hello_shape :: hello_shape ::
HsBindgen.Runtime.PtrConst.PtrConst Shape Shape
-- ^ __C declaration:__ @s@ -- ^ __C declaration:__ @s@
-> IO () -> IO ()
hello_shape = hs_bindgen_7de06f1fd827ca60 hello_shape =
\s0 ->
F.with s0 (\s1 -> hs_bindgen_7de06f1fd827ca60 s1)

View File

@ -5,6 +5,7 @@
module GarnetRs.Unsafe where module GarnetRs.Unsafe where
import qualified Foreign as F
import qualified Foreign.C as FC import qualified Foreign.C as FC
import qualified GHC.Ptr as Ptr import qualified GHC.Ptr as Ptr
import qualified HsBindgen.Runtime.Internal.CAPI import qualified HsBindgen.Runtime.Internal.CAPI
@ -23,16 +24,16 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, " hello(arg1);" , " hello(arg1);"
, "}" , "}"
, "void hs_bindgen_29d823ada2bc7302 (" , "void hs_bindgen_29d823ada2bc7302 ("
, " struct T const *arg1" , " struct T *arg1"
, ")" , ")"
, "{" , "{"
, " hello_struct(arg1);" , " hello_struct(*arg1);"
, "}" , "}"
, "void hs_bindgen_b3f40a03f07eaa85 (" , "void hs_bindgen_b3f40a03f07eaa85 ("
, " struct Shape const *arg1" , " struct Shape *arg1"
, ")" , ")"
, "{" , "{"
, " hello_shape(arg1);" , " hello_shape(*arg1);"
, "}" , "}"
])) ]))
@ -67,7 +68,7 @@ foreign import ccall unsafe "hs_bindgen_29d823ada2bc7302" hs_bindgen_29d823ada2b
-- __unique:__ @com_garnet_GarnetRs_Unsafe_hello_struct@ -- __unique:__ @com_garnet_GarnetRs_Unsafe_hello_struct@
hs_bindgen_29d823ada2bc7302 :: hs_bindgen_29d823ada2bc7302 ::
HsBindgen.Runtime.PtrConst.PtrConst T Ptr.Ptr T
-> IO () -> IO ()
hs_bindgen_29d823ada2bc7302 = hs_bindgen_29d823ada2bc7302 =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_29d823ada2bc7302_base HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_29d823ada2bc7302_base
@ -79,10 +80,12 @@ hs_bindgen_29d823ada2bc7302 =
__exported by:__ @garnet_rs.h@ __exported by:__ @garnet_rs.h@
-} -}
hello_struct :: hello_struct ::
HsBindgen.Runtime.PtrConst.PtrConst T T
-- ^ __C declaration:__ @t@ -- ^ __C declaration:__ @t@
-> IO () -> IO ()
hello_struct = hs_bindgen_29d823ada2bc7302 hello_struct =
\t0 ->
F.with t0 (\t1 -> hs_bindgen_29d823ada2bc7302 t1)
-- __unique:__ @com_garnet_GarnetRs_Unsafe_hello_shape@ -- __unique:__ @com_garnet_GarnetRs_Unsafe_hello_shape@
foreign import ccall unsafe "hs_bindgen_b3f40a03f07eaa85" hs_bindgen_b3f40a03f07eaa85_base :: foreign import ccall unsafe "hs_bindgen_b3f40a03f07eaa85" hs_bindgen_b3f40a03f07eaa85_base ::
@ -91,7 +94,7 @@ foreign import ccall unsafe "hs_bindgen_b3f40a03f07eaa85" hs_bindgen_b3f40a03f07
-- __unique:__ @com_garnet_GarnetRs_Unsafe_hello_shape@ -- __unique:__ @com_garnet_GarnetRs_Unsafe_hello_shape@
hs_bindgen_b3f40a03f07eaa85 :: hs_bindgen_b3f40a03f07eaa85 ::
HsBindgen.Runtime.PtrConst.PtrConst Shape Ptr.Ptr Shape
-> IO () -> IO ()
hs_bindgen_b3f40a03f07eaa85 = hs_bindgen_b3f40a03f07eaa85 =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_b3f40a03f07eaa85_base HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_b3f40a03f07eaa85_base
@ -103,7 +106,9 @@ hs_bindgen_b3f40a03f07eaa85 =
__exported by:__ @garnet_rs.h@ __exported by:__ @garnet_rs.h@
-} -}
hello_shape :: hello_shape ::
HsBindgen.Runtime.PtrConst.PtrConst Shape Shape
-- ^ __C declaration:__ @s@ -- ^ __C declaration:__ @s@
-> IO () -> IO ()
hello_shape = hs_bindgen_b3f40a03f07eaa85 hello_shape =
\s0 ->
F.with s0 (\s1 -> hs_bindgen_b3f40a03f07eaa85 s1)

View File

@ -31,6 +31,6 @@ typedef struct Shape {
void hello(const char *c); void hello(const char *c);
void hello_struct(const struct T *t); void hello_struct(struct T t);
void hello_shape(const struct Shape *s); void hello_shape(struct Shape s);

View File

@ -16,7 +16,7 @@ struct T {
b: u8, b: u8,
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn hello_struct(t: &T) -> () { extern "C" fn hello_struct(t: T) -> () {
say_hello(&format!("{:?}", t)) say_hello(&format!("{:?}", t))
} }
@ -29,6 +29,6 @@ enum Shape {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn hello_shape(s: &Shape) -> () { extern "C" fn hello_shape(s: Shape) -> () {
say_hello(&format!("{:?}", s)) say_hello(&format!("{:?}", s))
} }