annotation Packed

Overview

Marks a C struct or union, or an Extern struct as packed.

The alignment of a packed struct is one byte and there is no padding between fields.

lib Foo
  @[Packed]
  struct Bar
    x : Int8
    y : Int32
  end
end

sizeof(Foo::Bar) # => 5 (an unpacked struct would be 8)

@[Extern]
@[Packed]
struct Bar
  def initialize(@x : Int8, @y : Int32)
  end
end

sizeof(Bar) # => 5 (an unpacked struct would be 8)

This is typically only needed when interfacing with a C library that explicitly uses packed structs.

NOTE The Crystal book has a section about Built-in Annotations: Packed.

Defined in:

annotations.cr