Question about provenance

The type doesn't affect provenance. The key is whether you already had a pointer to the entire allocation or only to the 1st element in the first place.

Box::into_raw() gives you a pointer to the contents of the box, which is the entire slice if you had a box-of-slice. In contrast, &mut a[0] only ever points to the first element of the slice, and not to the entire slice. You probably meant a.as_mut_ptr() instead.

The pointee types are a red herring; you can cast raw pointers through an arbitrary chain of types without affecting provenance, and you'll be safe as long as the type is correct when you actually dereference the pointer (or turn it into a reference).

6 Likes