• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Making fields private
2
3Fields 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
8struct OneFieldPrivate {
9    /** Null-terminated, static string. <div rustbindgen private> */
10    const char *s;
11    bool b;
12};
13
14/** <div rustbindgen private> */
15struct MostFieldsPrivate {
16    int a;
17    bool b;
18    /** <div rustbindgen private="false"></div> */
19    char c;
20};
21```
22
23Then 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
32impl OneFieldPrivate {
33    pub fn new(s: &'static std::ffi::CStr, b: bool) -> Self {
34        OneFieldPrivate { s: s.as_ptr(), b }
35    }
36}
37```
38