That is an interesting use case, I also encountered the "I want to store data in an Arc/Rc, but also want pointers into it without lifetimes" problem. I think that an Projected{Rc, Arc} does not seem too far fetched. I think it could look like this:
pub struct ProjectedRc<T, S> {
backing: Rc<S>,
ptr: NonNull<T>,
}
impl<T> Rc<T> {
pub fn project<U>(&self, map: impl FnOnce(&T) -> &U) -> ProjectedRc<U, T> {
ProjectedRc {
backing: self.clone(),
ptr: NonNull::from(map(&**self)),
}
}
}
Not sure if this is sound (if someone inputs a malicious map closure). Also not sure if the type parameters really need to be Sized.
I do not think that it is possible to project Arcs without adding any additional information.
I do not like that this will enable implicit Arc creation. This has precedent, for example here is a post that tried to allow cloning Arcs directly into closures. I do not like that foo.bar.baz.bizz can now create possibly 3 new Arcs when someone inevitably refactors one of the fields to be of that type. I want struct.field to be reserved for pointer offsets with possibly zero cost transmute-like operations to adjust the type. I hope this will be better reflected in my next update that I will post soon.