Expand description
§使用方式
注:
hicc-std仅提供了std::string,std::u16string,std::u32string相关的构造函数.v0.2.0版本, 通过增加参数校验,消除了原接口中可能抛出的各种异常. 部分接口的返回值类型发生了变化.
因为都是模板类,需要使用者显式实现实例化模版类的构建函数. 参见如下代码:
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");
}hicc_build仅支持生成静态库, 需要最终构建为可执行程序或者动态库时指定所依赖的c++标准库.
§迭代器接口说明
c++容器基于迭代器实现插入删除等接口违背rust的借用规则, hicc-std将迭代器做了二次封装,提供容器遍历和插入删除功能.
§测试
doc test需要开启test feature, 提供了测试用例用到的容器实例化类型的构建函数.
# cargo test --features "test"Structs§
- Forward
List Iter - 对应
std::forward_list<T>::const_iterator - Forward
List Iter Mut - 对应
std::forward_list<T>::iterator - List
Iter Mut - 对应
std::list<T>::iterator - VecBool
- cpp class:
template<class Allocator> std::vector<bool, Allocator> - array
- cpp class:
template<class T, size_t N> std::array<T, N> - basic_
string - cpp class:
template<class CharT, class Traits, class Allocator> std::basic_string<CharT, Traits, Allocator> - deque
- cpp class:
template<class T, class Allocator> std::deque<T, Allocator> - forward_
list - cpp class:
template<class T, class Allocator> std::forward_list<T, Allocator> - list
- cpp class:
template<class T, class Allocator> std::list<T, Allocator> - map
- cpp class:
template<class K, class V, class Compare, class Allocator> std::map<K, V, Compare, Allocator> - multimap
- cpp class:
template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator> - multiset
- cpp class:
template<class T, class Compare, class Allocator> std::multiset<T, Compare, Allocator> - priority_
queue - cpp class:
template<class T, class Container, class Compare> std::priority_queue<T, Container, Compare> - queue
- cpp class:
template<class T, class Container> std::queue<T, Container> - set
- cpp class:
template<class T, class Compare, class Allocator> std::set<T, Compare, Allocator> - stack
- cpp class:
template<class T, class Container> std::stack<T, Container> - unordered_
map - cpp class:
template <class K, class V, class Hash, class Pred, class Allocator> std::unordered_map<K, V, Hash, Pred, Allocator> - unordered_
multimap - cpp class:
template <class K, class V, class Hash, class Pred, class Allocator> std::unordered_multimap<K, V, Hash, Pred, Allocator> - unordered_
multiset - cpp class:
template <class T, class Hash, class Pred, class Allocator> std::unordered_multiset<T, Hash, Pred, Allocator> - unordered_
set - cpp class:
template <class T, class Hash, class Pred, class Allocator> std::unordered_set<T, Hash, Pred, Allocator> - vector
- cpp class:
template<class T, class Allocator> std::vector<T, Allocator>