Remove safe_div (#3572)

This commit is contained in:
Laurenz 2024-03-07 17:07:52 +01:00 committed by GitHub
parent 1d32145319
commit 1fa0f2f0f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 15 deletions

View File

@ -117,17 +117,6 @@ impl Abs {
pub fn approx_eq(self, other: Self) -> bool {
self == other || (self - other).to_raw().abs() < 1e-6
}
/// Perform a checked division by a number, returning zero if the result
/// is not finite.
pub fn safe_div(self, number: f64) -> Self {
let result = self.to_raw() / number;
if result.is_finite() {
Self::raw(result)
} else {
Self::zero()
}
}
}
impl Numeric for Abs {

View File

@ -122,5 +122,5 @@ fn shrink(size: Size, padding: Sides<Rel<Abs>>) -> Size {
/// <=> (1 - p.rel) * w = s + p.abs
/// <=> w = (s + p.abs) / (1 - p.rel)
fn grow(size: Size, padding: Sides<Rel<Abs>>) -> Size {
size.zip_map(padding.sum_by_axis(), |s, p| (s + p.abs).safe_div(1.0 - p.rel.get()))
size.zip_map(padding.sum_by_axis(), |s, p| (s + p.abs) / (1.0 - p.rel.get()))
}

View File

@ -209,7 +209,7 @@ impl LayoutSingle for Packed<ImageElem> {
region
} else if expand.x {
// If just width is forced, take it.
Size::new(region.x, region.y.min(region.x.safe_div(px_ratio)))
Size::new(region.x, region.y.min(region.x / px_ratio))
} else if expand.y {
// If just height is forced, take it.
Size::new(region.x.min(region.y * px_ratio), region.y)
@ -220,7 +220,7 @@ impl LayoutSingle for Packed<ImageElem> {
let natural = Axes::new(pxw, pxh).map(|v| Abs::inches(v / dpi));
Size::new(
natural.x.min(region.x).min(region.y * px_ratio),
natural.y.min(region.y).min(region.x.safe_div(px_ratio)),
natural.y.min(region.y).min(region.x / px_ratio),
)
};
@ -229,7 +229,7 @@ impl LayoutSingle for Packed<ImageElem> {
let fitted = match fit {
ImageFit::Cover | ImageFit::Contain => {
if wide == (fit == ImageFit::Contain) {
Size::new(target.x, target.x.safe_div(px_ratio))
Size::new(target.x, target.x / px_ratio)
} else {
Size::new(target.y * px_ratio, target.y)
}

View File

@ -0,0 +1,8 @@
// Test that image measurement doesn't turn `inf / some-value` into 0pt.
// Ref: false
---
#context {
let size = measure(image("/assets/images/tiger.jpg"))
test(size, (width: 1024pt, height: 670pt))
}