Skip to content

Make mem::size_of_val and mem::align_of_val take raw pointers instead of reference #2017

Open
@mikeyhew

Description

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.

fn size_of_val<T>(*const T) -> usize;
fn align_of_val<T>(*const T) -> usize;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    T-compilerRelevant to the compiler team, which will review and decide on the RFC.T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions