mirror of
https://github.com/bevyengine/bevy
synced 2024-11-13 00:17:27 +00:00
Add conversions between Visibility and bool (#14784)
# Objective Fixes #14521. ## Solution Added to methods to the VIsibility. ```rs is_visible() -> Result<bool, String> ``` and ```rs visbility_from_bool(bool) -> Visibility ``` ## Testing Ran * `cargo run -p ci -- lints` * `cargo run -p ci -- test` * `cargo run -p ci -- compile` it seems to be working. However I got few error messages :`ERROR bevy_log: could not set global logger and tracing subscriber as they are already set. Consider disabling LogPlugin` in `cargo run -p ci -- test`, even though all test passed. I'm not sure if that's expected behaviour Ps. I'm new to contributing, please correct me if anything is wrong
This commit is contained in:
parent
d2fa55db6b
commit
e37bf18e2b
1 changed files with 39 additions and 0 deletions
|
@ -21,6 +21,8 @@ use crate::{
|
||||||
primitives::{Aabb, Frustum, Sphere},
|
primitives::{Aabb, Frustum, Sphere},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
use super::NoCpuCulling;
|
use super::NoCpuCulling;
|
||||||
|
|
||||||
/// User indication of whether an entity is visible. Propagates down the entity hierarchy.
|
/// User indication of whether an entity is visible. Propagates down the entity hierarchy.
|
||||||
|
@ -47,6 +49,43 @@ pub enum Visibility {
|
||||||
Visible,
|
Visible,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enum of errors that could occur during conversion to [`bool`]
|
||||||
|
#[non_exhaustive]
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum VisibilityToBoolConversionError {
|
||||||
|
#[error("The variant `{0:?}` cannot be converted to a bool")]
|
||||||
|
VariantNotSupported(Visibility),
|
||||||
|
}
|
||||||
|
/// Implements conversion from bool to Visibility
|
||||||
|
/// `true` corresponds to [`Visibility::Visible`], while false corresponds to [`Visibility::Hidden`].
|
||||||
|
impl From<bool> for Visibility {
|
||||||
|
fn from(visible: bool) -> Visibility {
|
||||||
|
if visible {
|
||||||
|
Visibility::Visible
|
||||||
|
} else {
|
||||||
|
Visibility::Hidden
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implements conversion from [`Visibility`] to [`bool`]
|
||||||
|
/// - returns `Ok(true)` if `Visibility::Visible`
|
||||||
|
/// - returns `Ok(false)` if `Visibility::Hidden`
|
||||||
|
/// - returns `Err()` if `Visibility::Inherited`
|
||||||
|
impl TryFrom<Visibility> for bool {
|
||||||
|
type Error = VisibilityToBoolConversionError;
|
||||||
|
|
||||||
|
fn try_from(visible: Visibility) -> Result<Self, Self::Error> {
|
||||||
|
match visible {
|
||||||
|
Visibility::Hidden => Ok(false),
|
||||||
|
Visibility::Visible => Ok(true),
|
||||||
|
Visibility::Inherited => Err(VisibilityToBoolConversionError::VariantNotSupported(
|
||||||
|
Visibility::Inherited,
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Allows `&Visibility == Visibility`
|
// Allows `&Visibility == Visibility`
|
||||||
impl PartialEq<Visibility> for &Visibility {
|
impl PartialEq<Visibility> for &Visibility {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
Loading…
Reference in a new issue