Skip to content

Make PhysicsLayer require a #[default] variant#494

Merged
Jondolf merged 5 commits into
mainfrom
explicit-default-layer
Aug 23, 2024
Merged

Make PhysicsLayer require a #[default] variant#494
Jondolf merged 5 commits into
mainfrom
explicit-default-layer

Conversation

@Jondolf

@Jondolf Jondolf commented Aug 22, 2024

Copy link
Copy Markdown
Member

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
Comment thread crates/avian_derive/src/lib.rs
Comment thread crates/avian_derive/src/lib.rs Outdated
Co-authored-by: Jan Hohenheim <jan@hohenheim.ch>
@Jondolf Jondolf merged commit 89b733e into main Aug 23, 2024
@Jondolf Jondolf deleted the explicit-default-layer branch August 23, 2024 08:39
@Jondolf Jondolf added the M-Migration-Guide A breaking change to Avian's public API that needs to be noted in a migration guide label Nov 27, 2024
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 M-Migration-Guide A breaking change to Avian's public API that needs to be noted in a migration guide

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants