Per the Rust FFI Omnibus the following should work.
This is a rust cdylib lib.rs
named "foo" made with cargo build...
use std::convert::From;
// Rust FFI Omnibus: Tuples
// http://jakegoulding.com/rust-ffi-omnibus/tuples/
// A Rust function that accepts a tuple
fn flip_things_around_rust(tup: (u32, u32)) -> (u32, u32) {
let (a, b) = tup;
(b + 1, a - 1)
}
// A struct that can be passed between C and Rust
#[repr(C)]
pub struct Tuple {
x: u32,
y: u32,
}
// Conversion functions
impl From<(u32, u32)> for Tuple {
fn from(tup: (u32, u32)) -> Tuple {
Tuple { x: tup.0, y: tup.1 }
}
}
impl From<Tuple> for (u32, u32) {
fn from(tup: Tuple) -> (u32, u32) {
(tup.x, tup.y)
}
}
// The exported C method - ORIG
//#[no_mangle]
//pub extern "C" fn flip_things_around(tup: Tuple) -> Tuple {
// flip_things_around_rust(tup.into()).into()
//}
// The exported C method - EDIT per Christoph
#[no_mangle]
pub extern "C" fn flip_things_around(tup: Tuple) -> *const Tuple {
&flip_things_around_rust(tup.into()).into()
}
And this is the raku script ffi-omnibus.raku
that consumes the library via Nativecall
use NativeCall;
constant $n-path = './ffi-omnibus/target/debug/foo';
## Rust FFI Omnibus: Tuples
## http:##jakegoulding.com/rust-ffi-omnibus/tuples/
class Tuple is repr('CStruct') {
has uint32 $.x;
has uint32 $.y;
}
sub flip_things_around(Tuple) returns Tuple is native($n-path) { * }
my \initial = Tuple.new( x => 10, y => 20 );
my \result = flip_things_around(initial);
dd result;
say result.x, result.y;
There are 6 types of examples in the Rust FFI Omnibus and this is the only one that I cannot debug. It kinda works if you remove the returns Tuple
and just have a Tuple argument, but no return type.
I have made a DRAFT raku module over here that has simple guidance of how to set this up (Dockerfile, cargo new ...) so you can just git clone https://github.com/p6steve/raku-Inline-Rust.git
and uncomment the failing code in these two files.
Oh, and the error is Segmentation fault (core dumped)
(on ubuntu)
Welcome to Rakudo™ v2022.04.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2022.04.
Any help / guidance much appreciated!
EDIT - Thanks to Christoph comment the segfault is now fixed.
BUT - I now have a new issue, the result is jumbled like this:
Tuple.new(x => 4252849424, y => 65535) #I added a dd
425284942465535
So looks like there is some error still ;-(
As before any help much appreciated!