【发布时间】:2019-03-10 10:35:59
【问题描述】:
我有一些代码,其中有许多完全限定语法的实例;举个例子:
mod hal {
pub trait Backend {
type Device;
}
}
mod back {
pub struct Backend {}
impl ::hal::Backend for Backend {
type Device = i32;
}
}
fn main() {
let d: back::Backend::Device = 0;
}
为了避免如下错误:
error[E0223]: ambiguous associated type
--> src/main.rs:16:12
|
16 | let d: back::Backend::Device = 0;
| ^^^^^^^^^^^^^^^^^^^^^ ambiguous associated type
|
= note: specify the type using the syntax `<back::Backend as Trait>::Device`
有没有一个好方法可以给SomeType as SomeTrait 取别名?然后,只要需要这个完全限定语法的实例,我就可以写:
<S>::associated_fn(...)
请注意,此错误不会发生,因为某些 trait 的定义实际上有多个实现(根据 Rust 编程语言,这是 FQS 应该处理的)。
【问题讨论】:
-
为什么要从“许多完全限定语法的实例”开始?
-
@Shepmaster 我正在使用一个名为
gfx-rs的库,它有一个名为hal(硬件抽象层)的东西。hal确实是一堆 trait,这些 trait 由平台特定的后端(例如 vulkan、directx 等)实现。因此,我经常不得不写<gfx_vulkan::backend::Backend as hal::Backend>::some_fn,以实现某些特定功能。 -
这些都不能说明为什么你必须使用 FQS 而不是直接调用方法。是否有多个特征定义
some_fn? -
@Shepmaster 这是我对库不太了解的地方,因为在我看来
hal没有实现定义某些关联函数或类型的多个特征......但是,编译器当 FQS 被忽略时,抱怨接收到一个模棱两可的类型。我现在在问题中包含了一个错误示例。 -
@Shepmaster 所以我能够确认错误的发生不是因为某些定义有多个 impl;这是一个重现相同错误的最小示例(由项目 gitter 上的某人提供):play.rust-lang.org/…
标签: syntax rust type-alias