为什么这些 From<A> 和 From<B> 实现会导致重复实现错误?

问题描述 投票:0回答:1

背景

我有一个类型

Signal<T>
,我想为
From<A>
From<B>
创建两个实现
Signal<T>

具体来说,我希望能够从

Signal<T>
或给出
T
的函数创建
T
。我在下面编写了一些示例代码,这里是相同代码的
rust
1个回答
0
投票

当前提议的专业化是不可能的,因为这两个 impl 都不是更具体,RFC 包含一个类似的示例:

// Overlap: instantiate T = &'a U.
impl<T> Bar<T> for T {}         // Neither is more specific
impl<'a, T, U> Bar<T> for &'a U
    where U: Bar<T> {}

此外,提供反例是没有意义的(编译器不会检查是否找到一个反例,只有在类型允许的情况下)无论如何,这里是一个:

#![feature(fn_traits)]
#![feature(unboxed_closures)]
struct Foo;
impl FnOnce<()> for Foo {
    type Output = Foo;
    extern "rust-call" fn call_once(self, (): ()) -> Foo {
        Foo
    }
}
impl FnMut<()> for Foo {
    extern "rust-call" fn call_mut(&mut self, (): ()) -> Foo {
        Foo
    }
}
impl Fn<()> for Foo {
    extern "rust-call" fn call(&self, (): ()) -> Foo {
        Foo
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.