Skip to content

Commit

Permalink
Implement Rem, arithmetic operations for reference types (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 6, 2023
1 parent c4fbb7c commit 056b160
Show file tree
Hide file tree
Showing 2 changed files with 297 additions and 30 deletions.
176 changes: 164 additions & 12 deletions src/detail/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,209 @@ use quote::quote;

pub(crate) fn implement_arithmetic(name: &syn::Ident) -> TokenStream {
quote! {
impl std::ops::Add for #name {
impl std::ops::Add<Self> for #name {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self(self.value() + rhs.value())
Self::new(self.value() + rhs.value())
}
}

impl std::ops::AddAssign for #name {
impl std::ops::Add<&Self> for #name {
type Output = <Self as std::ops::Add<Self>>::Output;

fn add(self, rhs: &Self) -> Self::Output {
Self::new(self.value() + rhs.value())
}
}

impl<'a> std::ops::Add<#name> for &'a #name {
type Output = #name;

fn add(self, rhs: #name) -> Self::Output {
#name::new(self.value() + rhs.value())
}
}

impl<'a> std::ops::Add<&#name> for &'a #name {
type Output = #name;

fn add(self, rhs: &#name) -> Self::Output {
#name::new(self.value() + rhs.value())
}
}

impl std::ops::AddAssign<Self> for #name {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.value()
}
}

impl std::ops::Sub for #name {
impl std::ops::AddAssign<&Self> for #name {
fn add_assign(&mut self, rhs: &Self) {
self.0 += rhs.value()
}
}

impl std::ops::Sub<Self> for #name {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self(self.value() - rhs.value())
Self::new(self.value() - rhs.value())
}
}

impl std::ops::Sub<&#name> for #name {
type Output = Self;
fn sub(self, rhs: &Self) -> Self::Output {
Self::new(self.value() - rhs.value())
}
}

impl std::ops::SubAssign for #name {
impl<'a> std::ops::Sub<#name> for &'a #name {
type Output = #name;
fn sub(self, rhs: #name) -> Self::Output {
#name::new(self.value() - rhs.value())
}
}

impl<'a> std::ops::Sub<&#name> for &'a #name {
type Output = #name;
fn sub(self, rhs: &#name) -> Self::Output {
#name::new(self.value() - rhs.value())
}
}


impl std::ops::SubAssign<Self> for #name {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.value()
}
}

impl std::ops::Mul for #name {
impl std::ops::SubAssign<&Self> for #name {
fn sub_assign(&mut self, rhs: &Self) {
self.0 -= rhs.value()
}
}

impl std::ops::Mul<Self> for #name {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self(self.value() * rhs.value())
Self::new(self.value() * rhs.value())
}
}

impl std::ops::Mul<&Self> for #name {
type Output = Self;
fn mul(self, rhs: &Self) -> Self::Output {
Self::new(self.value() * rhs.value())
}
}

impl std::ops::MulAssign for #name {
impl<'a> std::ops::Mul<#name> for &'a #name {
type Output = #name;
fn mul(self, rhs: #name) -> Self::Output {
#name::new(self.value() * rhs.value())
}
}

impl<'a> std::ops::Mul<&#name> for &'a #name {
type Output = #name;
fn mul(self, rhs: &#name) -> Self::Output {
#name::new(self.value() * rhs.value())
}
}

impl std::ops::MulAssign<Self> for #name {
fn mul_assign(&mut self, rhs: Self) {
self.0 *= rhs.value()
}
}

impl std::ops::Div for #name {
impl std::ops::MulAssign<&Self> for #name {
fn mul_assign(&mut self, rhs: &Self) {
self.0 *= rhs.value()
}
}

impl std::ops::Div<Self> for #name {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.value() / rhs.value())
Self::new(self.value() / rhs.value())
}
}

impl std::ops::Div<&Self> for #name {
type Output = Self;
fn div(self, rhs: &Self) -> Self::Output {
Self::new(self.value() / rhs.value())
}
}

impl<'a> std::ops::Div<#name> for &'a #name {
type Output = #name;
fn div(self, rhs: #name) -> Self::Output {
#name::new(self.value() / rhs.value())
}
}

impl<'a> std::ops::Div<&#name> for &'a #name {
type Output = #name;
fn div(self, rhs: &#name) -> Self::Output {
#name::new(self.value() / rhs.value())
}
}

impl std::ops::DivAssign for #name {
impl std::ops::DivAssign<Self> for #name {
fn div_assign(&mut self, rhs: Self) {
self.0 /= rhs.value()
}
}

impl std::ops::DivAssign<&Self> for #name {
fn div_assign(&mut self, rhs: &Self) {
self.0 /= rhs.value()
}
}

impl std::ops::Rem<Self> for #name {
type Output = Self;
fn rem(self, rhs: Self) -> Self::Output {
Self::new(self.value() % rhs.value())
}
}

impl std::ops::Rem<&Self> for #name {
type Output = Self;
fn rem(self, rhs: &Self) -> Self::Output {
Self::new(self.value() % rhs.value())
}
}

impl<'a> std::ops::Rem<#name> for &'a #name {
type Output = #name;
fn rem(self, rhs: #name) -> Self::Output {
#name::new(self.value() % rhs.value())
}
}

impl<'a> std::ops::Rem<&#name> for &'a #name {
type Output = #name;
fn rem(self, rhs: &#name) -> Self::Output {
#name::new(self.value() % rhs.value())
}
}

impl std::ops::RemAssign<Self> for #name {
fn rem_assign(&mut self, rhs: Self) {
self.0 %= rhs.value()
}
}

impl std::ops::RemAssign<&Self> for #name {
fn rem_assign(&mut self, rhs: &Self) {
self.0 %= rhs.value()
}
}
}
}
Loading

0 comments on commit 056b160

Please sign in to comment.