是否有一种方法可以为智能指针中的对象(例如Box
,Rc
等)的特征提供通用的实现?
示例:
use std::borrow::Borrow;
use std::rc::Rc;
trait KindaEqual<Rhs = Self> {
fn kinda_equal(&self, other: &Rhs) -> bool;
}
// Nice to have (still need to copy-paste for each LHS container)
impl<T, Rhs, WrappedRhs> KindaEqual<WrappedRhs> for T
where
WrappedRhs: Borrow<Rhs>,
T: KindaEqual<Rhs>,
{
fn kinda_equal(&self, other: &WrappedRhs) -> bool {
self.kinda_equal(other.borrow())
}
}
// Best to have (handles both LHS and RHS containers)
impl<WrappedT, T, Rhs, WrappedRhs> KindaEqual<WrappedRhs> for WrappedT
where
WrappedRhs: Borrow<Rhs>,
WrappedT: Borrow<T>,
T: KindaEqual<Rhs>,
{
fn kinda_equal(&self, other: &WrappedRhs) -> bool {
self.borrow().kinda_equal(other.borrow())
}
}
impl KindaEqual for f64 {
fn kinda_equal(&self, other: &Self) -> bool {
num::abs(self - other) < 0.01f64
}
}
impl KindaEqual<u64> for f64 {
fn kinda_equal(&self, other: &u64) -> bool {
num::abs(self - *other as f64) < 1f64
}
}
fn main() {
assert!(3.141592654.kinda_equal(&3.14));
assert!(3.141592654.kinda_equal(&3));
assert!(3.141592654.kinda_equal(&Rc::new(3.14)));
}
上面的错误给了我
error[E0207]: the type parameter `Rhs` is not constrained by the impl trait, self type, or predicates
--> src/main.rs:9:9
|
9 | impl<T, Rhs, WrappedRhs> KindaEqual<WrappedRhs> for T
| ^^^ unconstrained type parameter
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
--> src/main.rs:20:16
|
20 | impl<WrappedT, T, Rhs, WrappedRhs> KindaEqual<WrappedRhs> for WrappedT
| ^ unconstrained type parameter
error[E0207]: the type parameter `Rhs` is not constrained by the impl trait, self type, or predicates
--> src/main.rs:20:19
|
20 | impl<WrappedT, T, Rhs, WrappedRhs> KindaEqual<WrappedRhs> for WrappedT
| ^^^ unconstrained type parameter
我已经读到,添加关联的类型可能会解决它,但这是不希望的,因为这会强制使用trait来实现它。
答案 0 :(得分:1)
/-- everything here
impl<Types...> Trait for Self
\-- has to appear here \-- or here
正如error message所说,Types...
必须受Trait
,Self
或Trait
或Self
的谓词约束。
如果您对每个LHS容器都可以进行复制粘贴,则可以将Borrow
处理向下移动到函数本身,如下所示:
use std::borrow::Borrow;
trait KindaEqual<Rhs = Self> {
fn kinda_equal<R: Borrow<Rhs>>(&self, other: &R) -> bool;
}
impl KindaEqual<u32> for u32 {
fn kinda_equal<R: Borrow<u32>>(&self, other: &R) -> bool {
self == other.borrow()
}
}
// repeat for all supported containers
impl<T> KindaEqual<T> for Box<T>
where
T: KindaEqual<T>,
{
fn kinda_equal<R: Borrow<T>>(&self, other: &R) -> bool {
(&**self).kinda_equal(other.borrow())
}
}