diff options
author | Christian Poveda Ruiz <31802960+pvdrz@users.noreply.github.com> | 2022-10-24 10:04:26 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-24 10:04:26 -0500 |
commit | 170b79c88ea5d527184eccc2903ae4ac52272800 (patch) | |
tree | 6998aa846270c76e34793695db76ee6268d912eb | |
parent | 3994a3315e268ee509f7d0e74b3818540cc97d3c (diff) | |
parent | fc56c70500d8a6845ab2a7b886425905818a56b5 (diff) |
Merge pull request #2321 from GKFX/private_fields
Document visibility annotation
-rw-r--r-- | book/src/SUMMARY.md | 1 | ||||
-rw-r--r-- | book/src/visibility.md | 37 |
2 files changed, 38 insertions, 0 deletions
diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index f9cc869f..e7feff95 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -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) diff --git a/book/src/visibility.md b/book/src/visibility.md new file mode 100644 index 00000000..ac0aac15 --- /dev/null +++ b/book/src/visibility.md @@ -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 } + } +} +``` |