当前位置: 首页 > news >正文

【CXX】6.4 CxxString — std::string

std::string 的 Rust 绑定称为 CxxString。有关 Rust API 的文档,请参见链接。

限制:

Rust 代码永远不能通过值获取 CxxString。C++ 的 string 需要一个移动构造函数,并且可能持有内部指针,这与 Rust 的移动行为不兼容。因此,在 Rust 代码中,我们只能通过引用或智能指针来查看 CxxString,例如 &CxxString、Pin<&mut CxxString> 或 UniquePtr。

为了在 Rust 中在栈上构造一个 CxxString,你必须使用 let_cxx_string! 宏,该宏会正确地固定字符串。下面的代码在一个地方使用了这个宏,链接中涵盖了其语法。

示例

这个示例使用 C++17 的 std::variant 来构建一个玩具 JSON 类型。JSON 可以包含各种类型,包括字符串,而 JSON 的对象类型是一个带有字符串键的映射。该示例演示了 Rust 如何索引到这些映射之一。

// src/main.rs

use cxx::let_cxx_string;

#[cxx::bridge]
mod ffi {
unsafe extern "C++" {
include!("example/include/json.h");

    #[cxx_name = "json"]
    type Json;
    #[cxx_name = "object"]
    type Object;

    fn isNull(self: &Json) -> bool;
    fn isNumber(self: &Json) -> bool;
    fn isString(self: &Json) -> bool;
    fn isArray(self: &Json) -> bool;
    fn isObject(self: &Json) -> bool;

    fn getNumber(self: &Json) -> f64;
    fn getString(self: &Json) -> &CxxString;
    fn getArray(self: &Json) -> &CxxVector<Json>;
    fn getObject(self: &Json) -> &Object;

    #[cxx_name = "at"]
    fn get<'a>(self: &'a Object, key: &CxxString) -> &'a Json;

    fn load_config() -> UniquePtr<Json>;
}
}

fn main() {
let config = ffi::load_config();

let_cxx_string!(key = "name");
println!("{}", config.getObject().get(&key).getString());
}
// include/json.h

#pragma once
#include <map>
#include <memory>
#include <string>
#include <variant>
#include <vector>

class json final {
public:
static const json null;
using number = double;
using string = std::string;
using array = std::vector<json>;
using object = std::map<string, json>;

json() noexcept = default;
json(const json &) = default;
json(json &&) = default;
template <typename... T>
json(T &&...value) : value(std::forward<T>(value)...) {}

bool isNull() const;
bool isNumber() const;
bool isString() const;
bool isArray() const;
bool isObject() const;

number getNumber() const;
const string &getString() const;
const array &getArray() const;
const object &getObject() const;

private:
std::variant<std::monostate, number, string, array, object> value;
};

using object = json::object;

std::unique_ptr<json> load_config();
// include/json.cc

#include "example/include/json.h"
#include <initializer_list>
#include <utility>

const json json::null{};
bool json::isNull() const { return std::holds_alternativestd::monostate(value); }
bool json::isNumber() const { return std::holds_alternative<number>(value); }
bool json::isString() const { return std::holds_alternative<string>(value); }
bool json::isArray() const { return std::holds_alternative<array>(value); }
bool json::isObject() const { return std::holds_alternative<object>(value); }
json::number json::getNumber() const { return std::get<number>(value); }
const json::string &json::getString() const { return std::get<string>(value); }
const json::array &json::getArray() const { return std::get<array>(value); }
const json::object &json::getObject() const { return std::get<object>(value); }

std::unique_ptr<json> load_config() {
return std::make_unique<json>(
std::in_place_typejson::object,
std::initializer_list<std::pair<const std::string, json>>{
{"name", "cxx-example"},
{"edition", 2021.},
{"repository", json::null}});
}

相关文章:

  • 第十七:go 反射
  • Spring Boot中@Valid 与 @Validated 注解的详解
  • macOS 终端优化
  • 使用DeepSeek+蓝耘快速设计网页简易版《我的世界》小游戏
  • 从0到1:JavaScript小白进阶之路
  • mapbox-gl的Popup的使用详解
  • 旋转位置编码(3)
  • HarmonyOS
  • Spring Boot 项目中使用责任链模式实现复杂接口解耦和动态编排(带示例)
  • 前端技术百宝箱
  • Tweak Power:全方位电脑系统优化的高效工具
  • MySQL 与 MongoDB 的区别
  • CAN总线协议攻防实战:从漏洞分析到攻击模拟
  • 衣联网的商品列表页面结构是怎样的?
  • 设计基于锁的并发数据结构_第六章_《C++并发编程实战》笔记
  • 新一代开源数字供应链安全审查与治理平台:悬镜源鉴SCA
  • 版本控制泄露源码 .svn
  • 机器学习数学基础:45.多重响应分析
  • 鸿蒙应用开发-轻松获取http网络请求
  • 【从零开始学习计算机科学】操作系统(七)文件管理
  • 复旦一校友捐赠1亿元,却不留名
  • 李洋谈美国黑帮电影与黑帮文化
  • 2025年上海科技节开幕,人形机器人首次登上科学红毯
  • 朝鲜称将在各领域采取反制措施,应对美国敌对挑衅
  • 浙江演艺集团7部作品组团来沪,今夏开启首届上海演出季
  • 沃尔玛上财季净利下滑12%:关税带来成本压力,新财季价格涨幅将高于去年