#cpp #cc #ffi

sys hicc

Safe, efficient, full-featured FFI between Rust and C++

1 unstable release

new 0.1.0 May 6, 2025

#5 in #cxx


Used in hicc-std

MIT/Apache

73KB
1K SLoC

hicc

A Rust FFI SDK that easily and efficiently enables the calling of any c++ api function, while significantly increasing the memory safety of c++ api interfaces.

    --------                ---------------------------        ------------------              --------
    | rust | ---rust api---| c++ abi declare with rust |       | c++ abi declare |---c++ api---| c++  |
    -------                 ---------------------------        ------------------              --------
                                          |                             |
                                 -----------------               ---------------
                                 | hicc rust sdk |-----c abi-----| hicc c++ sdk |
                                 -----------------        |      ---------------
                                                          |                     
                                 -----------------        |
                                 | hicc js sdk   | --------
                                 -----------------

支持的功能介绍

支持的数据类型.

数据类型 是否支持 备注
T support
const T& support
T& support
T&& support rust侧同T
const T* support 程序员管理资源生命周期
T* support 程序员管理资源生命周期
const T**多重指针 support 程序员管理资源生命周期
T**多重指针 support 程序员管理资源生命周期
std::function<R(ArgTypes...)> support

说明:

  1. rust侧忽略volatile.

支持的函数类型.

函数分类 是否支持
funtion(external linkage/internal linkage/no linkage) support
function overloading support
default parameters support
template function support
class member function support
noexcept(false) support
template class support
dynamic_cast support
realizing virtual function with rust support
va_list support
variadic(...) partial support(仅全局函数,参数和返回值无类类型)

安全性保证和增强

基于rust生命周期机制,可增强c++接口的安全性.

c++容器的迭代器是一个典型的例子.

template<class T, class Allocator> class vector {
    //...
    iterator begin();
};

可映射为如下rust接口:

hicc::import_class! {
    pub class vector<T> {
        // ...
        fn begin(&mut self) -> &mut VecIter<T>;
    }
    pub class VecIter<T> {
        //...
    }
}

迭代器和容器的生命周期关联起来,基于rust的借用规则,增强了接口的安全性.

开发指南.

详细内容参见cargo doc生成的文档.

可参考examples目录下的样例,模板类可参考hicc-std的实现.

约束: 依赖c++11及更高版本的特性.

Dependencies

~195–630KB
~15K SLoC