Crate repc[][src]

This crate contains APIs that allow you to calculate the layout of C types.

Example

Consider the C type

struct __attribute__((packed)) X {
    char c;
    int i:2 __attribute__((aligned(16)));
};

You can compute the layout of this type as follows:

let ty = Type::<()> {
    layout: (),
    annotations: vec![Annotation::AttrPacked],
    variant: TypeVariant::Record(Record {
        kind: RecordKind::Struct,
        fields: vec![
            RecordField {
                layout: None,
                annotations: vec![],
                named: true,
                bit_width: None,
                ty: Type {
                    layout: (),
                    annotations: vec![],
                    variant: TypeVariant::Builtin(BuiltinType::Char),
                },
            },
            RecordField {
                layout: None,
                annotations: vec![Annotation::Align(Some(128))],
                named: true,
                bit_width: Some(2),
                ty: Type {
                    layout: (),
                    annotations: vec![],
                    variant: TypeVariant::Builtin(BuiltinType::Int),
                },
            },
        ]
    }),
};
let layout = compute_layout(Target::X86_64UnknownLinuxGnu, &ty).unwrap();
assert_eq!(layout.layout, TypeLayout {
    size_bits: 256,
    field_alignment_bits: 128,
    pointer_alignment_bits: 128,
    required_alignment_bits: 8,
});
let fields = match &layout.variant {
    TypeVariant::Record(r) => &r.fields,
    _ => unreachable!(),
};
assert_eq!(fields[0].layout.unwrap(), FieldLayout {
    offset_bits: 0,
    size_bits: 8,
});
assert_eq!(fields[1].layout.unwrap(), FieldLayout {
    offset_bits: 128,
    size_bits: 2,
});
println!("{:#?}", layout);

Modules

layout

Types describing the structure and layout of C types.

visitor

Types and functions allowing you to traverse a Type.

Structs

Error

An error produced by this crate.

Enums

ErrorType

The type of an error produced by this crate.

Target

The target of a C compiler.

Constants

HOST_TARGET

The target that this crate was compiled for.

TARGETS

A slice of all targets.

Functions

compute_layout

Computes the layout of a type.