Make all functions part of alloc_layout_extra
usable in const
contexts
#94
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
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 asconst
, 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'soffsetof
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 aconst
variable). 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
const
keyword to the above function definitions. A few functions currently use thestd::cmp::max
function which relies on theOrd
trait, which can't be used in const contexts. Sostd::cmp::max
would 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.
The text was updated successfully, but these errors were encountered: