NativeCall Segfaults从Rust中获得元组

发布于 2025-02-02 21:52:26 字数 2734 浏览 1 评论 0原文

per rust ffi omnibus 以下内容应该有效。

这是一个生锈的cdylib lib.rs,名为“ foo”,用货物构建制造...

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()
}

这是raku脚本ffi-omnibus.raku,通过< a href =“ https://docs.raku.org/language/nativecall#structs” rel =“ nofollow noreferrer”> 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;

rust ffi Omnibus中有6种示例,这是唯一的一个示例我不能调试。如果您删除返回元组,并且只有一个元组参数,但没有返回类型,则有点有效。

我已经通过在这里有一个简单的指导,我有一个简单的指导设置此设置(Dockerfile,货物新...),以便您只需git克隆https://github.com/p6steve/raku-inku-inline-rust.git and cont这两个中的失败代码文件。

哦,错误是分割故障(核心倾倒)< / code>(在Ubuntu上)

Welcome to Rakudo™ v2022.04.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2022.04.

任何帮助 /指导非常感谢!

编辑 - 感谢Christoph的评论,Segfault现在已固定。

但是 - 我现在有一个新问题,结果是这样的混乱:

Tuple.new(x => 4252849424, y => 65535)   #I added a dd
425284942465535

因此,看起来仍然有一些错误; - (

如以前,任何帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文