rust-lang / rust-lang/rfcs

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

Open
#2,017 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-compiler T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

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;

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior described in the linked gist and Rust playground, then examine the proposed signatures for mem::size_of_val and mem::align_of_val. Research whether their operations require dereferencing the input and consider the claimed backward-compatibility impact. Done means the RFC question has a technically supported decision.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.