2023-04-23 11:03:09 +00:00
|
|
|
//@run-rustfix
|
2022-05-21 11:24:00 +00:00
|
|
|
#![allow(unused, clippy::redundant_clone, clippy::derive_partial_eq_without_eq)] // See #5700
|
2020-07-14 12:59:59 +00:00
|
|
|
|
|
|
|
// Define the types in each module to avoid trait impls leaking between modules.
|
|
|
|
macro_rules! impl_types {
|
|
|
|
() => {
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub struct Owned;
|
|
|
|
|
|
|
|
pub struct Borrowed;
|
|
|
|
|
|
|
|
impl ToOwned for Borrowed {
|
|
|
|
type Owned = Owned;
|
|
|
|
fn to_owned(&self) -> Owned {
|
|
|
|
Owned {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::borrow::Borrow<Borrowed> for Owned {
|
|
|
|
fn borrow(&self) -> &Borrowed {
|
|
|
|
static VALUE: Borrowed = Borrowed {};
|
|
|
|
&VALUE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only Borrowed == Owned is implemented
|
|
|
|
mod borrowed_eq_owned {
|
|
|
|
impl_types!();
|
|
|
|
|
|
|
|
impl PartialEq<Owned> for Borrowed {
|
|
|
|
fn eq(&self, _: &Owned) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compare() {
|
|
|
|
let owned = Owned {};
|
|
|
|
let borrowed = Borrowed {};
|
|
|
|
|
|
|
|
if borrowed == owned {}
|
|
|
|
if borrowed == owned {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only Owned == Borrowed is implemented
|
|
|
|
mod owned_eq_borrowed {
|
|
|
|
impl_types!();
|
|
|
|
|
|
|
|
impl PartialEq<Borrowed> for Owned {
|
|
|
|
fn eq(&self, _: &Borrowed) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare() {
|
|
|
|
let owned = Owned {};
|
|
|
|
let borrowed = Borrowed {};
|
|
|
|
|
|
|
|
if owned == borrowed {}
|
|
|
|
if owned == borrowed {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue_4874 {
|
|
|
|
impl_types!();
|
|
|
|
|
|
|
|
// NOTE: PartialEq<Borrowed> for T can't be implemented due to the orphan rules
|
|
|
|
impl<T> PartialEq<T> for Borrowed
|
|
|
|
where
|
|
|
|
T: AsRef<str> + ?Sized,
|
|
|
|
{
|
|
|
|
fn eq(&self, _: &T) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Borrowed {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "borrowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compare() {
|
|
|
|
let borrowed = Borrowed {};
|
|
|
|
|
|
|
|
if borrowed == "Hi" {}
|
|
|
|
if borrowed == "Hi" {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|