4 releases
new 0.2.0 | Oct 26, 2024 |
---|---|
0.1.2 | Oct 26, 2024 |
0.1.1 | Oct 24, 2024 |
0.1.0 | Oct 24, 2024 |
#684 in Rust patterns
280 downloads per month
19KB
245 lines
roxygen - documenting function parameters
The #[roxygen]
attribute allows you to add doc-comments to function
parameters, which is a compile error in current Rust. Generic lifetimes,
types, and constants of the function can also be documented.
You can now write
use roxygen::*;
#[roxygen]
/// sum the rows of an image
fn sum_image_rows(
/// the image data in row-major format
image_data: &[f32],
/// the number of rows in the image
nrows: u32,
/// the number of columns in the image
ncols: u32,
/// an out buffer into which the resulting
/// sums are placed. Must have space
/// for exactly `nrows` elements
sums: &mut [f32]) -> Result<(),String> {
todo!()
}
You have to document at least one parameter (or generic), but you don't have to document all of them. The example above will produce documentation as if you had written a doc comment for the function like so:
/// sum the rows of an image
///
/// **Arguments**:
///
/// * `image_data`: the image data in row-major format
/// * `nrows`: the number of rows in the image
/// * `ncols`: the number of columns in the image
/// * `sums`: an out buffer into which the resulting
/// sums are placed. Must have space
/// for exactly `nrows` elements
fn sum_image_rows(
image_data: &[f32],
nrows: u32,
ncols: u32,
sums: &mut [f32]) -> Result<(),String> {
todo!()
}
Placing the Arguments-Section
By default, the section documenting the arguments will go at the end of the top-level function documentation. However, this crate allows to explicitly place the section by using a custom attribute like so:
use roxygen::*;
#[roxygen]
/// long documention
/// ...
#[arguments_section]
/// # Examples
/// ...
fn foo(
/// some docs
first: i32,
second: f32
)
{}
Considerations
It's a long standing issue
whether and how to add this capability to rustdoc
. Firstly, there's no
general consensus on how exactly to document function parameters. However,
I've seen the presented style used a lot, with minor variations.
Secondly, the standard library doesn't need this
style of documentation at all. So before you stick this macro on every function,
do consider
- taking inspiration from how the standard library deals with function parameters,
- using fewer function parameters,
- using more descriptive parameters names,
- using types to communicate intent,
- sticking function parameters in a
struct
.
Here is an elegant way, how the example above can be reworked without using per parameter documentation:
/// Sums the rows of an image.
///
/// The rows of `image_data`, an `nrows` by `ncols`
/// matrix in row-major ordering, are summed into `sums`
/// which must have exactly `nrows` elements.
fn sum_image_rows(
image_data: &[f32],
nrows: u32,
ncols: u32,
sums: &mut [f32]) -> Result<(),String> {
todo!()
}
All that being said, I've realized that sometimes I still want to document function parameters.
Compile Times
Macros will always increase your compile time to some degree, but I don't think this is a giant issue here for two reasons: firstly, this macro is to be used sparingly. Secondly, this macro just does some light parsing and shuffling around of the documentation tokens. It introduces no additional code. Thus, it doesn't make your actual code more or less complex and should not affect compile times much (after this crate was compiled once), but I haven't measured it... so take it with a grain of sodium-chloride.
Dependencies
~1.5MB
~42K SLoC