Skip to content

Replace Eq and PartialEq with the C++ counterparts - #697

Merged
nunoplopes merged 20 commits into
Cpp2Rust:masterfrom
lucic71:partia-eq-as-cpp-trait
Dec 3, 2026
Merged

nunoplopes merged 20 commits into
Cpp2Rust:masterfrom
lucic71:partia-eq-as-cpp-trait

Conversation

@lucic71

@lucic71 lucic71 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Depends on #696

C++ offers WeaklyEqualityComparableWith, EqualityComparable, EqualityComparableWith.

The relationship between them is:

    pub trait WeaklyEqualityComparableWith<U: WeaklyEqualityComparableWith<Self> = Self>: Sized {                                                                    
        fn equal(this: Ptr<Self>, rhs: Ptr<U>) -> bool;                                  // t == u
        fn not_equal(this: Ptr<Self>, rhs: Ptr<U>) -> bool { !Self::equal(this, rhs) }   // t != u
    }                                                                             
                                                                                  
    pub trait EqualityComparable: WeaklyEqualityComparableWith<Self> {}           
    impl<T: WeaklyEqualityComparableWith<T>> EqualityComparable for T {}                                                                                             
                                                                                  
    pub trait EqualityComparableWith<U: EqualityComparable + WeaklyEqualityComparableWith<Self>>:
        EqualityComparable + WeaklyEqualityComparableWith<U> {}                   
    impl<T, U> EqualityComparableWith<U> for T                                    
    where                                                                         
        T: EqualityComparable + WeaklyEqualityComparableWith<U>,                  
        U: EqualityComparable + WeaklyEqualityComparableWith<T>,                  
    {}  

std::find now is written as:

    template <class T1, class T2> T1 f3(T1 first, T1 last, const T2 &value) {                                                                                        
      return std::find(first, last, value);                                                                                                                          
    }

to

    unsafe fn f3<                                                                                                                                                    
        T1: UnsafeCppInputIterator<T3>,                                                                                                                              
        T2: WeaklyEqualityComparableWithUnsafe<T3>,                                                                                                                  
        T3: WeaklyEqualityComparableWithUnsafe<T2>,                                                                                                                  
    >(a0: T1, a1: T1, a2: T2) -> T1 {

CppInputIterator is now bounded on EqualityComparable: pub trait CppInputIterator<T>: CppIterator<T> + EqualityComparable {}

@lucic71

lucic71 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Kept PartialEq on Ptr, FnPtr and AnyPtr so that we can write ==/!= in the converted code instead of going through the equal/not_equal route. Rules will still use equal/not_equal so that we have a common interface for all the types.

@nunoplopes
nunoplopes merged commit b26d80b into Cpp2Rust:master Dec 3, 2026
9 checks passed
nunoplopes pushed a commit that referenced this pull request Dec 3, 2026
Depends on #697

Closes #677 

Added LessThanComparable, TotallyOrdered and ThreeWayComparable.

LessThanComparable only requires operator<.
TotallyOrdered requires <, >, <=, >=.
ThreeWayComparable requires <=>.

---

Removed PartialOrd and Ord.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants