Skip to content

Commit

Permalink
implemented neg for pgmoney
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Andreba committed Feb 19, 2023
1 parent 6d0d740 commit ffa0d72
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion sqlx-core/src/postgres/types/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use byteorder::{BigEndian, ByteOrder};
use std::{
io,
ops::{Add, AddAssign, Sub, SubAssign},
ops::{Add, AddAssign, Sub, SubAssign, Neg},
};

/// The PostgreSQL [`MONEY`] type stores a currency amount with a fixed fractional
Expand Down Expand Up @@ -248,6 +248,19 @@ impl SubAssign<PgMoney> for PgMoney {
}
}

impl Neg for PgMoney {
type Output = Self;

/// Negates the monetary value.
///
/// # Panics
/// Panics if the value is `i64::MIN`.
#[inline]
fn neg(self) -> Self::Output {
Self(-self.0)
}
}

#[cfg(test)]
mod tests {
use super::PgMoney;
Expand Down Expand Up @@ -304,6 +317,11 @@ mod tests {
money -= PgMoney(1);
}

#[test]
fn negating_works() {
assert_eq!(PgMoney(-3), -PgMoney(3))
}

#[test]
#[cfg(feature = "bigdecimal")]
fn conversion_to_bigdecimal_works() {
Expand Down

0 comments on commit ffa0d72

Please sign in to comment.