dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.8k stars 325 forks source link

Better integration between derives and operator overloads #109

Open dtolnay opened 4 years ago

dtolnay commented 4 years ago

Many integration opportunities here.

dtolnay commented 3 years ago

Update: cxx 1.0.6 covers the first of the 3 bullets above. We support derive(PartialEq) \<-> operator== operator!=; derive(PartialOrd) \<-> operator< operator<= operator> operator>=; and derive(Hash) \<-> template <> struct std::hash<T>.

yan-zaretskiy commented 3 years ago

I check for a new cxx release every morning, every new feature is so helpful! Do you plan to add devive support to shared enums? It looks like PartialEq works, but PartialOrd does not.

dtolnay commented 3 years ago

derive(PartialOrd, Ord) for shared enums is fixed in 1.0.9.

benruijl commented 3 months ago

What is the current recommended way of overloading operators in C++ for a Rust opaque type (for example the + operator)? For example, I have:

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        type Atom;
        fn add_atom(at1: &Atom, at2: &Atom) -> Box<Atom>;
    }
}

and I want to add something like

rust::cxxbridge1::Box<Atom> operator+(const Atom &other)
{
    return add_atom(this, other);
}

to the auto-generated code for struct Atom final : public ::rust::Opaque.