Skip to main content

compute_struct_layout

Function compute_struct_layout 

Source
pub fn compute_struct_layout(
    name: &str,
    fields: &[(String, FieldType)],
) -> StructLayout
Expand description

Compute the repr(C) struct layout for a named type with the given fields.

The layout follows C struct rules:

  1. An 8-byte v2 heap header occupies bytes [0, 8).
  2. Fields are placed sequentially after the header, each aligned to its natural alignment (inserting padding bytes as needed).
  3. The total size is padded to the struct’s overall alignment (the maximum alignment of the header and all fields).

§Examples

use shape_value::v2_struct_layout::{compute_struct_layout, FieldType};

let layout = compute_struct_layout("Point", &[
    ("x".into(), FieldType::F64),
    ("y".into(), FieldType::F64),
]);
assert_eq!(layout.fields[0].offset, 8);  // after 8-byte header
assert_eq!(layout.fields[1].offset, 16);
assert_eq!(layout.total_size, 24);