-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: Add documentation style guidelines #141
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
Documentation Style | ||
=================== | ||
|
||
In this project, code documentation is generated using Rustdoc, which | ||
automatically generates interactive web documentation. Here are some | ||
guidelines for documenting code effectively: | ||
|
||
- Follow [Rust's official indications.](https://doc.rust-lang.org/rustdoc/how-to-write-documentation.html) | ||
|
||
- Follow standard Markdown format, e.g. variables between backticks: | ||
|
||
- When adding doc comments to your code, use triple slashes (`///`) | ||
to document items; if you also want to document modules or crates, use | ||
`//!` and `#[doc = ""]` for documenting fields or expressions. | ||
|
||
```rust | ||
/// This function does A, takes parameter of type [`M`]. | ||
/// It returns [`B`], keep in mind C | ||
fn main(a: M) -> B { | ||
// Some code here | ||
} | ||
``` | ||
|
||
- Documenting trait implementations is optional since the generated | ||
Rust core library already documents them. The exception would be if your | ||
implementation does something counterintuitive to the trait's general | ||
definition. | ||
|
||
- When mentioning a type (e.g. \`RWLock\`, \`WriteLockGuard\`) it's good to | ||
add a link to the type with square brackets (e.g. [\`RWLock\`], | ||
[\`WriteLockGuard\`]). | ||
|
||
- When documenting a function, examples of usage relying on code blocks | ||
can help understand how to use your code. However, keep in mind that | ||
said code will be built and ran during tests, so it also needs to be | ||
maintained -- keep it simple. Here is an example of function | ||
documentation with Arguments, Returns and Examples: | ||
|
||
```rust | ||
|
||
/// Compares two [`Elf64AddrRange`] instances for partial ordering. It returns | ||
/// [`Some<Ordering>`] if there is a partial order, and [`None`] if there is no | ||
/// order (i.e., if the ranges overlap without being equal). | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `other` - The other [`Elf64AddrRange`] to compare to. | ||
/// | ||
/// # Returns | ||
/// | ||
/// - [`Some<Ordering::Less>`] if [`self`] is less than `other`. | ||
/// - [`Some<Ordering::Greater>`] if [`self`] is greater than `other`. | ||
/// - [`Some<Ordering::Equal>`] if [`self`] is equal to `other`. | ||
/// - [`None`] if there is no partial order (i.e., ranges overlap but are not equal). | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// use svsm::elf::Elf64AddrRange; | ||
/// use core::cmp::Ordering; | ||
/// | ||
/// let range1 = Elf64AddrRange { vaddr_begin: 0x1000, vaddr_end: 0x1100 }; | ||
/// let range2 = Elf64AddrRange { vaddr_begin: 0x1100, vaddr_end: 0x1200 }; | ||
/// | ||
/// assert_eq!(range1.partial_cmp(&range2), Some(Ordering::Less)); | ||
/// ``` | ||
impl cmp::PartialOrd for Elf64AddrRange { | ||
fn partial_cmp(&self, other: &Elf64AddrRange) -> Option<cmp::Ordering> { | ||
//(...) | ||
``` | ||
|
||
- Add section "Safety" if necessary to clarify what is unsafe, specially in | ||
public (`pub`) interfaces, when using `unsafe` blocks or in cases where | ||
undefined behavior may arise. For example: | ||
|
||
```rust | ||
/// # Safety | ||
/// | ||
/// This function is marked as `unsafe` because it uses unsafe assembly. | ||
/// It is the responsibility of the caller to ensure the following: | ||
/// | ||
/// 1. `src` and `dst` must point to valid memory. | ||
/// 2. The length `len` must accurately represent the number of bytes in | ||
/// `data`. | ||
/// 3. `src` must be correctly initialized. | ||
/// | ||
pub unsafe fn example_memcpy<T>(dest: *mut T, src: *const T, len: usize) { | ||
// Ensure the pointers are not null | ||
assert!(!dest.is_null() && !src.is_null()); | ||
let mut rcx: usize; | ||
|
||
unsafe { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function is already unsafe, so there is no need for this unsafe block as far as I know. |
||
asm!( | ||
"rep movsb" | ||
: "={rcx}"(rcx) | ||
: "0"(len), "D"(dest), "S"(src) | ||
: "memory" | ||
); | ||
} | ||
} | ||
``` | ||
- We can't have a section "Panic" for every place the SVSM may panic, but | ||
they should be included if your code checks assertions or uses the | ||
`unwrap()` method. For instance: | ||
|
||
```rust | ||
/// # Panics | ||
/// | ||
/// The function will panic if the provided length exceeds the buffer's capacity. | ||
/// | ||
pub fn my_function(buffer: &mut Vec<u8>, len: usize) { | ||
if len > buffer.capacity() { | ||
panic!("Length exceeds allocated capacity!"); | ||
} | ||
``` | ||
|
||
- Remember that if you update code, you also have to update its related | ||
documentation to ensure maintainability. | ||
|
||
- Be aware that your documentation comments have the potential to break the | ||
documentation generation process (cargo doc), which can delay the merging | ||
of your changes. Your new documentation should be warning-free. | ||
|
||
In general, even imperfect documentation is better than none at all. | ||
Prioritize documenting functions that are publicly exported, especially | ||
API calls, over internal helper functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function should have a
# Panics
section in this case. We can even merge this example with the one below.