Skip to main content

ensure

Macro ensure 

Source
macro_rules! ensure {
    ($condition:expr, $($args:tt)+) => { ... };
}
Expand description

Returns early with an error if a condition is false.

Equivalent to if !condition { bail!(...) }. As with bail!, the returned error defaults to Severity::Panic, and the severity can be changed by providing it after the condition.

ยงExample

use bevy_ecs::{ensure, error::{BevyError, Severity}};

fn validate_score(score: i32) -> Result<(), BevyError> {
    ensure!(score >= 0, "score must not be negative: {}", score);
    ensure!(score <= 100, Severity::Warning, "score is too high: {}", score);
    Ok(())
}