Is there a reason that std::ptr requires T: Sized?


#1

I’m trying to work with raw pointers to a type, T, which is not guaranteed to be Sized, but unfortunately ptr::null(), ptr::null_mut(), the is_null() method on *mut T, etc all require that T: Sized. Is there a reason for this?


#2

I can see at least three functions that require size: copy, copy_nonoverlapping, and write_bytes.

I never used std::ptr myself so I don’t know if these functions are required or if they could be moved to a more specialized std::ptr.


#3

I think the reason is that unsized pointers are usually (always?) “fat” pointers, with a second word that can’t be generalized. e.g. *const [T] and *const str have a length, and a trait object like *const Iterator has a vtable pointer.


#4

Ah, right you are on both accounts! I wrote up an example which inspects the fat pointers and you get exactly what you’d expect: https://is.gd/Q7H4DD