Suppose that I guarantee (through unspecified means) that I have an array of type [*mut T; N] containing N valid and disjoint mutable pointers, all with lifetime 'a. How can I convert this to an array of mutable references [&'a mut T; N]?
In other words, how can I implement this function?
unsafe fn ptrs_to_refs<'a, T, const N: usize>(ptrs: [*mut T; N]) -> [&'a mut T; N] {
// What goes here?
}
I'm not particularly looking for an implementation that uses an allocation step (e.g. using Vec).
Because min_const_generics is set to stabilize in Rust 1.51, this question is targetting a hypothetical stable Rust that also has min_const_generics. I am looking for answers compatible with these requirements, using no other unstable features.
E.g. with the feature array_map one can simply
unsafe fn ptrs_to_refs<'a, T, const N: usize>(ptrs: [*mut T; N]) -> [&'a mut T; N] {
arr.map(|ptr| unsafe { &mut *ptr })
}
but since that is not set to stabilize by 1.51 (as far as I know) I do not wish to use it.