我可以在Rust中借用指向共享特征的指针吗?

rod*_*igo 6 rust rust-obsolete

借来的指针教程(破碎),有点修改:

struct Point {x: float, y: float}

fn compute(p1 : &Point) {}

fn main() {
    let shared_box : @Point = @Point {x: 5.0, y: 1.0};
    compute(shared_box);
}
Run Code Online (Sandbox Code Playgroud)

一切都很好,因为共享框是自动借用的功能.

但做一个特点:

struct Point {x: float, y: float}
trait TPoint {}

impl TPoint for Point {}

fn compute(p1 : &TPoint) {}

fn main() {
    let shared_box : @TPoint = @Point {x: 5.0, y: 1.0} as @TPoint;

    compute(shared_box);
    //      ^~~~~~~ The error is here
}
Run Code Online (Sandbox Code Playgroud)

它失败了,(编译器版本0.6)说:

错误:不匹配的类型:预期&TPoint但找到@TPoint(特征存储不同:预期但发现@)

这是编译器中的错误吗?或者是不允许使用特征的借用指针?

如果答案是后者,那为什么呢?

pnk*_*lix 4

这是当前版本 Rust 中的一个已知错误:

#3794:转换为特征不会自动强制为&T类型

已经有一些工作试图解决这个问题,但有一些技术细节需要解决;感兴趣的各方可以在 Pull Request 4178 上看到一些讨论(几个月前的内容)。