dtolnay/cxx

Better integration between derives and operator overloads

Open

#109 opened on Apr 11, 2020

View on GitHub
 (5 comments) (0 reactions) (0 assignees)Rust (253 forks)batch import
help wanted

Repository metrics

Stars
 (4,472 stars)
PR merge metrics
 (Avg merge 8m) (3 merged PRs in 30d)

Description

Many integration opportunities here.

  • If a shared struct has a #[derive(PartialEq or Ord)] then emit a compatible operator ==, operator !=, operator <, operator <=, operator >, operator >= on the type for C++.

    mod ffi {
        #[derive(PartialEq)]
        struct Key {
            n: usize,
        }
    }
    
  • If an opaque Rust type is written with trait bounds then wire up the corresponding operator for C++ in a way that delegates to Rust's implementation of the operator.

    mod ffi {
        extern "Rust" {
            type R: PartialEq;
        }
    }
    
    struct R {...}
    impl PartialEq for R {...}
    
  • If an opaque C++ type is written with trait bounds then produce a trait impl in Rust that delegates to the corresponding overloaded operator(s) in C++.

    mod ffi {
        extern "C++" {
            type C: PartialEq;  // produce a PartialEq that calls `operator ==`
        }
    }
    

Contributor guide