FFI C++ help, wrapping Clipper2

Hi! I'm working on wrapping the C++ library Clipper2 which is a polygon boolean/clipping/offsetting library.

I want to make the functionality of that library available for the Rust community, as it would be useful for 2D polygon union/intersection/offsetting, for vector graphics, gamedev and so on. I think others might find this useful as well.

While I have experience with Rust, I have less so with C++ and FFI/bindgen.

I have some starter code at refactor: remove extra copy and simplify api by tirithen · Pull Request #4 · tirithen/clipper2 · GitHub and while cargo test compiles it crashes with a fatal runtime error: Rust cannot catch foreign exceptions.

The src/tests.rs is a good starting point to see the kind of API that I have in mind once the FFI parts has been figured out.

I need to find people that could help me setting up the basic FFI for a Paths struct so that it can be passed as an argument as well as used as a return type. The initial goal is to get the a union function working, then that setup should be easy to adapt to other functions like intersect and so on.

I currently am trying to map memory between C++

struct PathsC
{
    std::vector<Point> points;
    std::vector<size_t> path_starts;
    size_t num_paths;
};

And Rust

#[derive(Debug, Clone)]
pub struct Paths {
    points: *const Point,
    path_starts: *const usize,
    num_paths: usize,
}

With the help of bindgen, and after trying back and forth I feel a bit stuck and decided to search for help from others that know a bit more of FFI and the combination of C++ and Rust.

To alternative and better solutions for representing C++ std::vector types with zero (or close to zero) copy in Rust as I want to create a simple Rust API that matches the Clipper2 library API as closely as possible but in Rust. That end goal is the primary focus. It would be interesting to see how close the current code is to something reliable and useful.

Does anyone have advice on how/where to get help, or concrete suggestions for fixes in the code itself?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.