| Crates.io | hicc-std |
| lib.rs | hicc-std |
| version | 0.2.1 |
| created_at | 2025-05-06 00:53:02.66026+00 |
| updated_at | 2025-09-04 02:57:06.518497+00 |
| description | Based on hicc's ability to provide rust with the ability to call c++ standard container class interfaces |
| homepage | |
| repository | https://gitcode.com/xuanwu/hicc |
| max_upload_size | |
| id | 1661749 |
| size | 244,872 |
基于hicc为c++标准库中的容器类提供安全的rust api.
注:0.2.0版本为原可能抛出各种异常的调用接口增加入参检查消除异常, 部分这类接口的函数类型发生了变化.
支持c++标准库的全部容器类型:
std::basic_string<charT> (hicc/std/string.hpp)std::vector<T>(hicc/std/vector.hpp)std::vector<bool>(hicc/std/vector.hpp)std::deque<T>(hicc/std/deque.hpp)std::set<T>(hicc/std/set.hpp)std::multiset<T>(hicc/std/set.hpp)std::unordered_set<T>(hicc/std/unordered_set.hpp)std::unordered_multiset<T>(hicc/std/unordered_set.hpp)std::map<T>(hicc/std/map.hpp)std::multimap<T>(hicc/std/map.hpp)std::unordered_map<T>(hicc/std/unordered_map.hpp)std::unordered_multimap<T>(hicc/std/unordered_map.hpp)std::array<T>(hicc/std/array.hpp)std::list<T>(hicc/std/list.hpp)std::forward_list<T>(hicc/std/forward_list.hpp)std::queue<T>(hicc/std/queue.hpp)std::priority_queue<T>(hicc/std/queue.hpp)std::stack<T>(hicc/std/stack.hpp)注:hicc-std仅提供了std::string, std::u16string, std::u32string相关的构造函数.
因为都是模板类,需要使用者显示实现实例化模版类的构建函数. 参见如下代码:
use hicc::AbicClass;
hicc::cpp! {
// c++侧需要引用hicc提供的头文件.
#include <hicc/std/map.hpp>
#include <hicc/std/string.hpp>
// 按需定义容器类型. 可以包含非缺省的Allocator等模版参数类型.
typedef std::map<int, std::string> CppMap;
}
hicc::import_lib! {
#![link_name = "example"]
// 对应`c++`的`CppMap`
class RustMap = hicc_std::map<hicc::Pod<i32>, hicc_std::string>;
// 创建容器接口.
#[cpp(func = "std::unique_ptr<CppMap> hicc::make_unique<CppMap>()")]
fn rustmap_new() -> RustMap;
}
fn main() {
let mut map = rustmap_new();
let name = hicc_std::string::from(c"hello");
map.insert(&0, &name);
assert_eq!(map.get(&1), None);
assert_eq!(map.get(&0), Some(name.as_ref()));
}
注意:
- 模版参数类型只能是
c++类或者可直接在CABI接口上传递使用的POD数据类型,后者只能结合hicc::Pod<T>使用.
build.rs编译c++代码fn main() {
hicc_build::Build::new().rust_file("src/main.rs").compile("example");
println!("cargo::rustc-link-lib=example");
println!("cargo::rustc-link-lib=stdc++");
println!("cargo::rerun-if-changed=src/main.rs");
}
注意: 需要最终构建为可执行程序或者动态库时指定所依赖的**c++标准库**.
c++容器基于迭代器实现插入删除等接口违背rust的借用规则, hicc-std将迭代器做了二次封装,提供容器遍历和插入删除功能.
doc test需要开启test feature, 提供了测试用例用到的容器实例化类型的构建函数.
# cargo test --features "test"