Specifying a type on self

pub trait IntoFrpNodeAny {
    fn into_rc_any(self: Rc<Self>) -> Rc<FrpNodeAny>;
}

In the code above, we see a type signature on self. What is this feature called and where is it documented?

This is called arbitrary self types and is not completely stable yet I believe.

1 Like

You can actually have a type signature on self, since it is just another method parameter. The type of self is, without the feature @OptimisticPeach linked, limited to a specific list though (since it is hard to do method resolution in the more general case):

  • Self
  • &Self
  • &mut Self
  • Box<Self>
  • Rc<Self>
  • Arc<Self>
  • Pin<P> where P is any if the above except Self

The usual ways of writing self parameters (self, &self and &mut self) are just syntactic sugar for the corresponding type annotations in the most common cases.

4 Likes

@jameseb7 : Thanks for the explicit list. This explains why

self: Rc<...> compiled fine

self: RcByLoc<..> was giving me some weird resolution error (where RcByLoc is a custom class I wrote).