Git Product home page Git Product logo

rust-cpp's Introduction

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

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.

Differences with the cxx crate

This crate allows to write C++ code "inline" within your Rust functions, 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 might be helpful when trying to call to a C++ library and that one may wish to make plenty of call to small snippets. It can otherwise be fastidious to write and maintain the boiler plate for many small functions in different places.

These crate can also be used in together. The cxx crate offer some useful types such as CxxString that can also be used with this crate.

The cxx bridge does more type checking which can avoid some classes of errors. While this crate can only check for equal size and alignment.

rust-cpp's People

Contributors

ahmed-masud avatar aprilwade avatar curiousleo avatar efyang avatar fneddy avatar hunger avatar inokawa avatar jeffvandyke avatar mystor avatar ogoffart avatar ratijas avatar richardeoin avatar rumblefrog avatar spacemaniac avatar sthiele avatar twistedfall avatar waywardmonkeys avatar wicast avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.