From 5907a064a790e98c7e47e292d1c26cb19fe52879 Mon Sep 17 00:00:00 2001 From: George Thomas Date: Thu, 19 Feb 2026 15:47:55 +0000 Subject: [PATCH] Use fixed-width integers Even though `Int` and `isize` should be the same in practice, we can't cleanly convert, as the type information isn't quite there. And anyway, strictly speaking per the report, `Int` is only guaranteed to hold 30 bits. Note that this is essentially unchanged even if we specify `usize_is_size_t = true` for `cbindgen`. --- exe/GarnetRs/Wrapped.hs | 9 ++------- generated/GarnetRs/FunPtr.hs | 12 ++++++------ generated/GarnetRs/Safe.hs | 20 ++++++++++---------- generated/GarnetRs/Unsafe.hs | 20 ++++++++++---------- rust/garnet_rs.h | 2 +- rust/lib.rs | 2 +- 6 files changed, 30 insertions(+), 35 deletions(-) diff --git a/exe/GarnetRs/Wrapped.hs b/exe/GarnetRs/Wrapped.hs index 6be065f..d045576 100644 --- a/exe/GarnetRs/Wrapped.hs +++ b/exe/GarnetRs/Wrapped.hs @@ -16,7 +16,6 @@ import Foreign.C import GarnetRs qualified as Raw import GarnetRs.Safe qualified as Raw import HsBindgen.Runtime.PtrConst -import Unsafe.Coerce data T = T { a :: Bool @@ -42,9 +41,5 @@ helloStruct = Raw.hello_struct . convertT helloShape :: Shape -> IO () helloShape = Raw.hello_shape . convertShape -add :: Int -> Int -> IO Int -add a b = from <$> Raw.add (to a) (to b) - where - -- these types should be the same in practice - both are pointer-sized - to = unsafeCoerce @Int @CIntPtr - from = unsafeCoerce @CIntPtr @Int +add :: Int64 -> Int64 -> IO Int64 +add = Raw.add diff --git a/generated/GarnetRs/FunPtr.hs b/generated/GarnetRs/FunPtr.hs index 76031fb..77c1f94 100644 --- a/generated/GarnetRs/FunPtr.hs +++ b/generated/GarnetRs/FunPtr.hs @@ -44,9 +44,9 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un , "}" , "/* com_garnet_GarnetRs_get_add */" , "__attribute__ ((const))" - , "intptr_t (*hs_bindgen_bbabdbe61cd1eeb2 (void)) (" - , " intptr_t arg1," - , " intptr_t arg2" + , "int64_t (*hs_bindgen_bbabdbe61cd1eeb2 (void)) (" + , " int64_t arg1," + , " int64_t arg2" , ")" , "{" , " return &add;" @@ -118,17 +118,17 @@ foreign import ccall unsafe "hs_bindgen_bbabdbe61cd1eeb2" hs_bindgen_bbabdbe61cd IO (Ptr.FunPtr Void) -- __unique:__ @com_garnet_GarnetRs_get_add@ -hs_bindgen_bbabdbe61cd1eeb2 :: IO (Ptr.FunPtr (HsBindgen.Runtime.LibC.CIntPtr -> HsBindgen.Runtime.LibC.CIntPtr -> IO HsBindgen.Runtime.LibC.CIntPtr)) +hs_bindgen_bbabdbe61cd1eeb2 :: IO (Ptr.FunPtr (HsBindgen.Runtime.LibC.Int64 -> HsBindgen.Runtime.LibC.Int64 -> IO HsBindgen.Runtime.LibC.Int64)) hs_bindgen_bbabdbe61cd1eeb2 = HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_bbabdbe61cd1eeb2_base {-# NOINLINE add #-} {-| __C declaration:__ @add@ - __defined at:__ @garnet_rs.h 38:10@ + __defined at:__ @garnet_rs.h 38:9@ __exported by:__ @garnet_rs.h@ -} -add :: Ptr.FunPtr (HsBindgen.Runtime.LibC.CIntPtr -> HsBindgen.Runtime.LibC.CIntPtr -> IO HsBindgen.Runtime.LibC.CIntPtr) +add :: Ptr.FunPtr (HsBindgen.Runtime.LibC.Int64 -> HsBindgen.Runtime.LibC.Int64 -> IO HsBindgen.Runtime.LibC.Int64) add = GHC.IO.Unsafe.unsafePerformIO hs_bindgen_bbabdbe61cd1eeb2 diff --git a/generated/GarnetRs/Safe.hs b/generated/GarnetRs/Safe.hs index 28c2c65..7b0bec2 100644 --- a/generated/GarnetRs/Safe.hs +++ b/generated/GarnetRs/Safe.hs @@ -37,9 +37,9 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un , "{" , " hello_shape(*arg1);" , "}" - , "intptr_t hs_bindgen_1c0c71fa74c428a9 (" - , " intptr_t arg1," - , " intptr_t arg2" + , "int64_t hs_bindgen_1c0c71fa74c428a9 (" + , " int64_t arg1," + , " int64_t arg2" , ")" , "{" , " return add(arg1, arg2);" @@ -130,22 +130,22 @@ foreign import ccall safe "hs_bindgen_1c0c71fa74c428a9" hs_bindgen_1c0c71fa74c42 -- __unique:__ @com_garnet_GarnetRs_Safe_add@ hs_bindgen_1c0c71fa74c428a9 :: - HsBindgen.Runtime.LibC.CIntPtr - -> HsBindgen.Runtime.LibC.CIntPtr - -> IO HsBindgen.Runtime.LibC.CIntPtr + HsBindgen.Runtime.LibC.Int64 + -> HsBindgen.Runtime.LibC.Int64 + -> IO HsBindgen.Runtime.LibC.Int64 hs_bindgen_1c0c71fa74c428a9 = HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_1c0c71fa74c428a9_base {-| __C declaration:__ @add@ - __defined at:__ @garnet_rs.h 38:10@ + __defined at:__ @garnet_rs.h 38:9@ __exported by:__ @garnet_rs.h@ -} add :: - HsBindgen.Runtime.LibC.CIntPtr + HsBindgen.Runtime.LibC.Int64 -- ^ __C declaration:__ @a@ - -> HsBindgen.Runtime.LibC.CIntPtr + -> HsBindgen.Runtime.LibC.Int64 -- ^ __C declaration:__ @b@ - -> IO HsBindgen.Runtime.LibC.CIntPtr + -> IO HsBindgen.Runtime.LibC.Int64 add = hs_bindgen_1c0c71fa74c428a9 diff --git a/generated/GarnetRs/Unsafe.hs b/generated/GarnetRs/Unsafe.hs index cea8eee..87f47f8 100644 --- a/generated/GarnetRs/Unsafe.hs +++ b/generated/GarnetRs/Unsafe.hs @@ -37,9 +37,9 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un , "{" , " hello_shape(*arg1);" , "}" - , "intptr_t hs_bindgen_20eb651f0a8faf48 (" - , " intptr_t arg1," - , " intptr_t arg2" + , "int64_t hs_bindgen_20eb651f0a8faf48 (" + , " int64_t arg1," + , " int64_t arg2" , ")" , "{" , " return add(arg1, arg2);" @@ -130,22 +130,22 @@ foreign import ccall unsafe "hs_bindgen_20eb651f0a8faf48" hs_bindgen_20eb651f0a8 -- __unique:__ @com_garnet_GarnetRs_Unsafe_add@ hs_bindgen_20eb651f0a8faf48 :: - HsBindgen.Runtime.LibC.CIntPtr - -> HsBindgen.Runtime.LibC.CIntPtr - -> IO HsBindgen.Runtime.LibC.CIntPtr + HsBindgen.Runtime.LibC.Int64 + -> HsBindgen.Runtime.LibC.Int64 + -> IO HsBindgen.Runtime.LibC.Int64 hs_bindgen_20eb651f0a8faf48 = HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_20eb651f0a8faf48_base {-| __C declaration:__ @add@ - __defined at:__ @garnet_rs.h 38:10@ + __defined at:__ @garnet_rs.h 38:9@ __exported by:__ @garnet_rs.h@ -} add :: - HsBindgen.Runtime.LibC.CIntPtr + HsBindgen.Runtime.LibC.Int64 -- ^ __C declaration:__ @a@ - -> HsBindgen.Runtime.LibC.CIntPtr + -> HsBindgen.Runtime.LibC.Int64 -- ^ __C declaration:__ @b@ - -> IO HsBindgen.Runtime.LibC.CIntPtr + -> IO HsBindgen.Runtime.LibC.Int64 add = hs_bindgen_20eb651f0a8faf48 diff --git a/rust/garnet_rs.h b/rust/garnet_rs.h index 76f3125..15aaba1 100644 --- a/rust/garnet_rs.h +++ b/rust/garnet_rs.h @@ -35,4 +35,4 @@ void hello_struct(struct T t); void hello_shape(struct Shape s); -intptr_t add(intptr_t a, intptr_t b); +int64_t add(int64_t a, int64_t b); diff --git a/rust/lib.rs b/rust/lib.rs index 59044e0..5be6262 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -35,6 +35,6 @@ extern "C" fn hello_shape(s: Shape) -> () { } #[unsafe(no_mangle)] -extern "C" fn add(a: isize, b: isize) -> isize { +extern "C" fn add(a: i64, b: i64) -> i64 { a + b }