Mar*_*ark 0 types rust borrow-checker borrowing
我的代码如下所示:
pub enum Cache<'a, T> {
Pending(&'a dyn FnOnce() -> T),
Cached(T),
}
impl<'a, T> Cache<'a, T> {
pub fn get(&self) -> &mut T {
// This caches and borrows the T
}
}
impl<'a, T> PartialEq for Cache<'a, T>
where &'a mut T: PartialEq {
fn eq(&self, other: &Self) -> bool {
self.get().eq(other.get())
}
}
Run Code Online (Sandbox Code Playgroud)
但实施Eq失败:
pub enum Cache<'a, T> {
Pending(&'a dyn FnOnce() -> T),
Cached(T),
}
impl<'a, T> Cache<'a, T> {
pub fn get(&self) -> &mut T {
// This caches and borrows the T
}
}
impl<'a, T> PartialEq for Cache<'a, T>
where &'a mut T: PartialEq {
fn eq(&self, other: &Self) -> bool {
self.get().eq(other.get())
}
}
Run Code Online (Sandbox Code Playgroud)
我想我在概念上误解了一些东西。
您可以通过查看特征中方法&&mut T的定义来理解为什么 Rust 需要 an :eq()PartialEq
fn eq(&self, other: &Rhs) -> bool;\nRun Code Online (Sandbox Code Playgroud)\n\n该方法的参数类型为&Self和&Rhs;由于Rhs默认为Self且您没有在特征绑定中指定任何其他内容,因此两个参数都应为 类型&Self。
现在Self这个例子是什么?你的特质界限是这样的:
&'a mut T: PartialEq\nRun Code Online (Sandbox Code Playgroud)\n\n因此编译器可以使用的唯一PartialEq实现是 type 的实现&'a mut T,所以这就是Self;&Self反过来,必须是&&'a mut T,这正是编译器所期望的。
您可能想要的特征T是:
impl<'a, T> PartialEq for Cache<'a, T>\nwhere\n T: PartialEq,\n{\n fn eq(&self, other: &Self) -> bool {\n self.get() == other.get()\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n另请注意,您可以简单地使用==而不是显式调用eq(). 它使得获得正确的类型变得更容易,因为编译器将隐式地获取参数的引用 \xe2\x80\x93a == b扩展为PartialEq::eq(&a, &b).
| 归档时间: |
|
| 查看次数: |
1859 次 |
| 最近记录: |