docpos - compact-documenting items after defining them: functions, structs, enums…
(a fork of the "passively-maintained" roxygen with a few more supported items and allowing for a pos-position)
The #[docpos] attribute allows you to add doc-comments after an item, not before (as in regular Rust) in enums and structs, and also document function parameters (a compile error in current Rust). Generic lifetimes, types, and constants of the function can also be documented.
You can now write a much more readable and compact table:
use PathBuf;
use *;
instead of
use PathBuf;
/// "Outer" scruct docs wasting a line
Macro can be used with an explicit argument #[docpos(struct)] (enum,fn) or let the macro try each supported type via #[docpos], though the latter will generate errors for each type.
Similarly, for a function, you can add doc-comments to parameters:
use *;
(Or use the main roxygen's crate macro roxygen for reguler pre-doc-comment support)
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
///
/// **Parameters**:
///
/// * `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
Enums
// or #[docpos] for auto-detection
Replaces
/// enumPos line1
/// enumPos line2
Though here the expansion results is exactly this, without a separate section like for function parameters, since even when all docs are removed by the macro for manual formatting, the default Variants section remains (use #[docpos(enum_sect)] to force an explicit section)
⚠️ Renaming the macros exported from this crate (use ... as ...) or renaming the
crate itself (in your Cargo.toml) will make all of this stop working properly.
Placing the Parameters-Section
By default, the section documenting the parameters 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;
/// 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.
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.
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 (after the docpos dependency itself was compiled, that is): 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.
Known issues
- the default doc sections (e.g.,
Variantsfor enums) persists even when it has no docs, so structs and enums don't add their own section unless explicitly called withenum_sect,struct_sect - unrealized potential to waste less space in the output: short comments could've been "inlined" to the function signature as regular comments instead of having the whole new section
- rather limited support: functions (parameters and generics), structs (fields), enums (variants)
- invalid enum variant's visibility qualifiers
enum Vis {pub V1}are not rejected due to syn design decision