Expose alignment info in `KnownLayout`
- Dominant language
- Rust
- Stars
- 2.6k
- Forks
- 179
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 29
Description
# **What features would you like from zerocopy?**
## Expose Alignment Info
I'm working on a crate that uses `zerocopy` to manage writing to/reading from Vulkan buffers, many of which use slice DSTs as their backing types. Since these require manual allocation separate from the normal Rust APIs, I need to know the required alignment of the buffer's type before constructing it (meaning I can't use `mem::align_of_val`). `zerocopy` keeps track of this internally in the form of `DstLayout { align }`, but doesn't expose it anywhere. As-is, I have a dirty hack that over-estimates the alignment of a type by examining how its size changes with respect to element count.
It would be great if this info was exposed somewhere, perhaps through a trait method on `KnownLayout`. It doesn't really matter to me exactly how, as long as it's not `#[doc(hidden)]`. It doesn't seem to me that this would leak anything that might see a breaking change down the line, either, unless Rust eventually removes the `Sized` constraint from `mem::align_of`.
```rust
pub trait KnownLayout {
...
fn align() -> NonZeroUsize {
Self::LAYOUT.align
}
}
```
## (If Possible) Expose `PointerMetadata::from_element_count`/`to_element_count`
While I could hack around this with an extra trait, it would be nice these were exposed (i.e., not `#[doc(hidden)]`), or some equivalent.
In my use case, I have a field `elements: usize` of a non-generic descriptor struct for setting
up buffers, that's just ignored for types with `type PointerMetadata = ()`. It's nice to not have to expose a generic interface there. This is super not critical though, and doesn't have any actual safety implications, so feel ignore this part if it's not desired 🙂.
Contributor guide
Assessment
This issue has not been assessed yet.