调用交换两个值的FFI函数的正确方法是什么?

问题描述 投票:3回答:1

我是Rust的新手,我正在努力理解它的核心概念,即所有权和借款。我已经阅读了这本书和其他一些文章,但它仍然让我感到困惑。

我有一个像这样定义的结构:

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Element(pub (crate) [u64; 12]);

我有一个外部函数,它根据Element值交换两个flag的值。如果flag为0,则不交换它们,如果flag为1,则交换它们。这就是原型我使用&mut的原因:

extern {
    pub fn swap(x: &mut Element, y: &mut Element, flag: u8);
}

我围绕这个外部函数在Rust中创建了一个包装器方法:

impl for Element {
    fn wrapper_swap(&mut self, y: &mut Element, flag: u8) {
        unsafe {
            swap(self, y, flag); // OR swap(&mut self, &mut y, flag); ?!
        }
    }
}

我的wrapper_swap函数与外部原型函数swap具有相同的签名。我如何在这里调用外部函数swap

由于selfy已被定义为&mut self&mut y,我可以将交换函数称为swap(self, y, flag);吗?或者会删除可变引用,实际上我需要将其称为swap(&mut self, &mut y, flag);

对于我如何用实际值来调用它,对我来说也变得很复杂。例如,我有三个变量定义如下:

let mut one = Element([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
let mut two = Element([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]);
let flag: u8 = 0;

由于变量已经被定义为可变,我是否需要将wrapper_swap函数称为(&one).wrapper_swap(&two, flag);,或者这将使引用成为不可变的,无论变量是否被定义为可变,而我需要将其称为(&mut one).wrapper_swap(&mut two, flag);?哪种方法正确?

也许是因为我使用的是不安全的函数,但编译器并没有抱怨我如何调用这个函数。

如果变量onetwo没有定义为可变,会发生什么?我还可以像&mut one&mut two一样借用它们,并在另一个函数中改变它们的值吗?

rust
1个回答
2
投票

你可以用最少量的探索回答你的问题。编程时不要害怕尝试;你通常不太可能失去任何有价值的东西。

例如...

build.人生

extern crate cc;

fn main() {
    cc::Build::new()
        .file("src/swap.c")
        .compile("repro");
}

SRC / swap.c

#include <stdio.h>
#include <stdlib.h>

void swap(int32_t *a, int32_t *b) {
  printf("Swapping %p, %p\n", a, b);

  int32_t t = *a;
  *a = *b;
  *b = t;
}

SRC / main.rs

extern crate libc;

use libc::int32_t;

extern "C" {
    fn swap(a: &mut int32_t, b: &mut int32_t);
}

fn swap_wrapper_1(a: &mut i32, b: &mut i32) {
    unsafe { swap(a, b) }
}

fn main() {
    let mut a = 1;
    let mut b = 2;

    swap_wrapper_1(&mut a, &mut b);
    println!("{}, {}", a, b);
}

这打印:

Swapping 0x7ffee1cf9ff0, 0x7ffee1cf9ff4
2, 1

如果您尝试使用第二种形式:

fn swap_wrapper_2(a: &mut i32, b: &mut i32) {
    unsafe { swap(&mut a, &mut b) }
}

你得到一堆警告:

error[E0596]: cannot borrow immutable argument `a` as mutable
  --> src/main.rs:15:15
   |
14 | fn swap_wrapper_2(a: &mut i32, b: &mut i32) {
   |                   - consider changing this to `mut a`
15 |     swap(&mut a, &mut b)
   |               ^ cannot borrow mutably

如果你修复它,然后背靠背调用两个表单,你会得到:

Swapping 0x7ffee7483f60, 0x7ffee7483f64
2, 1
Swapping 0x7ffee7483f60, 0x7ffee7483f64
1, 2

这是对的 - 它正在交换完全相同的地址。那是因为当需要匹配签名时Rust会automatically dereference your value,所以只要你正确定义了FFI签名就没关系了。

© www.soinside.com 2019 - 2024. All rights reserved.