#![allow(dead_code, deprecated, unused_variables, unused_mut)] use std::alloc::{Layout, LayoutError}; pub fn repr_c(fields: &[Layout]) -> Result<(Layout, Vec), LayoutError> { let mut offsets = Vec::new(); let mut layout = Layout::from_size_align(0, 1)?; for &field in fields { let (new_layout, offset) = layout.extend(field)?; layout = new_layout; offsets.push(offset); } Ok((layout.pad_to_align(), offsets)) } #[repr(C)] struct S { a: u64, b: u32, c: u16, d: u32, } fn main() { let s = Layout::new::(); let u16 = Layout::new::(); let u32 = Layout::new::(); let u64 = Layout::new::(); assert_eq!(repr_c(& [u64, u32, u16, u32]), Ok((s, vec![0, 8, 12, 16]))); }