Git Product home page Git Product logo

rustcxx's Introduction

rustcxx: Using C++ from Rust made easy

rustcxx is a tool allowing C++ to be used from a Rust project easily. It works by allowing snippets of C++ to be included within a Rust function, and vice-versa.

Example

#![plugin(rustcxx_plugin)]

cxx_inline! {
    #include <stdint.h>
    uint32_t square_it(uint32_t x) {
        return rust![(x: u32) -> u32 {
            println!("Rust: Squaring {}", x);
            x * x
        }];
    }
}

let x: u32 = 5;
let square = unsafe { cxx![(x: u32) -> u32 {
    std::cout << "C++: Squaring " << x << std::endl;
    square_it(x)
}] };

See the provided example for more details.

Usage

rustcxx requires a nightly version of the Rust compiler.

Add to your Cargo.toml:

[package]
build = "build.rs"

[dependencies.rustcxx_plugin]
git = "https://github.com/google/rustcxx"
branch = "unstable"

[build-dependencies.rustcxx_codegen]
git = "https://github.com/google/rustcxx"
branch = "unstable"

and create a build.rs file containing the following:

extern crate rustcxx_codegen;

fn main() {
    rustcxx_codegen::build("src/main.rs");
}

Authors

The main author is Paul Liétar.

Contributions

We gladly accept contributions via GitHub pull requests, as long as the author has signed the Google Contributor License. Please see CONTRIBUTING.md for more details.

Disclaimer

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.

rustcxx's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rustcxx's Issues

Compile error

I cloned your Repository and tried to compile the example but i got this:

[jan@helium example]$ cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
   Compiling rustcxx_common v0.1.0 (file:///home/jan/Uni/Bachelorarbeit/Programme/rustcxx/common)
   Compiling gcc v0.3.54
error[E0432]: unresolved import `syntax::abi`
  --> common/lib.rs:27:13
   |
27 | use syntax::abi::Abi;
   |             ^^^ Could not find `abi` in `syntax`

error[E0432]: unresolved import `syntax::codemap::spanned`
  --> common/lib.rs:29:61
   |
29 | use syntax::codemap::{Span, Spanned, dummy_spanned, respan, spanned, DUMMY_SP};
   |                                                             ^^^^^^^ no `spanned` in `codemap`. Did you mean to use `Spanned`?

error[E0432]: unresolved import `syntax::parse::common`
  --> common/lib.rs:35:20
   |
35 | use syntax::parse::common::SeqSep;
   |                    ^^^^^^ Could not find `common` in `parse`

error[E0412]: cannot find type `SpannedIdent` in module `ast`
  --> common/lib.rs:52:40
   |
52 |                            ident: ast::SpannedIdent) -> PResult<'a, Self::ArgValue>;
   |                                        ^^^^^^^^^^^^ not found in `ast`

error[E0412]: cannot find type `SpannedIdent` in module `ast`
  --> common/lib.rs:68:40
   |
68 |                            ident: ast::SpannedIdent) -> PResult<'a, Self::ArgValue> {
   |                                        ^^^^^^^^^^^^ not found in `ast`

error[E0412]: cannot find type `SpannedIdent` in module `ast`
  --> common/lib.rs:97:40
   |
97 |                            ident: ast::SpannedIdent) -> PResult<'a, Self::ArgValue> {
   |                                        ^^^^^^^^^^^^ not found in `ast`

error[E0412]: cannot find type `SpannedIdent` in module `ast`
   --> common/lib.rs:212:21
    |
212 |     pub ident: ast::SpannedIdent,
    |                     ^^^^^^^^^^^^ not found in `ast`

error[E0425]: cannot find function `intern` in module `token`
   --> common/lib.rs:342:57
    |
342 |         let no_mangle = ecx.meta_word(self.span, token::intern("no_mangle").as_str());
    |                                                         ^^^^^^ not found in `token`

error[E0425]: cannot find function `intern` in module `token`
   --> common/lib.rs:348:20
    |
348 |             token::intern("deny").as_str(),
    |                    ^^^^^^ not found in `token`

error[E0425]: cannot find function `intern` in module `token`
   --> common/lib.rs:349:60
    |
349 |             vec![ecx.meta_list_item_word(self.span, token::intern("private_no_mangle_fns").as_str())]);
    |                                                            ^^^^^^ not found in `token`

warning: use of deprecated item 'std::hash::SipHasher': use `std::collections::hash_map::DefaultHasher` instead
  --> common/lib.rs:24:17
   |
24 | use std::hash::{SipHasher, Hash, Hasher};
   |                 ^^^^^^^^^
   |
   = note: #[warn(deprecated)] on by default

warning: use of deprecated item 'std::hash::SipHasher': use `std::collections::hash_map::DefaultHasher` instead
   --> common/lib.rs:135:30
    |
135 |             let mut hasher = SipHasher::new();
    |                              ^^^^^^^^^^^^^^

error[E0624]: method `check` is private
  --> common/lib.rs:71:27
   |
71 |             while !parser.check(&token::Comma) &&
   |                           ^^^^^

error[E0624]: method `check` is private
  --> common/lib.rs:72:27
   |
72 |                   !parser.check(&token::CloseDelim(token::Paren)) {
   |                           ^^^^^

error[E0624]: method `parse_token_tree` is private
  --> common/lib.rs:73:41
   |
73 |                 tokens.push(try!(parser.parse_token_tree()));
   |                                         ^^^^^^^^^^^^^^^^

error[E0624]: method `parse_token_tree` is private
  --> common/lib.rs:92:29
   |
92 |             |parser| parser.parse_token_tree())
   |                             ^^^^^^^^^^^^^^^^

error[E0624]: method `check` is private
   --> common/lib.rs:120:30
    |
120 |         let args = if parser.check(&token::OpenDelim(token::Paren)) {
    |                              ^^^^^

error[E0624]: method `check` is private
   --> common/lib.rs:126:50
    |
126 |         let ret_ty = if args.is_some() && parser.check(&token::RArrow) {
    |                                                  ^^^^^

warning: use of deprecated item 'std::hash::SipHasher::new': use `std::collections::hash_map::DefaultHasher` instead
   --> common/lib.rs:135:30
    |
135 |             let mut hasher = SipHasher::new();
    |                              ^^^^^^^^^^^^^^

error[E0624]: method `parse_unspanned_seq` is private
   --> common/lib.rs:153:16
    |
153 |         parser.parse_unspanned_seq(
    |                ^^^^^^^^^^^^^^^^^^^

error[E0599]: no associated item named `Inherited` found for type `syntax::codemap::Spanned<syntax::ast::VisibilityKind>` in the current scope
   --> common/lib.rs:189:18
    |
189 |             vis: ast::Visibility::Inherited,
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^ associated item not found in `syntax::codemap::Spanned<syntax::ast::VisibilityKind>`

error[E0615]: attempted to take value of method `lo` on type `syntax::codemap::Span`
   --> common/lib.rs:221:34
    |
221 |             let lo = parser.span.lo;
    |                                  ^^
    |
    = help: maybe a `()` to call it is missing?

error[E0615]: attempted to take value of method `lo` on type `syntax::codemap::Span`
   --> common/lib.rs:223:34
    |
223 |             let hi = parser.span.lo;
    |                                  ^^
    |
    = help: maybe a `()` to call it is missing?

error[E0061]: this function takes 4 parameters but 6 parameters were supplied
   --> common/lib.rs:356:23
    |
356 |           let fn_item = ast::ItemKind::Fn(
    |  _______________________^
357 | |             decl, ast::Unsafety::Unsafe, dummy_spanned(ast::Constness::NotConst),
358 | |             Abi::C, ast::Generics::default(), self.body.clone());
    | |________________________________________________________________^ expected 4 parameters

error[E0599]: no associated item named `Public` found for type `syntax::codemap::Spanned<syntax::ast::VisibilityKind>` in the current scope
   --> common/lib.rs:365:18
    |
365 |             vis: ast::Visibility::Public,
    |                  ^^^^^^^^^^^^^^^^^^^^^^^ associated item not found in `syntax::codemap::Spanned<syntax::ast::VisibilityKind>`

error[E0063]: missing field `tokens` in initializer of `syntax::ast::Item`
   --> common/lib.rs:360:11
    |
360 |         P(ast::Item {
    |           ^^^^^^^^^ missing `tokens`

error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
   --> common/lib.rs:382:40
    |
382 |             (Some(&TokenTree::Token(_, token::Ident(ident))),
    |                                        ^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1

error[E0599]: no variant named `Sequence` found for type `syntax::tokenstream::TokenTree` in the current scope
   --> common/lib.rs:401:20
    |
401 |             (Some(&TokenTree::Sequence(..)), _, _) => unimplemented!(),
    |                    ^^^^^^^^^^^^^^^^^^^^^^^ variant not found in `syntax::tokenstream::TokenTree`

error[E0308]: mismatched types
   --> common/lib.rs:387:39
    |
387 |                 result.extend(f(span, &contents.tts));
    |                                       ^^^^^^^^^^^^^ expected slice, found struct `syntax::tokenstream::ThinTokenStream`
    |
    = note: expected type `&[syntax::tokenstream::TokenTree]`
               found type `&syntax::tokenstream::ThinTokenStream`

error[E0609]: no field `open_span` on type `&syntax::tokenstream::Delimited`
   --> common/lib.rs:392:45
    |
392 |                 result.push(respan(contents.open_span, token_to_string(&contents.open_token())));
    |                                             ^^^^^^^^^

error[E0308]: mismatched types
   --> common/lib.rs:393:48
    |
393 |                 result.extend(parse_rust_macro(&contents.tts, f));
    |                                                ^^^^^^^^^^^^^ expected slice, found struct `syntax::tokenstream::ThinTokenStream`
    |
    = note: expected type `&[syntax::tokenstream::TokenTree]`
               found type `&syntax::tokenstream::ThinTokenStream`

error[E0609]: no field `close_span` on type `&syntax::tokenstream::Delimited`
   --> common/lib.rs:394:45
    |
394 |                 result.push(respan(contents.close_span, token_to_string(&contents.close_token())));
    |                                             ^^^^^^^^^^

error[E0599]: no variant named `Sequence` found for type `syntax::tokenstream::TokenTree` in the current scope
   --> common/lib.rs:430:14
    |
430 |             &TokenTree::Sequence(..) => unimplemented!()
    |              ^^^^^^^^^^^^^^^^^^^^^^^ variant not found in `syntax::tokenstream::TokenTree`

error[E0609]: no field `open_span` on type `&syntax::tokenstream::Delimited`
   --> common/lib.rs:422:45
    |
422 |                 let open = respan(delimited.open_span, token_to_string(&delimited.open_token()));
    |                                             ^^^^^^^^^

error[E0609]: no field `close_span` on type `&syntax::tokenstream::Delimited`
   --> common/lib.rs:423:46
    |
423 |                 let close = respan(delimited.close_span, token_to_string(&delimited.close_token()));
    |                                              ^^^^^^^^^^

error[E0308]: mismatched types
   --> common/lib.rs:426:40
    |
426 |                     .chain(flatten_tts(&delimited.tts))
    |                                        ^^^^^^^^^^^^^^ expected slice, found struct `syntax::tokenstream::ThinTokenStream`
    |
    = note: expected type `&[syntax::tokenstream::TokenTree]`
               found type `&syntax::tokenstream::ThinTokenStream`

error[E0615]: attempted to take value of method `lo` on type `syntax::codemap::Span`
   --> common/lib.rs:440:57
    |
440 |     let mut last_pos = codemap.lookup_char_pos(DUMMY_SP.lo);
    |                                                         ^^
    |
    = help: maybe a `()` to call it is missing?

error[E0615]: attempted to take value of method `lo` on type `syntax::codemap::Span`
   --> common/lib.rs:446:58
    |
446 |             let pos = codemap.lookup_char_pos(token.span.lo);
    |                                                          ^^
    |
    = help: maybe a `()` to call it is missing?

error[E0615]: attempted to take value of method `lo` on type `syntax::codemap::Span`
   --> common/lib.rs:475:44
    |
475 |     let pos = codemap.lookup_char_pos(span.lo);
    |                                            ^^
    |
    = help: maybe a `()` to call it is missing?

error[E0618]: expected function, found enum variant `Err`
  --> common/types.rs:38:13
   |
38 |             Err(handler.struct_span_err(ty.span, &msg))
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a function
help: `Err` is a unit variant, you need to write it without the parenthesis
   |
38 |             Err
   |

error[E0609]: no field `identifier` on type `&syntax::ast::PathSegment`
  --> common/types.rs:63:40
   |
63 |             let name : &str = &segment.identifier.name.as_str();
   |                                        ^^^^^^^^^^

error[E0609]: no field `identifier` on type `&syntax::ast::PathSegment`
  --> common/types.rs:79:40
   |
79 |         Some((module, rest)) if module.identifier.name.as_str() == "libc" && rest.len() == 1 => {
   |                                        ^^^^^^^^^^

error[E0609]: no field `identifier` on type `syntax::ast::PathSegment`
  --> common/types.rs:80:40
   |
80 |             let name : &str = &rest[0].identifier.name.as_str();
   |                                        ^^^^^^^^^^ unknown field
   |
   = note: available fields are: `ident`, `args`

error: aborting due to 40 previous errors

Some errors occurred: E0023, E0061, E0063, E0308, E0412, E0425, E0432, E0599, E0609...
For more information about an error, try `rustc --explain E0023`.
error: Could not compile `rustcxx_common`.

To learn more, run the command again with --verbose.

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.