Skip to content

Add methods for use cases that align_to does not cover #164

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
oli-obk opened this issue Jan 17, 2023 · 5 comments
Closed

Add methods for use cases that align_to does not cover #164

oli-obk opened this issue Jan 17, 2023 · 5 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@oli-obk
Copy link

oli-obk commented Jan 17, 2023

Proposal

Problem statement

The align_to slice method is only useful for optimizations that want to process all elements of the original slice, but has an optimized path for larger or more-aligned elements. The function may fall back to not producing a middle (aligned) slice and instead just return the original slice in the head or tail slice (for which, according to align_to's docs, an implementation must exist).

There are many users in the ecosystem that use it for guaranteed alignment, which, while working most of the time, is not guaranteed by the API.

Related discussion: https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/align_to.20and.20other.20APIs.20with.20surprising.20const.20behavior

Motivation, use-cases

When you have a slice to primitives (like integers), it is sometimes convenient to convert said slice to a smaller or larger primitive. Especially common are conversions to and from [u8].

Solution sketches

Add two new slice methods (plus their mut counterparts):

  • aligned_subslice: Get a subslice where the first element is aligned to a given alignment and the size of the slice is a multiple of the alignment.
  • transmute_elements:
    • Transmute the slice elements to another type.
    • If the target element type is smaller than the source element type, the returned slice will have multiple elements per element of the original slice.
    • Cannot be used to go to an element type with higher alignment requirements. Use aligned_subslice for that instead.

Alternatives:

We can also remove the fuzziness of the align_to method, but that would mean it could never be used in const eval.

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.

@oli-obk oli-obk added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jan 17, 2023
@the8472
Copy link
Member

the8472 commented Jan 17, 2023

Under which circumstances does code just ignore the head and tail? If it's about getting overaligned memory, wouldn't it be better to put it in an allocation API?

@oli-obk
Copy link
Author

oli-obk commented Jan 17, 2023

@the8472
Copy link
Member

the8472 commented Jan 17, 2023

All those cases look like they go to the same or smaller alignment, so transmute_elements would work for that purpose.

aligned_subslice is about going to a higher alignment, I was asking about that case. Does anyone do that on memory they don't own? If not that maybe an allocation API would be better.
Or an to_aligned::<T>() that asserts that there's no head and tail because one is slicing correctly into a prealigned buffer at the right offset and length.

@ChayimFriedman2
Copy link

Maybe we can guarantee that aligned_to() will always return the biggest possible middle slice, but only not during const eval?

@oli-obk
Copy link
Author

oli-obk commented Feb 16, 2024

Has seen no activity in 1 year, let's take it off the T-libs pile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

3 participants