-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement FromStr for Val #16926
base: main
Are you sure you want to change the base?
Implement FromStr for Val #16926
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A sensible addition IMO. I would expect many bsn
-like alternatives would have a similar implementation anyway, so it might as well be first-party. I would like this to be const though, just for that extra utility.
impl core::str::FromStr for Val { | ||
type Err = ValParseError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would love to see this pulled out as a const
function if it's compatible. Not a blocker if it's not a simple change though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked and every single function that's being called is non-const 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically: str::trim
, str::bytes()
, iterator::position
, str::split_at
, ?
, f32::parse
are not const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Damn, thanks for clarifying!
return Ok(Val::Auto); | ||
} | ||
|
||
let Some(end_of_number) = s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only small nit with this PR is that this doesn't accommodate all of rust's valid f32
parsing inputs; I don't think people will often want to make values of 1e5 px
or whatever, but it's possible that values like that may show up in automatically-formatted float output, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't support: NaN
, inf
, infinity
, +inf
, 1E4
and 1e5
. I think most developers won't need this, and it can be added later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. That's why I approved it :)
Objective
This PR implements
FromStr
forVal
, so developers can parse values like10px
and50%
Testing
Added tests for this. I think they cover pretty much everything, and it's a fairly simple unit test.
Limitations
Currently the following float values are not parsed:
inf
,-inf
,+infinity
,NaN
2.5E10
,2.5e10
,2.5E-10
For my use case this is perfectly fine but other developers might want to support these values