Two things I had brewing the last few days. Firstly, this trait would allow us to improve some parts around treatment of pointers by introducing a very simple wrapper type, not unlike NonNull:
/// A pointer to an allocated memory region that has no aliases.
///
/// This acts similar to a reference but does not enforce the referenced
/// region to be valid, while not allowing you to read.
#[repr(transparent)]
pub struct InitPtr<'a, T: ?Sized>(NonNull<T>, PhantomData<&'a ()>);
impl<T: ?Sized> InitPtr<'_, T> {
/// Construct a new instance.
///
/// The pointed-to region must be valid, mutably accessible and not
/// aliased for the chosen lifetime. It need not be initialized.
pub unsafe fn new(ptr: NonNull<T>) -> Self { InitPtr(ptr, PhantomData) }
/// Replace the data at the pointed-to place.
///
/// If previously initialized, the value is not dropped.
pub fn write(&self, data: T) where T: Sized {
/// SAFETY: opted-into by constructing the instance.
unsafe { pointer::write(self.0.get(), data) }
}
}
Basically, it encapsulates the ability to safely perform llvm inbounds access to the type’s fields and to initalize the memory pointed to. Therefore we could give it a safe impl of Project with highly useful semantics, unlike a raw pointer, in which we nevertheless exploit the performance benefits etc. And whatever code is normally used to initialize a T can also safely initialize a InitPtr<T>. All of the unsafety is opted-into at the call to new.
What this would also improve is the current design of MaybeUninit. Currently, the only safe access is to a raw pointer. But that looses the information that the pointed-to region is definitely valid since it points inside the object itself. Returning a InitPtr instead (or additionally) would allow one to safely construct pointers to individual fields without ever having to resort to raw pointer dereferencing and the necessary unsafe block from that. The only point with actual unsafe code would be if one were to take out the initialized value afterwards, or convert to a reference but that is not on us.
There are two unresolved questsions here: Firstly, the type would not be able to traverse to fields of packed structs but neither could normal references atm. One similar trait to the PinnedField could allow opt-in for some members. Alternatively, an intermediate pointer type would initialize with pointer::write_unaligned instead and a new sub-trait of Field would assert the alignedness which would make references fine.
Secondly, the access is far from ergonomic for unions or types with tuple members, since each type and member would need to be referenced explicitely. An operator with new syntax could solve this that is reasonably close the field access, but one thing is missing. The base type which is projected does, under the design discussed above, not have to commit to a type for which to do the field lookup.
// `field` of which type?
some_ptr~field
- How to find the instance for
field given the pointer type.
- Is it maybe fine to defer to simply
Deref drilling to find the type which has such a field? If so, which Project instance should be chosen?
- Should it be possible to implement
Project for fields of more than one type? If not, must it be consistent with Deref?
- Or make some new
trait Pointer { type Pointee; } and use Pointee to resolve the field in that syntax.
- Maybe we should restrict ourselves to
field from the top-most pointed type, and not recursively search through auto-deref types. The reason is that many auto-deref results are not within the memory region of the original type and many pointer types are not capable of that (except &_).