-
Notifications
You must be signed in to change notification settings - Fork 24
Description
Proposal
Problem statement
The Rust standard library provides the Layout type for use with the allocator API. Of the functions in rust-lang/rust#55724, some are already marked as const, but the following currently are not:
align_topad_to_alignrepeatextendrepeat_packedextend_packedarray
This makes them unusable in const contexts.
Motivation, use-cases
Layout is also documented by example as being able to calculate the offsets of fields in a #[repr(C)] struct. This is useful since there is no equivalent of C's offsetof in the standard library, and users must rely on a third party crate.
Layout can also be used to correctly determine the offset of trailing elements in a dynamically sized type. It may be desirable to know this offset at compile time (as a const variable). I believe this can currently be emulated by including a zero-length array of the trailing element in the struct definition and using std::mem::size_of, but the intent is not as clear.
Solution sketches
Add the const keyword to the above function definitions. A few functions currently use the std::cmp::max function which relies on the Ord trait, which can't be used in const contexts. So std::cmp::max would need to be replaced by a plain if-else block expression.
Links and related work
- Tracking issue for alloc_layout_extra rust#55724
- Make functions part of
alloc_layout_extra(#55724) const. rust#99016
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.