Skip to content
Merged
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
37 changes: 37 additions & 0 deletions bsize/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,36 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use core::iter;

use crate::ByteSize;
use crate::traits::BaseByteSize;

#[cfg(feature = "nightly")]
mod nightly;
#[cfg(not(feature = "nightly"))]
mod stable;

impl<T> iter::Sum for ByteSize<T>
where
T: BaseByteSize + iter::Sum,
{
#[inline(always)]
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
ByteSize(iter.map(ByteSize::bytes).sum())
}
}

impl<'a, T> iter::Sum<&'a ByteSize<T>> for ByteSize<T>
where
T: BaseByteSize + iter::Sum,
{
#[inline(always)]
fn sum<I: Iterator<Item = &'a ByteSize<T>>>(iter: I) -> Self {
iter.copied().sum()
}
}

#[cfg(test)]
mod tests {
use crate::BSize;
Expand Down Expand Up @@ -56,4 +81,16 @@ mod tests {
size -= BSize::b(5);
assert_eq!(size.bytes(), 3);
}

#[test]
fn sums_byte_sizes() {
let sizes = [BSize64::b(3), BSize64::b(5), BSize64::b(8)];

assert_eq!(sizes.into_iter().sum::<BSize64>(), BSize64::b(16));
assert_eq!(sizes.iter().sum::<BSize64>(), BSize64::b(16));
assert_eq!(
core::iter::empty::<BSize64>().sum::<BSize64>(),
BSize64::b(0)
);
}
}