Skip to content

Commit

Permalink
Add from_f32_px_by_trunc and from_f64_px_by_trunc methods
Browse files Browse the repository at this point in the history
Signed-off-by: asun0204 <asun0204@163.com>
  • Loading branch information
Asun0204 authored and kongbai1996 committed Jan 9, 2025
1 parent 480397f commit 4500eb0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "app_units"
version = "0.7.6"
version = "0.7.7"
authors = ["The Servo Project Developers"]
description = "Servo app units type (Au)"
documentation = "https://docs.rs/app_units/"
Expand Down
28 changes: 28 additions & 0 deletions src/app_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ impl Au {
Au::from_f64_au(float)
}

#[inline]
pub fn from_f32_px_by_trunc(px: f32) -> Au {
let float = (px * AU_PER_PX as f32).trunc();
Au::from_f64_au(float as f64)
}

#[inline]
pub fn from_f64_px_by_trunc(px: f64) -> Au {
let float = (px * AU_PER_PX as f64).trunc();
Au::from_f64_au(float)
}

#[inline]
pub fn abs(self) -> Self {
Au(self.0.abs())
Expand Down Expand Up @@ -407,6 +419,22 @@ fn convert() {
assert_eq!(Au::from_f64_px(6.), Au(360));
assert_eq!(Au::from_f64_px(6.12), Au(367));
assert_eq!(Au::from_f64_px(6.13), Au(368));

assert_eq!(Au::from_f32_px_truncate(5.0), Au(300));
assert_eq!(Au::from_f32_px_truncate(5.2), Au(312));
assert_eq!(Au::from_f32_px_truncate(5.5), Au(330));
assert_eq!(Au::from_f32_px_truncate(5.8), Au(348));
assert_eq!(Au::from_f32_px_truncate(6.), Au(360));
assert_eq!(Au::from_f32_px_truncate(6.12), Au(367));
assert_eq!(Au::from_f32_px_truncate(6.13), Au(367));

assert_eq!(Au::from_f64_px_truncate(5.), Au(300));
assert_eq!(Au::from_f64_px_truncate(5.2), Au(312));
assert_eq!(Au::from_f64_px_truncate(5.5), Au(330));
assert_eq!(Au::from_f64_px_truncate(5.8), Au(348));
assert_eq!(Au::from_f64_px_truncate(6.), Au(360));
assert_eq!(Au::from_f64_px_truncate(6.12), Au(367));
assert_eq!(Au::from_f64_px_truncate(6.13), Au(367));
}

#[test]
Expand Down

0 comments on commit 4500eb0

Please sign in to comment.