Here is the code that demonstrates my question:
trait T { type A; fn get(&mut self) -> Self::A; } struct Foo; impl T for Foo { type A = i32; fn get(&mut self) -> i32 { 3 } } struct Wrapped<F, V> { func: F, cached: Option<V>, } impl<F, R> T for Wrapped<F, R::A> where F: FnMut() -> R, R: T, R::A: Copy, { type A = R::A; fn get(&mut self) -> R::A { let v: R::A = self.cached.unwrap_or_else(|| (self.func)().get()); self.cached = Some(v); v } } fn make_foo() -> Foo { Foo } fn main() { let mut wrapped: Wrapped<fn() -> Foo, i32> = Wrapped { func: make_foo as fn() -> Foo, cached: None, }; T::get(&mut wrapped); // the following does not compile: WHY? // wrapped.get(); }
Rust playground
Shouldn't the syntax for calling a universal function be the same as a regular function call?
This error message is:
rustc 1.15.1 (021bd294c 2017-02-08) error[E0284]: type annotations required: cannot resolve `<_ as T>::A == _` --> <anon>:44:13 | 44 | wrapped.get(); |
I know that the problem is that I am using impl T<F, R::A> , i.e. impl for the associated type. There is a workaround avoiding this related type:
struct Wrapped<R: T, F: FnMut() -> R> { func: F, cached: Option<R::A> }
but that is not my question. I'm interested in the reason for the difference between UFCS and a regular function call.
rust
bennofs
source share