-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Document required components with a marker trait #18169
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
Document required components with a marker trait #18169
Conversation
@@ -202,6 +202,40 @@ pub fn derive_component(input: TokenStream) -> TokenStream { | |||
let struct_name = &ast.ident; | |||
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl(); | |||
|
|||
let required_component_impls = attrs.requires.as_ref().map(|r| { |
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.
Can you add #[cfg(doc)] to the generated impl/trait?
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.
Sadly not, I tried but cargo doc
doesn't propagate doc
to dependencies or macros in a way that worked reliably.
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.
Yep, that's because of rust-lang/cargo#8811. For docs.rs and dev-doc.bevyengine.org you could set the flags in Cargo.toml
/docs.yaml
.
I'm not exactly a new user, but my knee-jerk reaction to seeing |
Agreed. I'm going to mark the trait as |
Saw this just after making my own solution: #18171 :P |
Discourages users from implementing it themselves, since it is purely for documentation purposes. Co-Authored-By: JoshValjosh <[email protected]>
8376275
to
0cd1cda
Compare
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.
I'm on board with this plan: the backlinks are incredibly useful. That said, this trait needs to be way more explicit about a) it just being a marker for docs and b) the fact that users should effectively never ever implement this because it's automatically generated.
I think that we should do that and remove the unsafe
: there's no actual memory safety hazards here.
Co-Authored-By: Alice Cecile <[email protected]>
I've moved the trait into a |
Co-Authored-By: Alice Cecile <[email protected]>
@@ -455,6 +455,24 @@ pub trait Component: Send + Sync + 'static { | |||
fn visit_entities_mut(_this: &mut Self, _f: impl FnMut(&mut Entity)) {} | |||
} | |||
|
|||
// doc(hidden) module makes it clear that usage of this trait outside of `bevy_ecs` is unsupported. | |||
#[doc(hidden)] |
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.
For me, this removes all Require listings from the docs, which defeats the purpose of this feature :)
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.
Ah whoops, should be tested first! I assumed keeping the trait public would keep it in the documentation.
quote! { | ||
/// If not already present, the required component will be inserted using | ||
#[doc = #insertion_info] | ||
impl #impl_generics #bevy_ecs_path::component::document_required_components::Require<#path> for #struct_name #type_generics #where_clause {} |
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.
I'm not convinced that this is meaningfully "better" than the current "required list in Component docs":
- Because traits are listed alphabetically, users will hit the
Component
trait before they hit theRequire<X>
traits. - The require list in
Component
is significantly less "noisy". You can consume the entire list at a glance, rather than needing to parse out multiple lines of docs for each required component (and needing to look "around" generics / formatting / etc) - The Require trait impl is confusing / introduces a weirdness factor (even if we manage to hide it). This is non-standard trait usage (traits are generally about "rust behaviors"). People reading the docs need to deal with the fact that
- This introduces additional codegen, which is something we should be wary of, given that we are already "heavy" on the codegen side.
From my perspective the only real win here is that the require list shows up in the sidebar, but that is also a downside, as a long list takes up significant space and obscures other "real" trait impls. And it doesn't necessarily meaningfully improve the prominence of the require list. The sidebar is often littered with other things (ex: I have to scroll to see the list for Node).
Edit: The backlinks are kind of nice, but the fact that A->B
and C->A
requires are interleaved together in the list is super confusing to me.
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.
Additionally, our docs are just generally already very "noisy" and this adds even more to the pile.
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.
I think these are valid concerns, and I don't think this technique can be amended to satisfy them. Thanks for providing the feedback! I'll close this out
Closed based on feedback as not being the desired path forward. Thanks for the reviews and feedback! Good to explore and experiment even if it doesn't get merged. |
Objective
Solution
Require<C: Component>: Component
, which is automatically implemented in theComponent
derive macro.Testing
Notes
cargo doc --open
with this PR and inspecting theButton
andNode
components as examples for the effects of this PR.I have based this PR off of Fix Component require() IDE integration #18165 and will rebase once it is merged.Examples
Button
required components are listed logically in Trait ImplementationsThe method used to insert a required component can also be documented

Node
can document components it requires and components that require itComparison to #18171 (Rustdoc Extension)
Cons
where
bounds, etc.Pros
Button
andNode
both get documentation forButton
requiringNode
)