Skip to content

Commit e37bf18

Browse files
authored
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
1 parent d2fa55d commit e37bf18

File tree

1 file changed

+39
-0
lines changed
  • crates/bevy_render/src/view/visibility

1 file changed

+39
-0
lines changed

crates/bevy_render/src/view/visibility/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use crate::{
2121
primitives::{Aabb, Frustum, Sphere},
2222
};
2323

24+
use thiserror::Error;
25+
2426
use super::NoCpuCulling;
2527

2628
/// User indication of whether an entity is visible. Propagates down the entity hierarchy.
@@ -47,6 +49,43 @@ pub enum Visibility {
4749
Visible,
4850
}
4951

52+
/// Enum of errors that could occur during conversion to [`bool`]
53+
#[non_exhaustive]
54+
#[derive(Error, Debug)]
55+
pub enum VisibilityToBoolConversionError {
56+
#[error("The variant `{0:?}` cannot be converted to a bool")]
57+
VariantNotSupported(Visibility),
58+
}
59+
/// Implements conversion from bool to Visibility
60+
/// `true` corresponds to [`Visibility::Visible`], while false corresponds to [`Visibility::Hidden`].
61+
impl From<bool> for Visibility {
62+
fn from(visible: bool) -> Visibility {
63+
if visible {
64+
Visibility::Visible
65+
} else {
66+
Visibility::Hidden
67+
}
68+
}
69+
}
70+
71+
/// Implements conversion from [`Visibility`] to [`bool`]
72+
/// - returns `Ok(true)` if `Visibility::Visible`
73+
/// - returns `Ok(false)` if `Visibility::Hidden`
74+
/// - returns `Err()` if `Visibility::Inherited`
75+
impl TryFrom<Visibility> for bool {
76+
type Error = VisibilityToBoolConversionError;
77+
78+
fn try_from(visible: Visibility) -> Result<Self, Self::Error> {
79+
match visible {
80+
Visibility::Hidden => Ok(false),
81+
Visibility::Visible => Ok(true),
82+
Visibility::Inherited => Err(VisibilityToBoolConversionError::VariantNotSupported(
83+
Visibility::Inherited,
84+
)),
85+
}
86+
}
87+
}
88+
5089
// Allows `&Visibility == Visibility`
5190
impl PartialEq<Visibility> for &Visibility {
5291
#[inline]

0 commit comments

Comments
 (0)