Skip to content

Add rules for libc structs#257

Merged
nunoplopes merged 8 commits into
Cpp2Rust:masterfrom
lucic71:libc-structs
Jul 15, 2026
Merged

Add rules for libc structs#257
nunoplopes merged 8 commits into
Cpp2Rust:masterfrom
lucic71:libc-structs

Conversation

@lucic71

@lucic71 lucic71 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

This adds rules for both unsafe and recount libc structs. For unsafe the rule is trivial:

fn t1() -> libc::sockaddr {
    unsafe { std::mem::zeroed() }
}

For refcount, I added a new dir in libcc2rs, called libc_shims. It defines refcount shim structs for each libc struct, for example:

pub struct Sockaddr {
    pub sa_family: Value<u16>,
    pub sa_data: Value<Box<[u8]>>,
}

The shims exist for 2 reasons:

  • generated code expects each field to be boxed
  • libc fields can contain raw pointers, which are not permitted in refcount

The refcount rule for sockaddr becomes:

fn t1() -> libcc2rs::Sockaddr {
    Default::default()
}

@lucic71 lucic71 marked this pull request as draft July 15, 2026 08:37
@lucic71 lucic71 marked this pull request as ready for review July 15, 2026 09:36
@nunoplopes nunoplopes merged commit 571f697 into Cpp2Rust:master Jul 15, 2026
9 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.

2 participants