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_to
pad_to_align
repeat
extend
repeat_packed
extend_packed
array
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
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.
Proposal
Problem statement
The Rust standard library provides the
Layouttype for use with the allocator API. Of the functions in rust-lang/rust#55724, some are already marked asconst, but the following currently are not:align_topad_to_alignrepeatextendrepeat_packedextend_packedarrayThis makes them unusable in
constcontexts.Motivation, use-cases
Layoutis 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'soffsetofin the standard library, and users must rely on a third party crate.Layoutcan 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 aconstvariable). I believe this can currently be emulated by including a zero-length array of the trailing element in the struct definition and usingstd::mem::size_of, but the intent is not as clear.Solution sketches
Add the
constkeyword to the above function definitions. A few functions currently use thestd::cmp::maxfunction which relies on theOrdtrait, which can't be used in const contexts. Sostd::cmp::maxwould need to be replaced by a plain if-else block expression.Links and related work
alloc_layout_extra(#55724) const. rust#99016What 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.