-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make mem::size_of_val and mem::align_of_val take raw pointers instead of reference #2017
Comments
Somewhat-relevant RFC: #1524 (Custom DSTs) |
You are saving vtable and reconstructing your reference with it, but it's the vtable that actually contains size information, so your example doesn't show anything. It works because you explicitly preserve the part that holds the size. Does null raw pointer have a vtable, or a slice length? I strongly doubt that. |
@le-jzr Hmm... I didn't think about just calling Still, even if it's not a good idea for those functions to take |
But it needs to point to something valid, that's the whole point. You are querying the size of that "something valid". If you don't want to query a valid object, you can use |
You can't use |
How is that supposed to work for array (slices)? It looks like in that case you are required yo use a fat pointer, otherwise it is impossible to know the size. |
What does it mean to get |
In |
We haven't defined whether raw unsized pointers need to have valid metadata, I don't think. @SimonSapin You can go further than that and just try |
If you want to allow a null pointer, just use |
I've been thinking about this again recently. As some of you have pointed out, In fact, the only way to get a null pointer to a DST is to cast from a sized type, e.g. One concern that might come up, is the possibility that raw pointers to DSTs might one day possibly have a thin representation, where the pointer would have to be dereferenced to get at size/alignment info in the metadata. If thin pointers are implemented, though, I'm pretty sure they would have to be (or at least, really should be) a separate type from |
@mikeyhew currently the
fails with error[E0607]: cannot cast thin pointer Regarding my concerns, it is still not clear to me how to provide the number of elements of the slice when performing the cast (unless you manually and unsafely construct the pointer/length pair and transmute it to a slice). |
@ranma42 you need to cast from a type that implements let slice = ptr::null::<[i32; 3]>() as *const [i32]; The way it works is, the compiler provides the implementation of |
This would allow you to use null pointers to get the size of a value. As far as I know, these methods do not actually dereference the
&T
that is passed in. Here is a gist and a playground link that shows that they work just fine with null pointers, as long as you cast them to references so that it will type check.I can't think of any reason that the pointer being passed in would actually need to be dereferenced. My understanding is that the pointer itself, along with its type, are all that are needed to give the size and alignment information.
Changing the signature to the following should be possible without breaking any backward compatibility, because
&T
is implicitly coerced to*const T
.The text was updated successfully, but these errors were encountered: