Not really. Projection on pointers must be unsafe since they do not assert that the structure to project upon is actually pointed to. But maybe the converse could be true, one trait that exposes it as an unsafe fn
and another subtrait that declares it safe.
Why would Field
need to be a trait? It's an internal so I think a const
enabled struct could suffice. That would buy us stricter typing over usize
and a value representation that one would be used-to from T::*U
. Or alternative, it could be a trait with an associated constant
with such a type:
/// Internal, const constructed by the syntax `Foo::bar` or equivalent.
struct Field<Parent, Child> {
offset: usize,
}
trait Project<T> {
type Type;
type Projection;
fn project(self, field: Field<Self::Type, T>) -> Self::Project;
}
This is more like what went through my head previously. But Type
appearing as associated type
in the trait locks Self
into choosing one base type, this is most likely a plus.