cpp_core/cmp.rs
1//! Comparison operator traits
2//!
3//! C++'s comparison operators have different semantics from Rust's `PartialOrd` and `Ord` traits.
4//! If all the operators (`Lt`, `Le`, `Gt`, `Ge`) are implemented for a type, the pointer types
5//! (`CppBox`, `Ptr`, `Ref`) automatically implement `PartialOrd`.
6
7/// Represents C++'s `operator<`.
8pub trait Lt<Rhs: ?Sized = Self> {
9 /// This method tests less than (for `self` and `other`) and is used by the `<` operator.
10 ///
11 /// # Safety
12 ///
13 /// The caller must make sure `self` and `other` contain valid pointers. This function
14 /// may invoke arbitrary foreign code, so no safety guarantees can be made.
15 unsafe fn lt(&self, other: &Rhs) -> bool;
16}
17
18/// Represents C++'s `operator<=`.
19pub trait Le<Rhs: ?Sized = Self> {
20 /// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
21 ///
22 /// # Safety
23 ///
24 /// The caller must make sure `self` and `other` contain valid pointers. This function
25 /// may invoke arbitrary foreign code, so no safety guarantees can be made.
26 unsafe fn le(&self, other: &Rhs) -> bool;
27}
28
29/// Represents C++'s `operator>`.
30pub trait Gt<Rhs: ?Sized = Self> {
31 /// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
32 ///
33 /// # Safety
34 ///
35 /// The caller must make sure `self` and `other` contain valid pointers. This function
36 /// may invoke arbitrary foreign code, so no safety guarantees can be made.
37 unsafe fn gt(&self, other: &Rhs) -> bool;
38}
39
40/// Represents C++'s `operator>=`.
41pub trait Ge<Rhs: ?Sized = Self> {
42 /// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
43 /// operator.
44 ///
45 /// # Safety
46 ///
47 /// The caller must make sure `self` and `other` contain valid pointers. This function
48 /// may invoke arbitrary foreign code, so no safety guarantees can be made.
49 unsafe fn ge(&self, other: &Rhs) -> bool;
50}