All Projects → mystor → Rust Cpp

mystor / Rust Cpp

Licence: other
Embed C++ directly inside your rust code!

Programming Languages

rust
11053 projects
macros
77 projects

Labels

Projects that are alternatives of or similar to Rust Cpp

cffi
Safe* C foreign function interface for Rust, using proc macros and marshaling types.
Stars: ✭ 15 (-97.01%)
Mutual labels:  ffi
gpgme
GPGme bindings for Rust
Stars: ✭ 55 (-89.04%)
Mutual labels:  ffi
C Cpp Notes
Notes about modern C++, C++11, C++14 and C++17, Boost Libraries, ABI, foreign function interface and reference cards.
Stars: ✭ 363 (-27.69%)
Mutual labels:  ffi
unixstring
An FFI-friendly null-terminated byte string
Stars: ✭ 19 (-96.22%)
Mutual labels:  ffi
uapi
Unix API
Stars: ✭ 18 (-96.41%)
Mutual labels:  ffi
ext-psi
PHP System Interface (aka FFI)
Stars: ✭ 24 (-95.22%)
Mutual labels:  ffi
swift-bridge
swift-bridge facilitates Rust and Swift interop.
Stars: ✭ 260 (-48.21%)
Mutual labels:  ffi
Chineseutil
PHP 中文工具包,支持汉字转拼音、拼音分词、简繁互转、数字、金额大写;QQ群:17916227
Stars: ✭ 413 (-17.73%)
Mutual labels:  ffi
renderdoc-rs
RenderDoc application bindings for Rust
Stars: ✭ 28 (-94.42%)
Mutual labels:  ffi
Z Engine
⚡️ PHP Engine Direct API
Stars: ✭ 362 (-27.89%)
Mutual labels:  ffi
mozilla-deepspeech-flutter
Mozilla DeepSpeech in flutter using Dart FFI
Stars: ✭ 23 (-95.42%)
Mutual labels:  ffi
ffi-libc
Useful Ruby FFI bindings for libc
Stars: ✭ 32 (-93.63%)
Mutual labels:  ffi
Win32
Build Win32 apps with Dart!
Stars: ✭ 256 (-49%)
Mutual labels:  ffi
PhpWebcam
This is a PHP library to capture webcam frames
Stars: ✭ 33 (-93.43%)
Mutual labels:  ffi
Ffi Overhead
comparing the c ffi (foreign function interface) overhead on various programming languages
Stars: ✭ 387 (-22.91%)
Mutual labels:  ffi
libserialport.dart
Serial Port for Dart
Stars: ✭ 51 (-89.84%)
Mutual labels:  ffi
cld3-ruby
cld3-ruby is an interface of Compact Language Detector v3 (CLD3) for Ruby.
Stars: ✭ 59 (-88.25%)
Mutual labels:  ffi
Haskellr
The full power of R in Haskell.
Stars: ✭ 491 (-2.19%)
Mutual labels:  ffi
Pyo3
Rust bindings for the Python interpreter
Stars: ✭ 5,110 (+917.93%)
Mutual labels:  ffi
Go Interlang
Examples of calls between Go and C/C++ (and how to call a Go shared object from Node/Ruby/Python/Java)
Stars: ✭ 346 (-31.08%)
Mutual labels:  ffi

rust-cpp - Embed C++ code directly in Rust

Build Status Build status Documentation

Overview

rust-cpp is a build tool & macro which enables you to write C++ code inline in your rust code.

let name = std::ffi::CString::new("World").unwrap();
let name_ptr = name.as_ptr();
let r = unsafe {
    cpp!([name_ptr as "const char *"] -> u32 as "int32_t" {
        std::cout << "Hello, " << name_ptr << std::endl;
        return 42;
    })
};
assert_eq!(r, 42)

The crate also help to expose some C++ class to Rust by automatically implementing trait such as Drop, Clone (if the C++ type can be copied), and others

cpp_class!{
    #[derive(PartialEq)]
    unsafe struct MyClass as "std::unique_ptr<MyClass>"
}

Usage

For usage information and in-depth documentation, see the cpp crate module level documentation.

Diference with the cxx crate

This crate allow to write C++ code "inline" while with the cxx crate, you have to write a bit of boiler plate to have calls to functions declared in a different .cpp file. Having C++ code inline with the rust code might be helpful when trying to call to a C++ library and that there are many roundtrip with small code snippet within a function. It can be fastidious to write and maintain the boiler plate for many small functions in different places, so this crate helps reducing boiler plate.

That said, these crate could be used in together. The cxx crate also offer some types such as CxxString and co. that can also be used wth this crate. The cxx bridge also does more type checking which can avoid some errors.

History

rust-cpp has had multiple different implementations. The code for these old implementations is still present in the tree today.

rustc_plugin

rust-cpp started life as a unstable compiler plugin. This code no longer builds on modern nightly rusts, but it had some features which are still unavailable on more recent versions of rust-cpp, as it was able to take advantage of the rust compiler's type inference by abusing a lint pass to collect type information.

Development on the original version ceased for 2 major reasons:

  1. The rustc internal libraries changed very often, meaning that constant maintenance work was required to keep it working on the latest nightly versions.

  2. The plugin had no support for stable rust, which is undesirable because the majority of crates are not built with the latest nightly compiler, and requiring unstable features is a deal breaker for them.

These limitations led to the development of the next phase of rust-cpp's lifetime.

stable (a.k.a v0.1)

The next phase in rust-cpp's lifetime was when it was rewritten as a syntex plugin. syntex is an extraction of the rust compiler's internal syntax library, and has support for performing procedural macro expansions by rewriting rust source files.

Performing a full rewrite of the source tree was very unfortunate, as it would mean that all compiler errors in crates which use the plugin would be reported in a generated file instead of at the original source location. Instead, this version of rust-cpp used a stable macro_rules! macro to perform the rust code generation, and a build step based on syntex to perform the c++ code generation and compilation.

Unfortunately this architecture meant that one of the neatest features, closures, was not available in this version of rust-cpp. Implementing that feature required some form of procedural code generation on the rust side, which was not possible in rust at that time without performing full text rewrites of the source code.

Macros 1.1 and syn (a.k.a. v0.2)

This is the current implementation of rust-cpp. In rustc 1.15, the first form of procedural macros, custom derive, was stabilized. Alongside this, @dtolnay implemented syn, which was a small, fast to compile, crate for parsing rust code. rust-cpp uses a fork of syn for its rust code parsing.

rust-cpp now internally uses a custom derive to implement the procedural components of the rust code generation, which means that closures are available again! It also builds much more quickly than the previous version as it no longer depends on syntex which could take a long time to build.

The fork of syn (cpp_syn) which rust-cpp uses differs from syn in that it keeps track of source location information for each AST node. This feature has not been landed into syn yet as it is a breaking change, and none of syn's other consumers would make use of it yet.

v0.5

The syn version was upgraded to syn 1.0 Since syn did not allow to access the actual source location or text, the cpp_build uses its own rust lexer, forked from the proc_macro2 crate.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].