Skip to content
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

Require #[derive(Component)] for component types #27

Merged
merged 4 commits into from
Oct 1, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions rfcs/27-derive-component.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Feature Name: `derive_component`

## Summary

Remove blanket impl from `Component` trait, and require it to be manually implemented or derived using `#[derive(Component)]`.
Extend the `Component` trait to declare more information statically, instead of requiring runtime registration.

## Motivation

The `Component` trait is being used inside Bundles and Queries. It's usage in Queries is enforced by the type system.
Unfortunately, because it's automatically derived, almost every type can be treated as a component, including Bundles.
That means `Component` APIs don't really have any way to prevent misuse, like `commands.insert(bundle)`. Right now
it's very easy to end up with that code because of a typo, as the `Bundle` api methods are very similarly named.

There are also other pitfalls, like primitive types being automatically `Component`s, but without clear meaning what they represent.
This is an especially severe issue for function pointer types. It's easy to use an enum variant or newtype constructor as
a component, like `.insert(MyEnum::Variant)`, without realising it. This leads to very hard to spot bugs without any hint
by the compiler or at runtime that something went wrong. This is easily preventable by not implementing the `Component` trait for those types.

We also already have `#[derive(Bundle)]` and others. Adding that to `Component` keeps the syntax similar across the board.

Apart from requiring explicit opt-in, `derive` gives us a natural place to define more metadata about specific component type.
One way to use that is to declare component's Storage type statically, instead of requiring manual registration of that metadata
into the world.

```rust
#[derive(Component)]
#[component(storage = "SparseSet")]
struct MyComponent(..);
```

This enables the compiler to perform much more optimizations in the query iteration code, as a lot of the code paths can be statically eliminated.

## User-facing explanation

In order to define your own component type, you have to implement the `Component` trait. The easiest way to do that is using a `derive` macro.

```rust
#[derive(Component)]
struct HitPoints(u32);
```

If you forget to properly annotate your component type, the compiler will remind you
of that as soon as you try to insert that component into the world or query for it. Apart from type safety, this also provides a visual indication that the given type
is intended to be directly associated with entities in the world.

By default, your component will be stored using Dense storage strategy.
In order to declare your component's storage strategy manually, you have to provide an additional attribute
to specify the component settings.

```rust
#[derive(Component)]
#[component(storage = "SparseSet")]
struct Selected;
```

### Migration strategy

All your component types must be annotated with a derive.

```diff
+ #[derive(Component)]
struct MyComponent { .. }
```

You can no longer use primitive types (e.g. `u32`, `bool`) as components.
Wrap them in a custom struct, and mark that as a component.

```diff
+ #[derive(Component)]
+ struct HitPoints(u32);

- commands.entity(id).insert(100);
+ commands.entity(id).insert(HitPoints(100));
```

Remove all registration of components. Make sure to correctly annotate components that were previously registered with `SparseSet` storage type.

```diff
- world.register_component(
- ComponentDescriptor::new::<MyComponent>(StorageType::SparseSet)
- ).unwrap();

+ #[derive(Component)]
+ #[component(storage = "SparseSet")]
struct MyComponent { .. }
```

## Implementation strategy

- remove the existing blanket impl
- implement basic derive macro that just implements a marker trait
- add associated `Storage` type to the `Component` trait
- provide a way to specify the `Storage` type using an macro attribute.
- use the associated `Storage` type inside query iterators to statically determine
if the query is sparse or dense.

## Drawbacks

Requiring extra annotation on every component type adds some boilerplate.

## Rationale and alternatives

- instead of `derive`, use an attribute macro.
- Do nothing and keep registering metadata at runtime. Live with the branching inside query iterators.
- Require manual implementation of `Component` trait without providing derive macro.

Frizi marked this conversation as resolved.
Show resolved Hide resolved
## Unresolved questions

- How to support dynamic components for future scripting layer?

## Future possibilities

The Component derive could later lead to automatic derives for reflection, or in general serve
as a way to reduce boilerplate from new features of the engine added in the future.