Would anyone point me to where this could brake stuff.
It's probably to avoid adding additional methods to RefMut
and Ref
as they're supposed to be transparent smart pointers. This is why, e.g., RefMut::map
is a static method.
For the map
I agree, but this is not the case for simple dereferncing. As they are transparent smart pointers the do the reference, the same as AsRef
. Ref
impl Deref
and same for RefMut
while AsRef
is more or less similar to what Deref
does, except it is apply able to non reference/pointers. Its almost like a superset of that and it is not implemented.
Unfortunately, implementing AsRef
would add an as_ref
method to all instances of Ref
/RefMut
as AsRef
is in the prelude (automatically imported). Basically, my_ref.as_ref()
would call AsRef::as_ref
on the Ref
, not the inner value.
Why not have smth like:
impl<Outer, Inner> AsRef<Inner> for Ref<Outer> where Outer: AsRef<Inner> {
fn as_ref(&self) -> &Inner {
self.deref().as_ref()
}
}
This will make as_ref
call on the inner of the Ref
and also be implemented for many common types.
I think this is fine though because of these impls:
impl<'a, T, U> AsRef<U> for &'a T where T: AsRef<U> + ?Sized, U: ?Sized
impl<'a, T, U> AsMut<U> for &'a mut T where T: AsMut<U> + ?Sized, U: ?Sized
ref.as_ref()
can be coerced to the type the inner type AsRef
s to. It might break some existing type inferences.
I could be wrong but I think this just hasn't been considered before.