Skip to content

Commit 09a7867

Browse files
Merge pull request #992 from nicholasbishop/bishop-api-guide
uefi-raw: Add API guidelines
2 parents 3fc2b15 + 6a88082 commit 09a7867

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

uefi-raw/api_guidelines.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# API Guidelines
2+
3+
The `uefi-raw` crate should closely match the definitions in the [UEFI
4+
Specification], with only some light changes to make it more friendly for use in
5+
Rust (e.g. casing follows Rust's conventions and modules are used to provide
6+
some hierarchy).
7+
8+
This document describes the API rules in detail. Some of these rules can be
9+
checked with `cargo xtask check-raw`, and that check is run automatically in CI
10+
as well. Other rules require human verification.
11+
12+
If you are contributing to this crate and run into any problems, such as a case
13+
that isn't covered by the rules, or a case where following the rules seems like
14+
it will lead to a bad API, don't hesitate to let us know (e.g. by filing an
15+
[issue]).
16+
17+
## Naming
18+
19+
Type names should match the corresponding spec names, but drop the `EFI_` prefix
20+
and change to `UpperCamelCase` to match Rust's convention. For example,
21+
`EFI_LOAD_FILE_PROTOCOL` becomes `LoadFileProtocol`.
22+
23+
Struct field names and function parameter names should match the corresponding
24+
spec names, but change the case to `snake_case` to match Rust's convention.
25+
26+
When defining a type that isn't part of the spec (for example, a `bitflags!`
27+
type that represents a collection of constants in the spec), prefix the name
28+
with a closely-associated type that is defined in the spec. For example, mode
29+
constants for a `FooBarProtocol` could be collected into a `FooBarMode` type.
30+
31+
It's OK to introduce minor naming changes from the specification where it
32+
improves clarity.
33+
34+
## Layout
35+
36+
All types must be `repr(C)`, `repr(C, packed)`, or `repr(transparent)`.
37+
38+
Types created with the `bitflags!` macro must set `repr(transparent)`.
39+
40+
### Dynamically Sized Types
41+
42+
Some types in the spec end with a variable-length array. It's possible to
43+
represent these as [Dynamically Sized Types], but that should be left to
44+
higher-level APIs. In this crate, add a zero-length array at the end of the
45+
struct to represent the field. For example, if a struct in the spec ends with
46+
`CHAR16 Name[];`, represent that in Rust with `name: [Char16; 0]`.
47+
48+
This pattern of using a `&Header` to work with dynamically-sized data is
49+
rejected by the Stacked Borrows model, but allowed by Tree Borrows. See [UCG
50+
issue 256] for more info.
51+
52+
## Visibility
53+
54+
Everything must have `pub` visibility.
55+
56+
## Constants
57+
58+
Use associated constants where possible instead of top-level constants.
59+
60+
Protocols must have an associated `GUID` constant, for example:
61+
62+
```rust
63+
impl RngProtocol {
64+
pub const GUID: Guid = guid!("3152bca5-eade-433d-862e-c01cdc291f44");
65+
}
66+
```
67+
68+
## Pointers
69+
70+
Use pointers (`*const`/`*mut`) instead of references (`&`/`&mut`).
71+
72+
Function pointers must be `unsafe` and have an explicit ABI (almost always
73+
`efiapi`). If a function pointer field can be null it must be wrapped in
74+
`Option`. Most function pointer fields do not need to allow null pointers
75+
though, unless the spec says otherwise.
76+
77+
### Mutability
78+
79+
Pointer mutability (`*mut` vs `*const`) is not a UB concern the way reference
80+
mutability is. In general, it is not UB to `cast_mut` a const pointer and write
81+
through it. So picking `*mut` vs `*const` is more about semantics.
82+
83+
Pointer fields in structs should always be `*mut`. Even if the pointer should
84+
not be used for mutation by bootloaders and OSes, these types are intended to be
85+
useful for UEFI _implementations_ as well, which may need to mutate data.
86+
87+
In function parameters, pick between `*const` and `*mut` based on how the
88+
parameter is described in the spec. An `OUT` or `IN OUT` pointer must be
89+
`*mut`. An `IN` pointer _may_ be `*mut`, but `*const` may be more appropriate if
90+
the parameter is described as being source data.
91+
92+
## Allowed top-level items
93+
94+
The allowed top-level items are `const`, `impl`, `macro`, `struct`, and
95+
`type`.
96+
97+
Rust `enum`s are not allowed; use the `bitflags!` or `newtype_enum!` macros
98+
instead.
99+
100+
[UEFI Specification]: https://uefi.org/specifications
101+
[issue]: https://github.com/rust-osdev/uefi-rs/issues/new
102+
[Dynamically Sized Types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
103+
[UCG issue 256]: https://github.com/rust-lang/unsafe-code-guidelines/issues/256

0 commit comments

Comments
 (0)