doc_for
[WARNING] This crate is still in development, and the API is subject to BREAKING CHANGES.
📖 Get the documentation comment for structs, enums and unions.
🪄 Features
- Zero-cost: All work is done at compile-time
- Simple: Just derive the
DocFortrait and use thedoc_for!macro
🤔 Usage
Get the documentation comment for a type
First, bring DocFor and doc_for! into scope:
use ;
Then, derive the DocFor trait for your struct:
# use ;
#
/// Some documentation
Finally, use the doc_for! macro to get the documentation comment, which returns an Option<&'static str>:
# use ;
#
# /// Some documentation
#
#
assert_eq!;
Note that the leading spaces are preserved. Multi-line comments are also supported:
# use ;
#
/// Some documentation
/// that spans multiple lines
///
/// Additional information
assert_eq!;
If the type does not have a documentation comment, doc_for! will return None:
# use ;
#
// No documentation comment here
assert!;
Also works with tuple structs, enums and unions:
# use ;
#
/// Tuple struct documentation
;
assert_eq!;
/// Enum documentation
assert_eq!;
/// Union documentation
union MyUnion
assert_eq!;
Get the documentation comment for fields and variants
Same as before, bring DocFor and doc_for! into scope and derive the DocFor trait for your struct:
use ;
Finally, use the doc_for! macro to get the documentation comment. If the field does not have a documentation comment, doc_for! will return None:
# use ;
#
#
#
assert_eq!;
assert!;
// Won't compile because the field does not exist
// assert_eq!(doc_for!(MyStruct, non_existent), None);
If the field does not exist, doc_for! will panic, thus failing the compilation:
# use ;
#
#
#
// Won't compile due to `Field does not exist`
assert_eq!;
Similarly, it also works with union fields (not listed here) and enum variants:
# use ;
#
assert_eq!;
assert!;
// Won't compile because the variant does not exist
// assert_eq!(doc_for!(MyEnum, NonExistent), None);
⚙️ Implementation
The doc_for crate provides a DocFor trait and a doc_for! macro:
- The
DocFortrait requires an associated constantDOCto be implemented for the type - Deriving the
DocFortrait sets theDOCconstant as the documentation comment of the type, and generates aconst fn doc_for_field(name: &'static str) -> Option<&'static str>function - If given a type, the
doc_for!macro retrieves the value of this constant; If given a type and a field name, thedoc_for!macro calls thedoc_for_fieldfunction with the given field name
Using these APIs is zero-cost, as all the work is done at compile-time:
- When compiled, types that derive
DocForwill have their documentation comments inlined as associated constants or in constant functions - Calls to
doc_for!will be replaced with the value of the associated constant or the result of the constant function
✅ TODO
- Strip each line of the documentation comment, via a
stripattribute - Access module documentation (e.g.
doc_for!(my_module)) - Access trait documentation (e.g.
doc_for!(MyTrait)) - Access sub-item documentation
- Access field documentation (e.g.
doc_for!(MyStruct, field)ordoc_for!(MyUnion, field)) - Access enum variant documentation (e.g.
doc_for!(MyEnum, Variant)) - Access enum variant instance documentation (e.g.
doc_for!(my_enum_variant)) - Access method documentation (e.g.
doc_for!(MyStruct, method)) - Access associated constant documentation (e.g.
doc_for!(MyStruct, CONSTANT)) - Access associated type documentation (e.g.
doc_for!(MyStruct, Type))
- Access field documentation (e.g.