Set alignment of a struct based on the type size

⚓ Rust    📅 2025-11-12    👤 surdeus    👁️ 5      

surdeus

Say I have a simple pair struct

pub struct Pair<First, Second> {
}

I want to be able to align it with, fn(size_of(First), size_of(Second)), where fn is min(16, bit_ceil(size_of(First) + size_of(Second))).

But repr(align()) seems to require a literal.

For more clarity, in C++, it would look something like:

struct alignas(get_alignment<First, Second>()) pair

where get_alignment is a const function

1 post - 1 participant

Read full topic

🏷️ Rust_feed