我正在尝试制作自己的自定义类型指针,但我似乎无法使其具有可比性。我把我的代码缩小到这个:
use std::marker::PhantomData;
#[derive(PartialEq, Copy, Clone)]
pub struct PhantomPair<PHANTOM, REAL: Copy + Clone + PartialEq> {
real: REAL,
phantom: PhantomData<PHANTOM>,
}
impl<PHANTOM, REAL: Copy + Clone + PartialEq> PhantomPair<PHANTOM, REAL> {
pub fn new(data: REAL) -> Self {
PhantomPair {
real: data,
phantom: PhantomData,
}
}
}
fn is_eq<PHANTOM, REAL: Copy + Clone + PartialEq>(
a: PhantomPair<PHANTOM, REAL>,
b: PhantomPair<PHANTOM, REAL>,
) -> bool {
a == b
}
fn main() {}
编译器出现以下错误:
error[E0369]: binary operation `==` cannot be applied to type `PhantomPair<PHANTOM, REAL>`
--> src/main.rs:22:5
|
22 | a == b
| ^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `PhantomPair<PHANTOM, REAL>`
我希望PhantomPair
拥有使用PartialEq
的{{1}}的派生REAL
。据我所知,PartialEq
也实现PhantomData
基本上假设相等。
当我尝试将PartialEq
添加到PartialOrd
和#[derive]
的约束时,会出现同样的问题。