Why `Arc::get_mut` takes a `&mut Self`?

When we want to extract the value inside the Arc, we usually use:

pub fn get_mut(this: &mut Self) -> Option<&mut T>;

It's very rare to me that using a &mut Arc<T>, I wondered why this function must take a &mut Self? What's the danger if it takes a &Self?

If it took &self then you would still be able to dereference the Arc with deref and get another another reference to the same T, thus breaking the borrowing rules.

1 Like

It would also be possible to clone the Arc and get simultaneous access that way.

1 Like

Sorry for asking a silly question, I'v been coding for hours without sleeping, and now I'm experiencing some serious brain fog :rofl:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.