Skip to content

add AddAssign operator to ByteSize #22

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

Merged
merged 8 commits into from
Jul 30, 2021
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
74 changes: 55 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
extern crate serde;

use std::fmt::{Debug, Display, Formatter, Result};
use std::ops::{Add, Mul};
use std::ops::{Add, AddAssign, Mul, MulAssign};

/// byte size for 1 byte
pub const B: u64 = 1;
Expand Down Expand Up @@ -217,14 +217,6 @@ impl Debug for ByteSize {

macro_rules! commutative_op {
($t:ty) => {
impl Add<$t> for ByteSize {
type Output = ByteSize;
#[inline(always)]
fn add(self, rhs: $t) -> ByteSize {
ByteSize(self.0 + (rhs as u64))
}
}

impl Add<ByteSize> for $t {
type Output = ByteSize;
#[inline(always)]
Expand All @@ -233,14 +225,6 @@ macro_rules! commutative_op {
}
}

impl Mul<$t> for ByteSize {
type Output = ByteSize;
#[inline(always)]
fn mul(self, rhs: $t) -> ByteSize {
ByteSize(self.0 * (rhs as u64))
}
}

impl Mul<ByteSize> for $t {
type Output = ByteSize;
#[inline(always)]
Expand All @@ -265,25 +249,71 @@ impl Add<ByteSize> for ByteSize {
}
}

impl AddAssign<ByteSize> for ByteSize {
#[inline(always)]
fn add_assign(&mut self, rhs: ByteSize) {
self.0 += rhs.0
}
}

impl<T> Add<T> for ByteSize
where T: Into<u64> {
type Output = ByteSize;
#[inline(always)]
fn add(self, rhs: T) -> ByteSize {
ByteSize(self.0 + (rhs.into() as u64))
}
}

impl<T> AddAssign<T> for ByteSize
where T: Into<u64> {
#[inline(always)]
fn add_assign(&mut self, rhs: T) {
self.0 += rhs.into() as u64;
}
}

impl<T> Mul<T> for ByteSize
where T: Into<u64> {
type Output = ByteSize;
#[inline(always)]
fn mul(self, rhs: T) -> ByteSize {
ByteSize(self.0 * (rhs.into() as u64))
}
}

impl<T> MulAssign<T> for ByteSize
where T: Into<u64> {
#[inline(always)]
fn mul_assign(&mut self, rhs: T) {
self.0 *= rhs.into() as u64;
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_arithmetic_op() {
let x = ByteSize::mb(1);
let mut x = ByteSize::mb(1);
let y = ByteSize::kb(100);

assert_eq!((x + y).as_u64(), 1_100_000u64);

assert_eq!((x + (100 * 1000) as u64).as_u64(), 1_100_000);

assert_eq!((x * 2u64).as_u64(), 2_000_000);

x += y;
assert_eq!(x.as_u64(), 1_100_000);
x *= 2u64;
assert_eq!(x.as_u64(), 2_200_000);
}

#[test]
fn test_arithmetic_primitives() {
let x = ByteSize::mb(1);
let mut x = ByteSize::mb(1);

assert_eq!((x + MB as u64).as_u64(), 2_000_000);

Expand All @@ -292,6 +322,12 @@ mod tests {
assert_eq!((x + KB as u16).as_u64(), 1_001_000);

assert_eq!((x + B as u8).as_u64(), 1_000_001);

x += MB as u64;
x += MB as u32;
x += 10 as u16;
x += 1 as u8;
assert_eq!(x.as_u64(), 3_000_011);
}

#[test]
Expand Down