Skip to content

Document visibility annotation #2321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [Preventing the Derivation of `Debug`](./nodebug.md)
- [Preventing the Derivation of `Default`](./nodefault.md)
- [Annotating types with `#[must-use]`](./must-use-types.md)
- [Field visibility](./visibility.md)
- [Generating Bindings to C++](./cpp.md)
- [Generating Bindings to Objective-c](./objc.md)
- [Using Unions](./using-unions.md)
Expand Down
37 changes: 37 additions & 0 deletions book/src/visibility.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Making fields private

Fields can be made private for various reasons. You may wish to enforce some invariant on the fields of a structure, which cannot be achieved if the field is public and can be set by any code. For example, you may wish to ensure that a pointer always points to something appropriate.

### Annotation

```c
struct OneFieldPrivate {
/** Null-terminated, static string. <div rustbindgen private> */
const char *s;
bool b;
};

/** <div rustbindgen private> */
struct MostFieldsPrivate {
int a;
bool b;
/** <div rustbindgen private="false"></div> */
char c;
};
```

Then in Rust:

```rust
# #[repr(C)]
# pub struct OneFieldPrivate {
# s: *const ::std::os::raw::c_char,
# pub b: bool,
# }

impl OneFieldPrivate {
pub fn new(s: &'static std::ffi::CStr, b: bool) -> Self {
OneFieldPrivate { s: s.as_ptr(), b }
}
}
```