Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Rc::downgrade(this: &Self) instead of Rc::downgrade(&self)

Tags:

rust

Is there any particular reason why the type signature of Rc::downgrade doesn't use &self and instead this: &Self as parameter?

To avoid Naming pollution when derefing? But why does Weak then use Weak::upgrade(&self) instead of Weak::upgrade(this: &Self)

like image 392
Philipp Mildenberger Avatar asked Sep 11 '25 19:09

Philipp Mildenberger


1 Answers

To avoid Naming pollution when derefing?

Exactly. Lots of built-in containers like Rc and Box try very hard to minimize the number of inherent functions defined on them, favoring associated functions instead to allow the structure to be as transparent as possible.

Weak, on the other hand, does not implement Deref. You must consciously try to get the value from it, using upgrade, and that function can fail (a Deref impl should never fail) if the value pointed to has already been dropped.

like image 200
Silvio Mayolo Avatar answered Sep 14 '25 09:09

Silvio Mayolo