In unsafe Rust, there has long been a lack of ergonomics. With advancements such as raw_ref_op we are coming closer to a more ergonomic unsafe Rust.
Auto-deref does not operate on raw pointers. This is because we want a clear boundary between unsafe and safe. We want dereferencing of raw pointers to be explicit. There is a reason that auto-deref exists for references - ergonomics.
It is possible to have both a clear boundary and ergonomics for raw pointers. The problem stems from the affix kind of the asterisk operator - prefix. Because the dot operator has higher precedence, we are left with excess parentheses. Example:
(*(*(*pointer.add(5)).some_field).method_returning_pointer()).other_method()
What we need here is either a suffix operator, or an infix operator. With the already existing RArrow token, we could have
pointer.add(5)->some_field->method_returning_pointer()->other_method()
This is identical to C and C++, which is also great.
One common non-solution to this problem is "encapsulate your unsafe code". First off, it does not address the ergonomics. Second, it is not possible in domains with irreducible encapsulations. Most notable prevalent domains with irreducible encapsulations are:
- Non-trivial intrusive data structures.
- Interoperability with complex systems written in C.
Note that the proposed ergonomic operates on place expressions. It is important that it operates on place expressions, because it is the only way to completely eliminate excess parentheses. Suppose we used the Tilde token for obtaining pointers to fields. Then the example above would be written as:
(*(**pointer.add(5)~some_field).method_returning_pointer()).other_method()
Which does not solve the problem.