I agree that the motivation may not be as fleshed out as it should be. The main reason that OaRTs should have reference magic is:
- OaRTs cannot derive traits properly. Imagine trying to implement common traits such as
Deref for a container into its OaRT, or Index/IndexMut. The problem is that these traits have methods that take by reference &self/&T, but for an OaRT, we want to take by value (self/T).
- OaRTs cannot be passed to certain generic functions that take reference arguments. The most obvious examples are ones that take two arguments
&T and &mut T. See also, functions that take multiple references with different lifetimes. This also affects trait implementations (see below).
In Rust, imagine I have to deal with a trait like the following:
trait Storage {
type Item;
fn get(&self) -> &Item;
fn get_mut(&mut self) -> &mut Item;
}
How can I implement this for my custom storage type with its OaRTs ItemRef and ItemRefMut without reference magic?
The purpose of this RST isn’t primarily for client/application code, but for libraries. C++ supports proxy-references (although not very well, and it’s probably bad style to use them). Here’s a C++ octree library that I wrote and have up on Github that you can take a look at. I resorted to using proxy-references because I had a hard restrictions on the data layout of the octree, but I wanted to meet certain C++ collection concepts (similar to implementing a Rust trait). If C++ didn’t have proxy references, I would have been out of luck.
If Rust ever adds collection traits, the same issue is going to arise then. For now things are workable but ugly, but they might not always be workable at all.