Currently, Pin
offers the unsafe map
method, which is safe to use when returning a Pin
to a struct field or other interior pointer that preserves Pin
's constraints. There is the idea of a #[derive]
-like macro to automatically generate safe accessor methods to make this nicer.
Similarly, Cell
has RFC 1789, which offers a safe conversion from &Cell<[T]>
to &[Cell<T>]
. This should also apply to struct fields, but there’s no easy way to express this in the type system.
If Pin<'a, T>
and &'a Cell<T>
were part of the language, these operations could be built in. But that’s a lot of surface area to add to the language. What if we had something like Deref
that applied to types like this, where deref
returned a *T
that the compiler would re-wrap in a Pin
/&Cell
after field projection or indexing? This could potentially be a much smaller language change to get the same ergonomic wins.
Could such a trait be shared between Pin
and &Cell
? At first glance it seems the two types of references can be projected into using the same rules. If not, it would need to be two traits, which is not much better than baking Pin
and &Cell
into the language.
Are there any other reference types that could take advantage of this? &RefCell
might be able to use a wide pointer that keeps a reference back to its counter, but I’m not sure how useful that would be. Or is it too specific? I can imagine "map
a field projection" working with Option
or Result
as well, where it wouldn’t be constrained by the same rules as Pin
/&Cell
.
Thoughts?