13da5c369Sopenharmony_ci# Conventions 23da5c369Sopenharmony_ci 33da5c369Sopenharmony_ciIn order to achieve our goal of wrapping [libc][libc] code in idiomatic rust 43da5c369Sopenharmony_ciconstructs with minimal performance overhead, we follow the following 53da5c369Sopenharmony_ciconventions. 63da5c369Sopenharmony_ci 73da5c369Sopenharmony_ciNote that, thus far, not all the code follows these conventions and not all 83da5c369Sopenharmony_ciconventions we try to follow have been documented here. If you find an instance 93da5c369Sopenharmony_ciof either, feel free to remedy the flaw by opening a pull request with 103da5c369Sopenharmony_ciappropriate changes or additions. 113da5c369Sopenharmony_ci 123da5c369Sopenharmony_ci## Change Log 133da5c369Sopenharmony_ci 143da5c369Sopenharmony_ciWe follow the conventions laid out in [Keep A CHANGELOG][kacl]. 153da5c369Sopenharmony_ci 163da5c369Sopenharmony_ci[kacl]: https://github.com/olivierlacan/keep-a-changelog/tree/18adb5f5be7a898d046f6a4acb93e39dcf40c4ad 173da5c369Sopenharmony_ci 183da5c369Sopenharmony_ci## libc constants, functions and structs 193da5c369Sopenharmony_ci 203da5c369Sopenharmony_ciWe do not define integer constants ourselves, but use or reexport them from the 213da5c369Sopenharmony_ci[libc crate][libc]. 223da5c369Sopenharmony_ci 233da5c369Sopenharmony_ciWe use the functions exported from [libc][libc] instead of writing our own 243da5c369Sopenharmony_ci`extern` declarations. 253da5c369Sopenharmony_ci 263da5c369Sopenharmony_ciWe use the `struct` definitions from [libc][libc] internally instead of writing 273da5c369Sopenharmony_ciour own. If we want to add methods to a libc type, we use the newtype pattern. 283da5c369Sopenharmony_ciFor example, 293da5c369Sopenharmony_ci 303da5c369Sopenharmony_ci```rust 313da5c369Sopenharmony_cipub struct SigSet(libc::sigset_t); 323da5c369Sopenharmony_ci 333da5c369Sopenharmony_ciimpl SigSet { 343da5c369Sopenharmony_ci ... 353da5c369Sopenharmony_ci} 363da5c369Sopenharmony_ci``` 373da5c369Sopenharmony_ci 383da5c369Sopenharmony_ciWhen creating newtypes, we use Rust's `CamelCase` type naming convention. 393da5c369Sopenharmony_ci 403da5c369Sopenharmony_ci## Bitflags 413da5c369Sopenharmony_ci 423da5c369Sopenharmony_ciMany C functions have flags parameters that are combined from constants using 433da5c369Sopenharmony_cibitwise operations. We represent the types of these parameters by types defined 443da5c369Sopenharmony_ciusing our `libc_bitflags!` macro, which is a convenience wrapper around the 453da5c369Sopenharmony_ci`bitflags!` macro from the [bitflags crate][bitflags] that brings in the 463da5c369Sopenharmony_ciconstant value from `libc`. 473da5c369Sopenharmony_ci 483da5c369Sopenharmony_ciWe name the type for a set of constants whose element's names start with `FOO_` 493da5c369Sopenharmony_ci`FooFlags`. 503da5c369Sopenharmony_ci 513da5c369Sopenharmony_ciFor example, 523da5c369Sopenharmony_ci 533da5c369Sopenharmony_ci```rust 543da5c369Sopenharmony_cilibc_bitflags!{ 553da5c369Sopenharmony_ci pub struct ProtFlags: libc::c_int { 563da5c369Sopenharmony_ci PROT_NONE; 573da5c369Sopenharmony_ci PROT_READ; 583da5c369Sopenharmony_ci PROT_WRITE; 593da5c369Sopenharmony_ci PROT_EXEC; 603da5c369Sopenharmony_ci #[cfg(any(target_os = "linux", target_os = "android"))] 613da5c369Sopenharmony_ci PROT_GROWSDOWN; 623da5c369Sopenharmony_ci #[cfg(any(target_os = "linux", target_os = "android"))] 633da5c369Sopenharmony_ci PROT_GROWSUP; 643da5c369Sopenharmony_ci } 653da5c369Sopenharmony_ci} 663da5c369Sopenharmony_ci``` 673da5c369Sopenharmony_ci 683da5c369Sopenharmony_ci 693da5c369Sopenharmony_ci## Enumerations 703da5c369Sopenharmony_ci 713da5c369Sopenharmony_ciWe represent sets of constants that are intended as mutually exclusive arguments 723da5c369Sopenharmony_cito parameters of functions by [enumerations][enum]. 733da5c369Sopenharmony_ci 743da5c369Sopenharmony_ci 753da5c369Sopenharmony_ci## Structures Initialized by libc Functions 763da5c369Sopenharmony_ci 773da5c369Sopenharmony_ciWhenever we need to use a [libc][libc] function to properly initialize a 783da5c369Sopenharmony_civariable and said function allows us to use uninitialized memory, we use 793da5c369Sopenharmony_ci[`std::mem::MaybeUninit`][std_MaybeUninit] when defining the variable. This 803da5c369Sopenharmony_ciallows us to avoid the overhead incurred by zeroing or otherwise initializing 813da5c369Sopenharmony_cithe variable. 823da5c369Sopenharmony_ci 833da5c369Sopenharmony_ci[bitflags]: https://crates.io/crates/bitflags/ 843da5c369Sopenharmony_ci[enum]: https://doc.rust-lang.org/reference.html#enumerations 853da5c369Sopenharmony_ci[libc]: https://crates.io/crates/libc/ 863da5c369Sopenharmony_ci[std_MaybeUninit]: https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html 87