rust - 使用 PhantomData 时无法按预期推导出 PartialEq
问题描述
我正在尝试制作自己的自定义类型指针,但似乎无法使其具有可比性。我将我的代码缩小到这个:
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
使用REAL
's的派生PartialEq
。据我所知,PhantomData
也PartialEq
基本上实现了假设平等。
当我尝试添加PartialOrd
to#[derive]
和 toREAL
的约束时,也会发生同样的问题。
解决方案
推荐阅读
- python - 通过 win32com 使用 Outlook,突然出现运行时错误 '_Folders' object is not callable`
- java - 将字符串结果分配给未知大小的数组
- java - 在 Java 中解析时间戳
- kubernetes-helm - 在 helm 模板中查找 configmap 值
- selenium - 使用 puppeteer 的 wsEndpoint 自动连接 chrome 浏览器以重新连接到打开的窗口的安全问题
- python - 使用 ipaddress 模块从主列表中排除子网?
- azure-files - Azure Monitor:如何获取特定文件共享的 UsedCapacity?
- quarkus - 如何在 Quarkus 中运行异步数据库任务
- javascript - 手动发布文件数组:Dropzone
- java - 删除字符串字符中的正则表达式