Skip to content

Add usize and isize pointer-width integer types - #4

Merged
Gulliby merged 1 commit into
mainfrom
feature/pointer-width-ints
Oct 21, 2026
Merged

Gulliby merged 1 commit into
mainfrom
feature/pointer-width-ints

Conversation

@Gulliby

@Gulliby Gulliby commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Requested by gulliby

Before

The struct description language accepted ptr for pointer-sized fields, but there was no integer type that follows the platform pointer width, so lengths and offsets such as size_t or Rust's usize had to be written as ptr or as a fixed-width integer.

After

usize and isize are accepted wherever ptr is, including array elements and nested structs. Both have exactly the size and alignment of ptr: 8 and 8 under lp64, 4 and 4 under ilp32. Other names such as size_t are still unknown types. For this struct:

struct Buf {
    u8 tag;
    usize len;
    isize off;
    u16 flags;
}

structpad layout --abi ilp32 ffi.sp prints:

struct Buf: size 16, align 4, padding 5
offset   size  field
     0      1  tag: u8
     1      3  (padding)
     4      4  len: usize
     8      4  off: isize
    12      2  flags: u16
    14      2  (padding)

How

  • src/structpad/types.py: two rows added to PRIMITIVE_TYPES right after ptr, with the same numbers as ptr. The parser, layout, suggestions and renderers already read this table, so nothing else changes.
  • tests/test_pointer_ints.py: five new tests (primitive sizes, layout under both ABIs, suggest and check, JSON output and the unknown size_t). The existing 145 tests are untouched; the suite now has 150.
  • README.md: a short paragraph and an example in the Input language section.

usize and isize take the size and alignment of ptr: 8 and 8 under lp64,
4 and 4 under ilp32. Document them in the README and add tests.
@Gulliby Gulliby self-assigned this Oct 21, 2026
@Gulliby
Gulliby merged commit 1e90957 into main Oct 21, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant