问题描述
我试图在 Rc
周围包装一个 DST,目的是克隆它并从代码的各个部分访问它,但在编译时出现以下错误。
这是错误 (playground) 的最小可重现示例:
use std::rc::Rc;
trait Trait<'a> {
fn return_hello(&self) -> &'a str;
}
impl<'a,F> Trait<'a> for F
where
F: Fn() -> &'a str
{
fn return_hello(&self) -> &'a str {
self()
}
}
impl<'a,T> Trait<'a> for Rc<T>
where
T: Trait<'a>
{
fn return_hello(&self) -> &'a str {
(**self).return_hello()
}
}
fn caller<'a,T>(t: T)
where
T: Trait<'a>
{
print!("{}\n",t.return_hello());
}
fn main() {
fn test1<'a>() -> &'a str {
"Hello from function"
}
let test2 = move || "hello from closure";
fn f<'a>() -> &'a str {
"Hello from Rc"
}
let test3: Rc<dyn Trait<'_>> = Rc::new(f);
caller(test1);
caller(test2);
caller(test3);
}
给出:
error[E0277]: the trait bound `Rc<dyn Trait<'_>>: Trait<'_>` is not satisfied
--> src/main.rs:45:12
|
25 | fn caller<'a,T>(t: T)
| ------ required by a bound in this
26 | where
27 | T: Trait<'a>
| --------- required by this bound in `caller`
...
45 | caller(test3);
| ^^^^^ the trait `Trait<'_>` is not implemented for `Rc<dyn Trait<'_>>`
|
= help: the following implementations were found:
<Rc<T> as Trait<'a>>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)