Skip to content
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

Avoid as casts in default FromPrimitive methods #73

Merged
merged 5 commits into from
Jun 20, 2018

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Jun 20, 2018

Particularly, the default from_f64 used n as i64, which has
undefined behavior on overflow, kind of defeating the purpose here.
Now we use a checked to_i64() for this, and even try to_u64() as a
fallback for completeness.

Also make similar changes to default ToPrimitive, but at least it
didn't have the same casting problem.

Particularly, the default `from_f64` used `n as i64`, which has
undefined behavior on overflow, kind of defeating the purpose here.
Now we use a checked `to_i64()` for this, and even try `to_u64()` as a
fallback for completeness.

(All of the primitive implementations already do better, at least.)
In `to_f64()`, we also try `to_u64()` if `to_i64()` failed.
@cuviper
Copy link
Member Author

cuviper commented Jun 20, 2018

bors r+

bors bot added a commit that referenced this pull request Jun 20, 2018
73: Avoid `as` casts in default FromPrimitive methods r=cuviper a=cuviper

Particularly, the default `from_f64` used `n as i64`, which has
undefined behavior on overflow, kind of defeating the purpose here.
Now we use a checked `to_i64()` for this, and even try `to_u64()` as a
fallback for completeness.

Also make similar changes to default `ToPrimitive`, but at least it
didn't have the same casting problem.

Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors
Copy link
Contributor

bors bot commented Jun 20, 2018

Build succeeded

@bors bors bot merged commit 7140579 into rust-num:master Jun 20, 2018
@cuviper cuviper deleted the robust-from branch February 8, 2024 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant