Skip to content

Commit 170b79c

Browse files
authored
Merge pull request #2321 from GKFX/private_fields
Document visibility annotation
2 parents 3994a33 + fc56c70 commit 170b79c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [Preventing the Derivation of `Debug`](./nodebug.md)
2121
- [Preventing the Derivation of `Default`](./nodefault.md)
2222
- [Annotating types with `#[must-use]`](./must-use-types.md)
23+
- [Field visibility](./visibility.md)
2324
- [Generating Bindings to C++](./cpp.md)
2425
- [Generating Bindings to Objective-c](./objc.md)
2526
- [Using Unions](./using-unions.md)

book/src/visibility.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Making fields private
2+
3+
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.
4+
5+
### Annotation
6+
7+
```c
8+
struct OneFieldPrivate {
9+
/** Null-terminated, static string. <div rustbindgen private> */
10+
const char *s;
11+
bool b;
12+
};
13+
14+
/** <div rustbindgen private> */
15+
struct MostFieldsPrivate {
16+
int a;
17+
bool b;
18+
/** <div rustbindgen private="false"></div> */
19+
char c;
20+
};
21+
```
22+
23+
Then in Rust:
24+
25+
```rust
26+
# #[repr(C)]
27+
# pub struct OneFieldPrivate {
28+
# s: *const ::std::os::raw::c_char,
29+
# pub b: bool,
30+
# }
31+
32+
impl OneFieldPrivate {
33+
pub fn new(s: &'static std::ffi::CStr, b: bool) -> Self {
34+
OneFieldPrivate { s: s.as_ptr(), b }
35+
}
36+
}
37+
```

0 commit comments

Comments
 (0)