summaryrefslogtreecommitdiff
path: root/book/src/visibility.md
blob: ac0aac15da4c382a021b465283c2974971d4fe42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 }
    }
}
```