【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");
}
相关推荐
钟离墨笺22 分钟前
【c++】【智能指针】什么情况下不适合智能指针
开发语言·c++
郭源潮11 小时前
《C++知识梳理及常见面试题》
c++
勇敢滴勇2 小时前
【C++】二叉搜索树(二叉查找树、二叉排序树)详解
开发语言·c++·算法·霍夫曼树
f狐0狸x2 小时前
【蓝桥杯每日一题】3.16
c++·算法·蓝桥杯
webrtc&ffmpeg_study2 小时前
操作系统八股文整理(一)
c++·操作系统
UestcXiye2 小时前
《TCP/IP网络编程》学习笔记 | Chapter 18:多线程服务器端的实现
c++·计算机网络
Zfox_2 小时前
【Linux】五种 IO 模型与非阻塞 IO
linux·运维·服务器·c++·io模型
周Echo周2 小时前
8、STL中的map和pair使用方法
开发语言·数据结构·c++·考研·算法·leetcode·pat考试
TravisBytes3 小时前
VS Code 配置优化指南
开发语言·c++·python
梓䈑3 小时前
【C++】list(下):list类的模拟实现(含反向迭代器实现)
c++·windows·list