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

Add from_f32_px_trunc and from_f64_px_trunc methods #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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_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_trunc(5.0), Au(300));
assert_eq!(Au::from_f32_px_trunc(5.2), Au(312));
assert_eq!(Au::from_f32_px_trunc(5.5), Au(330));
assert_eq!(Au::from_f32_px_trunc(5.8), Au(348));
assert_eq!(Au::from_f32_px_trunc(6.), Au(360));
assert_eq!(Au::from_f32_px_trunc(6.12), Au(367));
assert_eq!(Au::from_f32_px_trunc(6.13), Au(367));

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

#[test]
Expand Down