Skip to content

Commit

Permalink
Implement Sum and release a new version (#53)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mrobinson authored Feb 1, 2024
1 parent 560e7d4 commit f9e0504
Show file tree
Hide file tree
Showing 2 changed files with 8 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.3"
version = "0.7.4"
authors = ["The Servo Project Developers"]
description = "Servo app units type (Au)"
documentation = "https://docs.rs/app_units/"
Expand Down
7 changes: 7 additions & 0 deletions src/app_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,6 +179,12 @@ impl DivAssign<i32> for Au {
}
}

impl<'a> Sum<&'a Self> for Au {
fn sum<I: Iterator<Item = &'a Self>>(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]
Expand Down

0 comments on commit f9e0504

Please sign in to comment.