Comments (5)
Yes, this is equivalent to #1332. You need to provide an ABI-compatible Rust definition of the type using ExternType
.
from cxx.
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.
@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.
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.
@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)
- Question: estimate workload to call functions from geogram HOT 1
- Returning `CxxString` from Rust to C++ HOT 5
- Advice/example on docs.rs strategy HOT 2
- Feature Request: Add support for nested Vector types HOT 1
- Building two CXX C++ bindings with the same symbols but in different namespaces. HOT 2
- Including bridge from another crate
- Error in configuring crates_universe when included as a Bazel module HOT 6
- Error compiling `cxx.cc` on Windows HOT 1
- Proposal: implementing native C++ std::error_code
- Link failure when using CXX-backed function from another crate HOT 1
- Unsupported element type of Vec<[u64; 3]>
- How to include C++ files whose directory is given by an environment variable? HOT 1
- Error compiling demo to wasm on macOS HOT 5
- Is there a way to expose a C++ class that is itself a wrapper on `std::shared_ptr`?
- How can I `include!` a file in OUT_DIR
- How can I return a vector of non-trivial type of C++ to Rust?
- LNK2019 unresolved symbol for Vec<SharedStruct> using MSVC
- Using CXX in multiple DLLs always results in undefined behavior?
- Does the core safety claim need rephrasing? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cxx.