Figure out what to do with flexible array members

Open
#4,758 4 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Refactor
Clarity
Needs clarification
Activity status
Quiet
Tech stack
c, rust

Research direction

Start by locating the existing Rust definitions that map C flexible array members to public [T; 0] fields, then compare that pattern with unsized [c_char] fields and the listed size_of, offset_of, and reference-use cases. Done means reaching and documenting a decision for the 1.0 representation, with its ABI and usability tradeoffs justified.

Written by the indexing model from the issue text.

Description

C-discussion

A number of C structures make use of a flexible array member:

struct sockaddr {
     sa_family_t     sa_family;      /* Address family */
     char            sa_data[];      /* Socket address */
 };

When mapped to Rust, most of these are a public field of type [T; 0]. We could instead map them to Rust unsized [c_char] fields.

The current pattern has a few advantages over using DSTs:

  • size_of works to get you the base size
  • You can offset_of the field to get the start of the array if you need it
  • You can work with the type through references, they don't need to be constructed as fat pointers
  • Rust's flexible array members aren't all that convenient to work with

I expect we'll probably want to stick with the current pattern, but it's worth double checking that there isn't something better for 1.0.

Dominant language
Rust
Stars
2.6k
Forks
1.3k
Avg merge
2d 13h
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-lang/libc

All issues in rust-lang/libc

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.