Skip to content

Conversation

ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented Aug 11, 2025

Objective

Add constructor functions for each variant of Val
Fixes #15937

Solution

Add constructor functions for each variant of Val.

Doc comments feel a bit clumsy, not sure how to rephrase them, maybe doesn't matter. Perhaps it's not necessary to repeat the enum documentation either, not sure.

@ickshonpe ickshonpe added D-Trivial Nice and easy! A great choice to get started with Bevy A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 11, 2025
@ickshonpe ickshonpe added the M-Needs-Release-Note Work that should be called out in the blog due to impact label Aug 11, 2025
@TheBlckbird
Copy link
Contributor

TheBlckbird commented Aug 12, 2025

I like this, but what about doing something like mentioned in #15943 (review):

pub trait AsValNum {
    fn as_val_num(self) -> f32;
}

impl AsValNum for i32 {
    fn as_val_num(self) -> f32 {
        self as f32
    }
}

impl AsValNum for f32 {
    fn as_val_num(self) -> f32 {
        self
    }
}

pub fn px(n: AsValNum) -> Val {
    Val::Px(n.as_val_num())
}

I think, that'd be nicer than doing 10. in my opinion and might allow for some future changes without breaking old code.

@ickshonpe
Copy link
Contributor Author

ickshonpe commented Aug 12, 2025

I like this, but what about doing something like mentioned in #15943 (review):

pub trait AsValNum {
    fn as_val_num(self) -> f32;
}

impl AsValNum for i32 {
    fn as_val_num(self) -> f32 {
        self as f32
    }
}

impl AsValNum for f32 {
    fn as_val_num(self) -> f32 {
        self
    }
}

pub fn px(n: AsValNum) -> Val {
    Val::Px(n.as_val_num())
}

I think, that'd be nicer than doing 10. in my opinion and might allow for some future changes without breaking old code.

I think so too but afaik the type inference will break with bsn!. Also, the changes with this PR are super noncontroversial. It's an easy decision to merge them without discussion, that's also why I split off the From<f32> impl into a seperate PR.
We can always look into improving the implementation in a follow up, once this is merged.

@TheBlckbird
Copy link
Contributor

I think so too but afaik the type inference will break with bsn!. Also, the changes with this PR are super noncontroversial. It's an easy decision to merge them without discussion, that's also why I split off the From<f32> impl into a seperate PR. We can always look into improving the implementation in a follow up, once this is merged.

ok, that makes sense

@alice-i-cecile alice-i-cecile requested a review from cart August 12, 2025 19:59
@alice-i-cecile alice-i-cecile added S-Needs-SME Decision or review from an SME is required and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 12, 2025
@alice-i-cecile alice-i-cecile added this to the 0.17 milestone Aug 12, 2025
@alice-i-cecile
Copy link
Member

Adding to the 0.17 milestone and assigning @cart to review. I just want to be sure that this is in line with the final plan: we've gone back and forth a ton here but I don't want to leave users hanging longer if we're ready to decide.

}

/// Returns a [`Val::Px`] representing a value in logical pixels.
pub const fn px(value: f32) -> Val {
Copy link
Member

Choose a reason for hiding this comment

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

The alternative to this is adding use Val::* to the prelude, which feels less "special" to me, ties the docs directly to Val, keeps our code size down, and aligns with Rust std patterns (ex: Some, None, Ok, Err, etc).

The only problem is right now bsn! breaks on this case (as it assumes it can do parent_type.value.0 to set Some(x)). This is obviously a problem! The solution is to use if let to destructure all types. I believe this will also solve one of the outstanding autocomplete issues (and it will remove the need for the touch_type hack).

Copy link
Member

Choose a reason for hiding this comment

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

I'm going to strongly advocate that we do use Val::* instead.

Copy link
Contributor

@TheBlckbird TheBlckbird Aug 12, 2025

Choose a reason for hiding this comment

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

I think it just comes down to whether we wanna allow more types besides just f32 or not #20518 (comment).
But I think a decision needs to be made and you're the one to do it. I personally like the idea of being able to omit the decimal seperator, especially because this PR is about code readability, but it is ultimately up to you.

I just think, it would be nice to ship this in 0.17 (although it isn't important enough to be blocking, as @alice-i-cecile said on discord)

Copy link
Member

Choose a reason for hiding this comment

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

Arg yeah that does make this comfier. And currently type inference actually doesn't break in bsn! right now, as function arguments don't have implicit into().

Copy link
Member

Choose a reason for hiding this comment

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

Arguably less rusty, and it means we're encouraging people to throw a bunch of "int to float" casts everywhere. But I'll admit to hitting the "oops please make it a float" friction on the regular.

Copy link
Member

Choose a reason for hiding this comment

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

It does also mean we'll likely need to special case these functions somehow in BSN asset files (if we want to support them there). But I'm content making that a problem for future us.

Consider this approval for function helpers with type conversion.

Copy link
Contributor

@TheBlckbird TheBlckbird Aug 12, 2025

Choose a reason for hiding this comment

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

Sorry to bother you one more time, but now I'm a bit confused: what do you actually mean by this? That we might add automatic type conversion in a later PR or that it will be done by BSN?

But thanks for finally merging this, this will already make things way easier than before!

Copy link
Member

Choose a reason for hiding this comment

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

It will be done by BSN. There's automatic .into() calls via the macro now.

Copy link
Member

@cart cart Aug 12, 2025

Choose a reason for hiding this comment

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

@alice-i-cecile thats not what I meant in this case (see my message above about function parameters not having implicit into). Instead, we should add a trait as mentioned elsewhere.

@cart cart mentioned this pull request Aug 12, 2025
@alice-i-cecile alice-i-cecile modified the milestones: 0.17, 0.18 Aug 12, 2025
@alice-i-cecile alice-i-cecile added S-Needs-Design This issue requires design work to think about how it would best be accomplished and removed S-Needs-SME Decision or review from an SME is required labels Aug 12, 2025
@cart cart removed this from the 0.18 milestone Aug 12, 2025
@cart cart added this to the 0.17 milestone Aug 12, 2025
@cart cart added this pull request to the merge queue Aug 12, 2025
Merged via the queue into bevyengine:main with commit 1ec9804 Aug 12, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy M-Needs-Release-Note Work that should be called out in the blog due to impact S-Needs-Design This issue requires design work to think about how it would best be accomplished
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Val helper functions
4 participants