Need FFI help, passing tests for a Rust wrapped C++ Clipper2 crate

Hi! I’m working on wrapping the C++ library Clipper2 which is a polygon boolean/clipping/offsetting library. I believe that this crate would be useful for 2D polygon graphics, gamedev and similar once I get it working. I myself have a need for the Clipper2 functionality in my cnccoder crate, but I’m sure others would also find other uses for the Rust clipper2 crate.

I’m looking for help getting the FFI parts working, I’m trying to create a Paths struct that should be able to match the memory layout of the C++ std::vector type.

Currently the cargo test code on the no-copy branch compiles, but crashes in runtime with the error fatal runtime error: Rust cannot catch foreign exceptions.

The goal is to provide a Rust API to pass the src/

fn test_union() {
    let shape1 = vec![(1.0, 1.0), (1.0, 5.0), (5.0, 5.0), (5.0, 1.0)];
    let shape2 = vec![(2.0, 2.0), (2.0, 4.0), (8.0, 4.0), (8.0, 2.0)];
    let shape3 = vec![(20.0, 20.0), (20.0, 40.0), (80.0, 40.0), (80.0, 20.0)];
    let shapes = vec![shape1, shape2, shape3];

    let paths = Paths::from(shapes);
    let result = union(&paths, FillRule::NonZero);

    assert_eq!(result.iter().count(), 2);
        Paths::from(vec![vec![(1.0, 1.0) /* fix expected output once there are no more crashes */]]).to_vec()

As I currently got a bit stuck with the skills I currently have I decided to try to reach out and get help on getting the basic FFI setup working. Where would be a good place to find people with experience with the combination of C++ and Rust need for FFI?

Either just help with advice on moving forward, or with actual code contributions.

Hi! I’m afraid this is not the best place for your request. Exercism is for learning the fundamentals of a language, FFI is quite advanced. As the maintainer of the Rust track, I have Rust experience, but you probably know more about FFI than I do! I assume you already know about this, but if I had to do C++ FFI, I would start looking at

Good luck with your project!

Thank you for taking the time to answer, I appreciate that!

Yes it is a tricky subject, I’m starting to feel like I’m getting the hang of a lot of the Rust areas by now, FFI was okay to grasp as long as I passed simple values, but when I now want to pass nested C++ std::vector types without copying the data I stumbled into the area of memory mapping across the languages and then it got really hard. Also since I don’t know C++ that well other than simpler Arduino sketches and similar.

I’ll try to see if I can simplify my problem to a bare bones example that is easier to ask about and continue seeking help elsewhere. also looks interesting, thanks for the link! I read a bit about it Yesterday but are not yet sure if that will simplify things, or add yet another layer to the challenge.

Thanks again, I really appreciate the reply, and also all the effort with the Rust track on this site, I have always gotten very good and appreciating feedback when I have submitted exercises on this site!

Keep it up!

1 Like