rust-lang / rust-lang/nomicon

Discuss the `WrapperStruct(UnsafeCell<some_c_struct>)`FFI pattern

Open
#385 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
2.3k
Forks
325
PR merge metrics
No merged PRs in 30d

Description

Hey everyone,

I’d like to suggest adding the interior mutability FFI wrapper pattern to the FFI section, which usually looks something like this

#[repr(transparent)]
WrapperStruct(UnsafeCell<some_c_struct>)

This is used in e.g. the Linux kernel safe wrapper

I didn’t really understand it at first, but somebody explained it extremely well on Discord. Some of the explanation could likely be copied and pasted https://discord.com/channels/273534239310479360/818964227783262209/1040580112330002443

In general, I think the FFI section could use some updating, to be less of “this is how you call C functions” to more of “this is how you wrap a C API and make it safe”, since that’s what a lot of people are doing now as Rust works in to existing C projects. But that’s a big change, suitable for a separate issue.

Contributor guide

No contributing guide indexed for this repository

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.

Research direction

Start in the Nomicon's FFI section and review the linked Discord explanation for the WrapperStruct(UnsafeCell<some_c_struct>) pattern. Add a clear explanation of how this wrapper supports safe C API abstractions, while treating the broader FFI section rewrite as separate work.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.