Giter VIP home page Giter VIP logo

Comments (5)

dtolnay avatar dtolnay commented on August 23, 2024

Yes, this is equivalent to #1332. You need to provide an ABI-compatible Rust definition of the type using ExternType.

from cxx.

djlalo08 avatar djlalo08 commented on August 23, 2024

Ah, I actually came across that issue in my search, but that example was going in the other direction (call a function that returns an enum, rather than takes one). I was able to replicate #1332 but haven't been able to to pass in an extern enum -- after Your response, my guess is I've just made a mistake somewhere...

In any case, thanks for responding, and for developing this awesome crate!

from cxx.

mystchonky avatar mystchonky commented on August 23, 2024

@djlalo08 did you figure out how to do this ?

haven't been able to to pass in an extern enum -- after Your response, my guess is I've just made a mistake somewhere...

from cxx.

djlalo08 avatar djlalo08 commented on August 23, 2024

Yes! Here's an example:

//src/enums.rs

//This is the C++ enum. The repr part means it's effectively being treated as an integer (corresponding to the enum variant)
#[repr(transparent)]
pub struct MyEnum(pub std::ffi::c_int);
unsafe impl cxx::ExternType for MyEnum {
    type Kind = cxx::kind::Trivial;
    type Id = cxx::type_id("MyEnum");
}

//This is a regular rust enum You can use in Rust code
pub enum MyEnumR { 
    Variant1,
    Variant2,
    Variant3
}

//lib.rs
mod enums;

#[cxx:bridge]
mod ffi {
    unsafe extern "C++" {
         include!("myheader.h");

        type MyEnum = crate::enums::MyEnum;

        fn setEnum(e: MyEnum);
    }
}

pub fn do_stuff(e_r: MyEnumR) {
    let e_cpp = MyEnum(e_r as i32);
    ffi::setEnum(e_cpp);
}

from cxx.

djlalo08 avatar djlalo08 commented on August 23, 2024

@mystchonky This is an example based off what ultimately worked for me, but I've forgotten some of the finer points on how it works by now. This example is an untested simplification of my actual full program, so if this doesn't work, let me know and I can fix the example.

from cxx.

Related Issues (20)

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.