【CXX】6.3 &[T], &mut [T] — rust::Slice<T>

rust::Slice, rust::Slice

复制代码
Rust 中的 &[T] 在 C++ 中写作 rust::Slice<const T>
Rust 中的 &mut [T] 在 C++ 中写作 rust::Slice<T>
公共 API:
cpp 复制代码
// rust/cxx.h

template <typename T>
class Slice final {
public:
using value_type = T;

Slice() noexcept;
Slice(const Slice<T> &) noexcept;
Slice(T *, size_t count) noexcept;

template <typename C>
explicit Slice(C &c) : Slice(c.data(), c.size());

Slice &operator=(Slice<T> &&) & noexcept;
Slice &operator=(const Slice<T> &) & noexcept
requires std::is_const_v<T>;

T *data() const noexcept;
size_t size() const noexcept;
size_t length() const noexcept;
bool empty() const noexcept;

T &operator[](size_t n) const noexcept;
T &at(size_t n) const;
T &front() const noexcept;
T &back() const noexcept;

class iterator;
iterator begin() const noexcept;
iterator end() const noexcept;

void swap(Slice &) noexcept;
};
限制:

T 不能是不透明的 Rust 类型或不透明的 C++ 类型。对不透明 Rust 类型的切片支持即将推出。

允许作为函数参数或返回值。不支持在共享结构体中使用。

只有 rust::Slice 是可复制赋值的,rust::Slice 则不行。(两者都是可移动赋值的。)你需要偶尔写 std::move 来提醒自己,意外地将重叠的 &mut [T] 暴露给 Rust 是未定义行为。

示例

这个示例是一个 C++ 程序,它构造了一个包含 JSON 数据的切片(通过从 stdin 读取,但也可以来自任何地方),然后调用 Rust 来通过 serde_json 和 serde_transcode 库将该 JSON 数据漂亮地打印到一个 std::string 中。

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

#![no_main] // main 定义在 C++ 的 main.cc 中

use cxx::CxxString;
use std::io::{self, Write};
use std::pin::Pin;

#[cxx::bridge]
mod ffi {
extern "Rust" {
fn prettify_json(input: &[u8], output: Pin<&mut CxxString>) -> Result<()>;
}
}

struct WriteToCxxString<'a>(Pin<&'a mut CxxString>);

impl<'a> Write for WriteToCxxString<'a> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.as_mut().push_bytes(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

fn prettify_json(input: &[u8], output: Pin<&mut CxxString>) -> serde_json::Result<()> {
let writer = WriteToCxxString(output);
let mut deserializer = serde_json::Deserializer::from_slice(input);
let mut serializer = serde_json::Serializer::pretty(writer);
serde_transcode::transcode(&mut deserializer, &mut serializer)
}
cpp 复制代码
// src/main.cc

#include "example/src/main.rs.h"
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main() {
// 从 stdin 读取 json。
std::istreambuf_iterator<char> begin{std::cin}, end;
std::vector<unsigned char> input{begin, end};
rust::Slice<const uint8_t> slice{input.data(), input.size()};

// 使用 serde_json 和 serde_transcode 进行漂亮打印。
std::string output;
prettify_json(slice, output);

// 写入 stdout。
std::cout << output << std::endl;
}
测试示例:
复制代码
$ echo '{"fearless":"concurrency"}' | cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running target/debug/example
{
"fearless": "concurrency"
}
相关推荐
时空系9 小时前
认识Rust——我的第一个程序 Rust中文编程
开发语言·后端·rust
时空系10 小时前
第10篇:归属权与借用——Rust的安全保障 Rust中文编程
开发语言·安全·rust
时空系10 小时前
第6篇:数据容器——管理大量数据 Rust中文编程
开发语言·后端·rust
时空系10 小时前
第7篇:功能——打造你的工具箱 Rust中文编程
开发语言·网络·rust
qcx2313 小时前
拆解 Warp AI Agent(五):跨生态联邦——10 种 Skill + MCP + 多 Harness 互操作设计
人工智能·rust·ai agent·skill·warp·mcp·harness
时空系13 小时前
第8篇:结构模板——自定义数据类型 Rust中文编程
开发语言·网络·rust
冬奇Lab1 天前
一天一个开源项目(第89篇):Warp - AI 驱动的现代化 Rust 终端
人工智能·rust·开源
时空系1 天前
第2篇:数据与数据类型——存储信息的小盒子 Rust中文编程
开发语言·后端·rust