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

Use f64::clamp #44

Merged
merged 2 commits into from
Jan 6, 2025
Merged

Use f64::clamp #44

merged 2 commits into from
Jan 6, 2025

Conversation

tomcur
Copy link
Member

@tomcur tomcur commented Jan 3, 2025

Stabilized in Rust 1.50, and resvg now has MSRV 1.65.

Stabilized in Rust 1.50, and resvg now has MSRV 1.65.
@@ -310,7 +310,7 @@ fn f64_bound(min: f64, val: f64, max: f64) -> f64 {
debug_assert!(min.is_finite());
debug_assert!(val.is_finite());
debug_assert!(max.is_finite());
val.max(min).min(max)
val.clamp(min, max)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does it matter that this might introduce a panic? (In the face of wrong code…)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, but I don't think it matters. min and max aren't set by user code.

Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

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

I nearly brought this up in #43 before seeing this PR. I think this is reasonable

src/color.rs Outdated
Comment on lines 310 to 312
debug_assert!(min.is_finite());
debug_assert!(val.is_finite());
debug_assert!(max.is_finite());
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
debug_assert!(min.is_finite());
debug_assert!(val.is_finite());
debug_assert!(max.is_finite());
debug_assert!(val.is_finite());

I don't think the f64::INFINITY case is likely to come up for min or max.

(Fwiw, I think the last assertion is a bit dodgy; infinity is fine, it's just NaN that we don't want to propagate)

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that one is a bit dodgy, infinity would get clamped to the almost-surely-finite min or max anyway.

Copy link
Member Author

@tomcur tomcur Jan 6, 2025

Choose a reason for hiding this comment

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

I'll just merge it as-is (with that assertion) and not spend too much thought on it, as it's likely to go away anyway. This keeps the same behavior, which may be useful for the patch release.

Co-authored-by: Daniel McNab <[email protected]>
@tomcur tomcur added this pull request to the merge queue Jan 6, 2025
Merged via the queue into main with commit 5c115f9 Jan 6, 2025
15 checks passed
@tomcur tomcur deleted the clamp branch January 6, 2025 11:18
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.

3 participants