Skip to content
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

Add dec!() macro by example #692

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Version History

# 1.37.0

### Added

* Built in `dec!` macro with extended features. You no longer need external `rust_decimal_macros`.
If you do want to keep it, `use rust_decimal_macros::dec;`, not `use rust_decimal_macros::*;`,
which can now be ambiguous.

# 1.36.0

### Fixed
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ rust_decimal_macros = "1.36"
## Usage

Decimal numbers can be created in a few distinct ways. The easiest and most efficient method of creating a Decimal is to
use the procedural macro that can be enabled using the `macros` feature:
use the macro:

```rust
// Import the `rust_decimal_macros` crate and use the macro directly from there.
use rust_decimal_macros::dec;
use rust_decimal::dec;

let number = dec!(-1.23) + dec!(3.45);
assert_eq!(number, dec!(2.22));
Expand Down Expand Up @@ -93,7 +92,6 @@ Once you have instantiated your `Decimal` number you can perform calculations wi

```rust
use rust_decimal::prelude::*;
use rust_decimal_macros::dec;

let amount = dec!(25.12);
let tax_percentage = dec!(0.085);
Expand Down
3 changes: 1 addition & 2 deletions examples/rkyv-remote.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
extern crate rkyv_0_8 as rkyv;

use rkyv::{rancor::Error, Archive, Deserialize, Serialize};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use rust_decimal::{dec, Decimal};

/// The type containing a [`Decimal`] that will be de/serialized.
#[derive(Archive, Serialize, Deserialize, Debug, PartialEq, Eq)]
Expand Down
1 change: 0 additions & 1 deletion examples/serde-json-scenarios/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rust_decimal::prelude::*;
use rust_decimal_macros::dec;

type ExampleResult = Result<(), Box<dyn std::error::Error>>;

Expand Down
33 changes: 17 additions & 16 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,25 +484,26 @@ impl Decimal {
/// ```
pub const fn try_from_i128_with_scale(num: i128, scale: u32) -> crate::Result<Decimal> {
if scale > Self::MAX_SCALE {
return Err(Error::ScaleExceedsMaximumPrecision(scale));
}
let mut neg = false;
let mut wrapped = num;
if num > MAX_I128_REPR {
return Err(Error::ExceedsMaximumPossibleValue);
Err(Error::ScaleExceedsMaximumPrecision(scale))
} else if num > MAX_I128_REPR {
Err(Error::ExceedsMaximumPossibleValue)
} else if num < -MAX_I128_REPR {
return Err(Error::LessThanMinimumPossibleValue);
} else if num < 0 {
neg = true;
wrapped = -num;
Err(Error::LessThanMinimumPossibleValue)
} else {
Ok(Self::from_i128_with_scale_unchecked(num, scale))
}
let flags: u32 = flags(neg, scale);
Ok(Decimal {
}

#[inline]
pub(crate) const fn from_i128_with_scale_unchecked(num: i128, scale: u32) -> Decimal {
let flags = flags(num < 0, scale);
let num = num.unsigned_abs();
Decimal {
flags,
lo: (wrapped as u64 & U32_MASK) as u32,
mid: ((wrapped as u64 >> 32) & U32_MASK) as u32,
hi: ((wrapped as u128 >> 64) as u64 & U32_MASK) as u32,
})
lo: (num as u64 & U32_MASK) as u32,
mid: ((num as u64 >> 32) & U32_MASK) as u32,
hi: ((num >> 64) as u64 & U32_MASK) as u32,
}
}

/// Returns a `Decimal` using the instances constituent parts.
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod constants;
mod decimal;
mod error;
mod ops;
mod str;
pub mod str;

// We purposely place this here for documentation ordering
mod arithmetic_impls;
Expand Down Expand Up @@ -64,7 +64,7 @@ pub use maths::MathematicalOps;
pub mod prelude {
#[cfg(feature = "maths")]
pub use crate::maths::MathematicalOps;
pub use crate::{Decimal, RoundingStrategy};
pub use crate::{dec, Decimal, RoundingStrategy};
pub use core::str::FromStr;
pub use num_traits::{FromPrimitive, One, Signed, ToPrimitive, Zero};
// #[cfg(feature = "macros")]
Expand Down
Loading