use std::ops::Deref;
struct Foo<T:?Sized>(T);
trait MyTrait{
fn show(&self);
}
impl MyTrait for i32{
fn show(&self){}
}
trait Method{
fn method(&self);
}
impl Method for Foo<dyn MyTrait>{
fn method(&self) { // #1
}
}
impl Method for i32{
fn method(&self) {
println!("for i32");
}
}
static GLOBAL:[i32;1] = [0];
impl Deref for Foo<i32>{
type Target = [i32;1];
fn deref(&self) -> &Self::Target {
//&self.0
&GLOBAL
}
}
/*
impl Method for [i32]{
fn method(&self) {
println!("for [i32]");
}
}
*/
fn main(){
let r1:&Foo<dyn MyTrait> = &Foo(0);
let r = Foo(0);
r.method();
}
In this example, r.method
won't find the method at #1
. Method call expressions - The Rust Reference says:
The first step is to build a list of candidate receiver types. Obtain these by repeatedly dereferencing the receiver expression's type, adding each type encountered to the list, then finally attempting an unsized coercion at the end, and adding the result type if that is successful.
So, I wonder whether the unsized coercion in a method call only applies to the last type that results from the dereferenced types?