The exponent `A` is not implemented for type` A`

I am trying to use a feature that has a function that takes a closure as an argument, and then use it in the feature object.

trait A { fn f<P>(&self, p: P) where P: Fn() -> (); } struct B { a: Box<A> } impl B { fn c(&self) { self.af(|| {}); } } 

This snippet generates the following error:

 the trait `A` is not implemented for the type `A` [E0277] 

Version rustc - rustc 1.0.0-beta.3 (5241bf9c3 2015-04-25) (built 2015-04-25) .

+5
source share
1 answer

The problem is that the f method is not object-safe, because it is general, and therefore it cannot be called on the object-object. You will have to force your users to close in the box:

 trait A { fn f(&self, p: Box<Fn() -> ()>); } 

I wonder why Rust allows Box<A> in the first place, I would expect an error there. And this particular mistake is really misleading. I would make a mistake about it.

Alternatively, you can discard feature objects in favor of regular limited generics, although this is not always possible.

+7
source

All Articles