【发布时间】:2019-03-28 21:10:27
【问题描述】:
我的代码看起来像this:
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())
}
}
但实现 Eq 失败:
error[E0308]: mismatched types
--> src/lib.rs:20:23
|
20 | self.get().eq(other.get())
| ^^^^^^^^^^^ expected mutable reference, found type parameter
|
= note: expected type `&&'a mut T`
found type `&mut T`
我想我在概念上误解了一些东西。
【问题讨论】:
-
您是要比较
T对象本身,还是比较.get()返回的引用? -
@Frxstrem 在功能上,我希望 T 对象相同。
-
&'a dyn FnOnce() -> T看起来像是一个有问题的类型,因为您永远无法调用该函数。 (您需要拥有FnOnce才能调用它。)
标签: types rust borrow-checker borrowing