Skip to content

Add a trait for the Val helper functions to allow the input of multiple types #20549

@TheBlckbird

Description

@TheBlckbird

What problem does this solve or what need does it fill?

As discussed in #15937 and #20518, we could add a trait for the new helper functions to allow them to accept multiple types.

This would make the code look cleaner, because you could use i32 and f32 instead of writing px(10.), for example.

What solution would you like?

Add a trait AsValNum that with a method that converts something to a float:

pub trait ToValNum {
    fn to_val_num(self) -> f32;
}

impl ToValNum for f32 {
    fn to_val_num(self) -> f32 {
        self
    }
}

impl ToValNum for i32 {
    fn to_val_num(self) -> f32 {
        self as f32
    }
}

pub fn px<T: ToValNum>(n: T) -> Val {
    Val::Px(n.to_val_num())
}

Open decisions to be made:

  • Final decision: Is this what we want (although @cart already said yes?, just putting this in here again)?
  • For which types do we implement this? All integer types? I think, that would make the most sense

What alternative(s) have you considered?

Casting to f32 all the time.

Additional context

Even though the decision whether we want to implement this seems to have already been made, I'm just moving the design discussion to this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-UIGraphical user interfaces, styles, layouts, and widgetsC-UsabilityA targeted quality-of-life change that makes Bevy easier to useS-Needs-ReviewNeeds reviewer attention (from anyone!) to move forwardX-ControversialThere is active debate or serious implications around merging this PR

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions