From adc542fd219ed17a14cc175dea00ae1a2632cf73 Mon Sep 17 00:00:00 2001 From: George Thomas Date: Wed, 25 Mar 2026 01:01:22 +0000 Subject: [PATCH] Pass all non-primitive types by (immutable) reference --- lib/GarnetRs/Wrapped.hs | 6 +++--- rust/lib.rs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/GarnetRs/Wrapped.hs b/lib/GarnetRs/Wrapped.hs index 51d9255..b6b7c9c 100644 --- a/lib/GarnetRs/Wrapped.hs +++ b/lib/GarnetRs/Wrapped.hs @@ -57,13 +57,13 @@ hello :: ByteString -> IO () hello s = useAsCString s $ Raw.hello . unsafeFromPtr helloStruct :: T -> IO () -helloStruct = Raw.hello_struct . convertT +helloStruct = flip with (Raw.hello_struct . unsafeFromPtr) . convertT helloShape :: Shape -> IO () -helloShape = Raw.hello_shape . convertShape +helloShape = flip with (Raw.hello_shape . unsafeFromPtr) . convertShape add :: Int64 -> Int64 -> Int64 add = Raw.add sumTree :: BTree Int64 -> Int64 -sumTree = unsafePerformIO . flip withBTree Raw.sum_tree +sumTree = unsafePerformIO . flip withBTree (flip with $ Raw.sum_tree . unsafeFromPtr) diff --git a/rust/lib.rs b/rust/lib.rs index 35b3ba2..d80313d 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -22,7 +22,7 @@ struct T { } #[unsafe(no_mangle)] -extern "C" fn hello_struct(t: T) -> () { +extern "C" fn hello_struct(t: &T) -> () { say_hello(&format!("{:?}", t)) } @@ -34,7 +34,7 @@ enum Shape { } #[unsafe(no_mangle)] -extern "C" fn hello_shape(s: Shape) -> () { +extern "C" fn hello_shape(s: &Shape) -> () { say_hello(&format!("{:?}", s)) } @@ -78,6 +78,6 @@ enum BTreeC { } #[unsafe(no_mangle)] -extern "C" fn sum_tree(t: BTreeC) -> i64 { - (unsafe { std::mem::transmute::<_, &BTree>(&t) }).sum() +extern "C" fn sum_tree(t: &BTreeC) -> i64 { + (unsafe { std::mem::transmute::<_, &BTree>(t) }).sum() }