From f9e050447597bfdafd592f0a0a61aae3264cda86 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Thu, 1 Feb 2024 14:19:34 +0100 Subject: [PATCH] Implement `Sum` and release a new version (#53) This change implements the `Sum` traits so that iterators of `Au` can be summed. We would like to do this in Servo. In addition, it also prepares for a new release of the crate, so this can be used. --- Cargo.toml | 2 +- src/app_unit.rs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index dfbefd5..f861ae3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "app_units" -version = "0.7.3" +version = "0.7.4" authors = ["The Servo Project Developers"] description = "Servo app units type (Au)" documentation = "https://docs.rs/app_units/" diff --git a/src/app_unit.rs b/src/app_unit.rs index a8892a0..5fa9770 100644 --- a/src/app_unit.rs +++ b/src/app_unit.rs @@ -8,6 +8,7 @@ use num_traits::Zero; use serde::{Serialize, Deserialize, Deserializer}; use std::{fmt, i32, default::Default, ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign}}; +use std::iter::Sum; /// The number of app units in a pixel. pub const AU_PER_PX: i32 = 60; @@ -178,6 +179,12 @@ impl DivAssign for Au { } } +impl<'a> Sum<&'a Self> for Au { + fn sum>(iter: I) -> Self { + iter.fold(Self::zero(), |a, b| a + *b) + } +} + impl Au { /// FIXME(pcwalton): Workaround for lack of cross crate inlining of newtype structs! #[inline]