From b7de9e543715fdb730fc6ed0f2059235952635dd Mon Sep 17 00:00:00 2001 From: Andy Grover Date: Mon, 24 Apr 2017 17:21:09 -0700 Subject: [PATCH] Implement iter::Sum for Sectors and Bytes This is handy for adding up lengths of Segments. Signed-off-by: Andy Grover --- src/types.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index 8a6db33d..265617e9 100644 --- a/src/types.rs +++ b/src/types.rs @@ -6,7 +6,8 @@ use consts::SECTOR_SIZE; use std::fmt; use std::fmt::Display; -use std::ops::{Div, Mul, Rem}; +use std::iter::Sum; +use std::ops::{Div, Mul, Rem, Add}; use serde; // macros for unsigned operations on Sectors and Bytes @@ -94,6 +95,12 @@ impl Bytes { } } +impl Sum for Bytes { + fn sum>(iter: I) -> Bytes { + iter.fold(Bytes(0), Add::add) + } +} + unsigned_mul!(u64, Bytes); unsigned_mul!(u32, Bytes); unsigned_mul!(u16, Bytes); @@ -141,6 +148,12 @@ impl serde::Deserialize for Sectors { } } +impl Sum for Sectors { + fn sum>(iter: I) -> Sectors { + iter.fold(Sectors(0), Add::add) + } +} + unsigned_div!(u64, Sectors); unsigned_div!(u32, Sectors); unsigned_div!(u16, Sectors);