为什么Box< T>与Fn()冲突? [英] Why does trait implementation for Box<T> conflict with Fn()?
本文介绍了为什么Box< T>与Fn()冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用std :: boxed :: Box;
struct Cow;
trait CanSpeak {
fn speak(& self);
}
impl CanSpeak for Cow {
fn speak(& self){
println!(moo);
}
}
impl< F:Fn()> CanSpeak for F {
fn speak(& self){
self();
}
}
impl< T:CanSpeak> CanSpeak for Box< T> {
fn speak(&self){
(** self).speak()
}
}
fn lol_speak(){
println!(lol)
}
fn lets_speak< T:CanSpeak>(t:& T){
t.speak();
fn main(){
let cow = Cow;
lets_speak(& cow);
lets_speak(& lol_speak);
let boxed_cow = Box :: new(Cow);
lets_speak(& boxed_cow);
$ / code>
编译失败:
test.rs:15:1:19:2错误:特质`CanSpeak`的冲突实现[E0119]
test.rs:15 impl< F:Fn( )> CanSpeak for F {
test.rs:16 fn speak(& self){
test.rs:17 self();
test.rs:18}
test.rs:19}
test.rs:15:1:19:2 help:运行`rustc --explain E0119`查看详细说明
test.rs:21:1:25:2注意:注意冲突的实现在这里
test.rs:21 impl< T:CanSpeak> CanSpeak for Box< T> {
test.rs:22 fn speak(& self){
test.rs:23(** self).speak()
test.rs:24}
test.rs:25}
错误:由于前面的错误而中止
我的问题是:
- 据我所知,
Box< T>
没有实现Fn()
trait。那么为什么上面的例子失败? - 什么是我正在尝试做的正确实现?
我刚开始学习Rust。感谢您的帮助。
解决方案
两者之间存在冲突,因为可能 code> Box< T> 具有 T
执行 CanSpeak
和 Box< T>
执行 Fn()
。 Rust一致性规则不是关于是什么,而是什么可以是。 下面是一个实现<$对于 Box< Cow>
,c $ c> Fn(),如果允许你的两个通用trait实现, >
//(在箱子上的这个属性。)
#![feature(unboxed_closures,core)]
impl Fn<()>对于Box< Cow> {
externrust-callfn call(& self,_:()){}
}
impl FnMut<()>对于Box< Cow> {
externrust-callfn call_mut(& mut self,_:()){}
}
impl FnOnce<()>对于Box< Cow> {
type Output =();
externrust-callfn call_once(self,_:()){}
}
A simplistic program to demonstrate this behavior:
use std::boxed::Box;
struct Cow;
trait CanSpeak {
fn speak(&self);
}
impl CanSpeak for Cow {
fn speak(&self) {
println!("moo");
}
}
impl<F: Fn()> CanSpeak for F {
fn speak(&self) {
self();
}
}
impl<T: CanSpeak> CanSpeak for Box<T> {
fn speak(&self) {
(**self).speak()
}
}
fn lol_speak() {
println!("lol")
}
fn lets_speak<T: CanSpeak>(t: & T) {
t.speak();
}
fn main() {
let cow = Cow;
lets_speak( &cow );
lets_speak( &lol_speak );
let boxed_cow = Box::new(Cow);
lets_speak( &boxed_cow );
}
Compilation fails with:
test.rs:15:1: 19:2 error: conflicting implementations for trait `CanSpeak` [E0119]
test.rs:15 impl<F: Fn()> CanSpeak for F {
test.rs:16 fn speak(&self) {
test.rs:17 self();
test.rs:18 }
test.rs:19 }
test.rs:15:1: 19:2 help: run `rustc --explain E0119` to see a detailed explanation
test.rs:21:1: 25:2 note: note conflicting implementation here
test.rs:21 impl<T: CanSpeak> CanSpeak for Box<T> {
test.rs:22 fn speak(&self) {
test.rs:23 (**self).speak()
test.rs:24 }
test.rs:25 }
error: aborting due to previous error
My questions are:
- As far as I can tell
Box<T>
does not implementFn()
trait. Then why does above example fail? - What is the correct implementation for what I'm trying to do?
I've just started learning Rust. Thanks for your help.
解决方案
The two do conflict, because it is possible for a type Box<T>
to have T
implementing CanSpeak
and Box<T>
implementing Fn()
. Rust coherence rules aren’t about what is but what can be.
Here’s an example of implementing Fn()
for Box<Cow>
, which would clearly explode things if it allowed your two generic trait implementations:
// (This attribute on the crate.)
#![feature(unboxed_closures, core)]
impl Fn<()> for Box<Cow> {
extern "rust-call" fn call(&self, _: ()) { }
}
impl FnMut<()> for Box<Cow> {
extern "rust-call" fn call_mut(&mut self, _: ()) { }
}
impl FnOnce<()> for Box<Cow> {
type Output = ();
extern "rust-call" fn call_once(self, _: ()) { }
}
这篇关于为什么Box< T>与Fn()冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文