Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#69362 - CAD97:alloc_layout_extras, r=Amanieu
Stabilize most common subset of alloc_layout_extras Tracking issue: rust-lang#55724 Specifically, this stabilizes: ```rust pub fn Layout::align_to(&self, align: usize) -> Result<Layout, LayoutErr>; pub fn Layout::pad_to_align(&self) -> Layout; pub fn Layout::extend(&self, next: Layout) -> Result<(Layout, usize), LayoutErr>; pub fn Layout::array<T>(n: usize) -> Result<Layout, LayoutErr>; ``` Methods that are tracked by rust-lang#55724 but are not stabilized here: ```rust pub fn Layout::padding_needed_for(&self, align: usize) -> usize; pub fn Layout::repeat(&self, n: usize) -> Result<(Layout, usize), LayoutErr>; pub fn Layout::repeat_packed(&self, n: usize) -> Result<Layout, LayoutErr>; pub fn Layout::extend_packed(&self, next: Layout) -> Result<Layout, LayoutErr>; ``` Combined, these stabilized functions allow code to construct and manipulate `repr(C)` layouts while letting the standard library handle correctness in the face of edge cases. For example use cases, consider the usage in [hashbrown](https://github.com/Amanieu/hashbrown/blob/2f2af1d/src/raw/mod.rs#L143), [crossbeam-skiplist](https://github.com/crossbeam-rs/crossbeam-skiplist/blob/master/src/base.rs#L99), [pointer-utils/slice-dst](https://github.com/CAD97/pointer-utils/blob/92aeefeed9399f28d1b1654b63f8dcbe1242d8d4/crates/slice-dst/src/layout_polyfill.rs), and of course the standard library itself. Providing a higher-level API such as `Layout::repr_c<const N: usize>(fields: [Layout; N]) -> Result<(Layout, [usize; N]), LayoutErr>` is blocked on const generics, which are a ways off. Providing an API that doesn't provide offsets would be quite suboptimal, as the reason for calculating the layout like this rather than `Layout::new` is to get the field offsets. The primary issue with the current API is having to call `.pad_to_align()` to match the layout of a `repr(C)` struct. However, I think this is not just a (failing? limitation?) of the API, but rather intrinsic complexity. While all Rust-defined types have size==stride, and probably will for the foreseeable future, there is no inherent reason why this is a limitation of all allocations. As such, the `Layout` manipulation APIs shouldn't impose this limitation, and instead the higher level api of `repr_c` (or just plain old using `Layout::new`) can make keeping it simple. cc @matklad r? @rust-lang/libs
- Loading branch information