Skip to content
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

Make all functions part of alloc_layout_extra usable in const contexts #94

Closed
Raekye opened this issue Aug 31, 2022 · 3 comments
Closed
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

Comments

@Raekye
Copy link

Raekye commented Aug 31, 2022

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.

@Raekye Raekye added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Aug 31, 2022
@shepmaster
Copy link
Member

See also rust-lang/rfcs#3308

@thomcc
Copy link
Member

thomcc commented Sep 3, 2022

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.

FWIW, I think allowing Layout's functions to be used in const contexts is useful even if a const-supporting offset_of! is added to the standard library.

@Raekye
Copy link
Author

Raekye commented Mar 30, 2023

The proposed changes have been made and merged in the (later submitted) PR rust-lang/rust#102207 (note, I didn't see an ACP for said newer PR).

@Raekye Raekye closed this as completed Mar 30, 2023
@dtolnay dtolnay added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

4 participants