diff --git a/src/currency.rs b/src/currency.rs index 4f0a6f9..b163d3d 100644 --- a/src/currency.rs +++ b/src/currency.rs @@ -1,4 +1,4 @@ -use core::ops::{Add, Sub, Mul, Div}; +use core::ops::{Add, Sub, Mul, Div, Deref, DerefMut}; use core::num::ParseIntError; use crate::SiaEncodable; @@ -12,6 +12,28 @@ const SIACOIN_PRECISION_U32: u32 = 24; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct Currency(u128); +// Implement Deref and DerefMut to be able to implicitly use Currency as a u128 +// This gives us all the traits that u128 already implements for free. +impl Deref for Currency { + type Target = u128; + fn deref(&self) -> &u128 { &self.0 } +} + +impl DerefMut for Currency { + fn deref_mut(&mut self) -> &mut u128 { &mut self.0 } +} + +// Implement AsRef as well to be able to implicitly obtain a &u128 from a Currency as well. +impl AsRef for Currency + where + T: ?Sized, + ::Target: AsRef, +{ + fn as_ref(&self) -> &T { + self.deref().as_ref() + } +} + impl Currency { pub fn new(value: u128) -> Self { Currency(value)