Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `*mut T` implement `Unwindsafe` but `&mut T` doesn't?

In the documentation for Unwindsafe we have:

Types such as &mut T and &RefCell<T> are examples which are not unwind safe. The general idea is that any mutable state which can be shared across catch_unwind is not unwind safe by default. This is because it is very easy to witness a broken invariant outside of catch_unwind as the data is simply accessed as usual.

Following this logic it seems to me that *mut T should not be Unwindsafe. But it turns out that it is. Why is that?

like image 231
little-dude Avatar asked Oct 15 '22 02:10

little-dude


1 Answers

*mut T being a raw pointer, it has no invariant whatsoever.

It can be null, point to invalid memory, it is Copy, and you can have two of them pointing to the same area in memory.

There is nothing you can do mutably and safety with a *mut T anyway, so it has no reason not to be Unwindsafe.

like image 73
mcarton Avatar answered Oct 19 '22 10:10

mcarton