roxygen - documenting function parameters
The #[roxygen] attribute allows you to add doc-comments to function
parameters, which is a compile error in current Rust. You can now write
use *;
/// sum the rows of an image
This will produce documentation as if you had written the 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 `nrows * ncols` elements
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 *;
/// long documention
/// ...
/// # Examples
/// ...
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.
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, but I haven't measured it... so take it with a grain of sodium-chloride.