Why does this code work?
trait T: std::fmt::Debug {
fn func(mut self: Box<Self>) -> Box<T>;
}
#[derive(Debug)]
struct S {
i: i32,
}
impl T for S {
fn func(mut self: Box<Self>) -> Box<T> {
self.as_mut().i += 1;
self
}
}
impl S {
fn new() -> Box<S> {
Box::new(S{i: 0})
}
}
fn main() {
let s = S::new();
let s_inc = s.func();
print!("{:?}", s_inc);
}
(Playground)
In particular, I don't understand how the type annotation mut self: Box<Self>
in
impl T for S {
fn func(mut self: Box<Self>) -> Box<T> {
// ...
}
}
works. Shouldn't self
be of type S
, not Box<S>?
Which part of the method rules allow for this syntax?