Skip to content

Add ensure! macro for BevyError#25061

Open
0xEgao wants to merge 1 commit into
bevyengine:mainfrom
0xEgao:ensure!
Open

Add ensure! macro for BevyError#25061
0xEgao wants to merge 1 commit into
bevyengine:mainfrom
0xEgao:ensure!

Conversation

@0xEgao

@0xEgao 0xEgao commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Objective

Bevy provides bevy_error! and bail! for error handling, but lacks a concise way to return an error when a condition is false.

Fixes #24645.

Solution

  • Add an ensure! macro that delegates to bail! when its condition is false.
  • Support the existing error-message formatting and optional Severity syntax.
  • Add API documentation, regression test and usage examples.

Testing

  • RUST_BACKTRACE=1 cargo test -p bevy_ecs --lib
  • cargo test -p bevy_ecs --doc ensure
  • cargo clippy -p bevy_ecs --lib -- -D warnings
  • cargo fmt --all -- --check

Showcase

ensure!(score >= 0, "score must not be negative: {}", score);

ensure!(
    score <= 100,
    Severity::Warning,
    "score is too high: {}",
    score
);

@alice-i-cecile alice-i-cecile left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a cross-link from bail! for discoverability :)

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jul 18, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in ECS Jul 18, 2026
@0xEgao

0xEgao commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

Needs a cross-link from bail! for discoverability :)

Done :)

@Zeophlite Zeophlite added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

Add a macro similar to anyhow::ensure!

3 participants