Skip to content

Make size an associated constant #1168

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions text/0000-associated-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
- Feature Name: associated_size
- Start Date: 2015-06-22
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary

Provide the size and alignment of a type as associated constants.

# Motivation

Constants and array bounds can only contain constants, which currently does not
include sizes of types. By making them associated constants they become more
versatile and can e. g. be used in `const fn`s.

# Detailed design

Extend the `Sized` trait by two associated constants `SIZE` and `ALIGNMENT`,
which contain the size of the type in bytes, and the preferred alignment,
respectively.

# Drawbacks

`SIZE` and `ALIGNMENT` (names still up to debate) are going to be members of
every type, as such they are a slight backward incompatiblity.

# Alternatives

An alternative name for `ALIGNMENT` would be `ALIGN`, also, one could consider
to use snake case for associated constants, which would result in `size` and
`align`.

Additionally, one could keep the current design and make these values available
via freestanding `const fn`s. However in the past, Rust tried to avoid using
free functions instead of member functions, or in this case, associated
constants.

# Unresolved questions

None.