【CXX】6.5 Box<T> — rust::Box<T>

rust::Box公共 API:

cpp 复制代码
// rust/cxx.h

template <typename T>
class Box final {
public:
  using element_type = T;
  using const_pointer =
      typename std::add_pointer<typename std::add_const<T>::type>::type;
  using pointer = typename std::add_pointer<T>::type;

  Box(Box &&) noexcept;
  ~Box() noexcept;

  explicit Box(const T &);
  explicit Box(T &&);

  Box &operator=(Box &&) & noexcept;

  const T *operator->() const noexcept;
  const T &operator*() const noexcept;
  T *operator->() noexcept;
  T &operator*() noexcept;

  template <typename... Fields>
  static Box in_place(Fields &&...);

  void swap(Box &) noexcept;

  // 重要:要求 `raw` 必须来自 `into_raw` 调用。不要
  // 传递来自 `new` 或其他来源的指针。
  static Box from_raw(T *) noexcept;

  T *into_raw() noexcept;
};

限制:

Box 不支持 T 为不透明的 C++ 类型。对于在语言边界传递不透明 C++ 类型的所有权,应使用 UniquePtr 或 SharedPtr。

如果 T 是不透明的 Rust 类型,则要求该 Rust 类型为 Sized,即在编译时已知大小。未来可能会引入对动态大小的不透明 Rust 类型的支持。

示例:

以下程序使用 Box 将某些不透明的 Rust 状态的所有权传递给 C++,然后再传递回 Rust 回调函数。这是实现 FFI 异步函数的有用模式。

rust 复制代码
// src/main.rs

use std::io::Write;

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        type File;
    }

    unsafe extern "C++" {
        include!("example/include/example.h");

        fn f(
            callback: fn(Box<File>, fst: &str, snd: &str),
            out: Box<File>,
        );
    }
}

pub struct File(std::fs::File);

fn main() {
    let out = std::fs::File::create("example.log").unwrap();

    ffi::f(
        |mut out, fst, snd| { let _ = write!(out.0, "{}{}\n", fst, snd); },
        Box::new(File(out)),
    );
}
cpp 复制代码
// include/example.h

#pragma once
#include "example/src/main.rs.h"
#include "rust/cxx.h"

void f(rust::Fn<void(rust::Box<File>, rust::Str, rust::Str)> callback,
       rust::Box<File> out);

// include/example.cc

#include "example/include/example.h"

void f(rust::Fn<void(rust::Box<File>, rust::Str, rust::Str)> callback,
       rust::Box<File> out) {
  callback(std::move(out), "fearless", "concurrency");
}
相关推荐
逝雪Yuki1 小时前
牛客——接头密匙
c++·字典树·前缀树·数据结构与算法
三小尛1 小时前
C++拷贝构造函数
开发语言·c++
码农葫芦侠2 小时前
C++继承中虚函数调用时机问题及解决方案
c++·qt
一只余弦函数2 小时前
《C++》STL--list容器详解
开发语言·c++·list
爱学习的小邓同学2 小时前
C++ --- stack和queue的使用以及简单实现
c++
霜羽68923 小时前
【C++篇】模版进阶
开发语言·c++
超闻逸事3 小时前
题解:CF2129C Interactive RBS
c++·算法·codeforces
大卫小东(Sheldon)3 小时前
智能生成git提交消息工具 GIM 发布 1.7 版本了
git·ai·rust
一方热衷.4 小时前
YOLOv8/YOLOv11 C++ OpenCV DNN推理
c++·opencv·yolo