Pass all non-primitive types by (immutable) reference

This commit is contained in:
George Thomas 2026-03-25 01:01:22 +00:00
parent 8cb0bf9c5e
commit 9e8bccdafd
2 changed files with 7 additions and 7 deletions

View File

@ -57,13 +57,13 @@ hello :: ByteString -> IO ()
hello s = useAsCString s $ Raw.hello . unsafeFromPtr hello s = useAsCString s $ Raw.hello . unsafeFromPtr
helloStruct :: T -> IO () helloStruct :: T -> IO ()
helloStruct = Raw.hello_struct . convertT helloStruct = flip with (Raw.hello_struct . unsafeFromPtr) . convertT
helloShape :: Shape -> IO () helloShape :: Shape -> IO ()
helloShape = Raw.hello_shape . convertShape helloShape = flip with (Raw.hello_shape . unsafeFromPtr) . convertShape
add :: Int64 -> Int64 -> Int64 add :: Int64 -> Int64 -> Int64
add = Raw.add add = Raw.add
sumTree :: BTree Int64 -> Int64 sumTree :: BTree Int64 -> Int64
sumTree = unsafePerformIO . flip withBTree Raw.sum_tree sumTree = unsafePerformIO . flip withBTree (flip with $ Raw.sum_tree . unsafeFromPtr)

View File

@ -22,7 +22,7 @@ struct T {
} }
#[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))
} }
@ -34,7 +34,7 @@ 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))
} }
@ -78,6 +78,6 @@ enum BTreeC {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn sum_tree(t: BTreeC) -> i64 { extern "C" fn sum_tree(t: &BTreeC) -> i64 {
(unsafe { std::mem::transmute::<_, &BTree<i64>>(&t) }).sum() (unsafe { std::mem::transmute::<_, &BTree<i64>>(t) }).sum()
} }