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

Make PhysicsLayer require a #[default] variant #494

Merged
merged 5 commits into from
Aug 23, 2024
Merged

Conversation

Jondolf
Copy link
Owner

@Jondolf Jondolf commented Aug 22, 2024

Objective

#476 changed collision layers to reserve the first bit for a default layer. However, for enums implementing PhysicsLayer, the default layer is rather implicit and potentially footgunny.

The default layer should ideally be more explicit.

Solution

Change PhysicsLayer to require Default to be implemented. The proc macro orders the variants such that the layer set as the default is always the first bit.

Note that Default must currently be derived instead of implemented manually, as the macro can only access the default variant if the #[default] attribute exists. An error is emitted if #[default] does not exist. If someone knows a way to make it work with the manual impl, let me know or consider opening a PR!

I also added documentation for the PhysicsLayer macro, and improved error handling.


Migration Guide

Enums that derive PhysicsLayer must now also derive Default and specify a default layer using the #[default] attribute.

Before:

#[derive(PhysicsLayer)]
enum GameLayer {
    Player,
    Enemy,
    Ground,
}

After:

#[derive(PhysicsLayer, Default)]
enum GameLayer {
    #[default]
    Default, // The name doesn't matter, but Default is used here for clarity
    Player,
    Enemy,
    Ground,
}

The default layer always has the bit value 0b0001 regardless of its order relative to the other variants.

@Jondolf Jondolf added A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality C-Usability A quality-of-life improvement that makes Avian easier to use labels Aug 22, 2024
Co-authored-by: Jan Hohenheim <jan@hohenheim.ch>
@Jondolf Jondolf merged commit 89b733e into main Aug 23, 2024
4 checks passed
@Jondolf Jondolf deleted the explicit-default-layer branch August 23, 2024 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality C-Usability A quality-of-life improvement that makes Avian easier to use
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants