Skip to content

Conversation

@trolleyman
Copy link
Contributor

@trolleyman trolleyman commented Apr 28, 2016

Fixes #7.

A new crate is located in the decimal-macros folder that provides the constant macro. It uses a plugin to compile the d128 literals.

@alkis
Copy link
Owner

alkis commented May 1, 2016

Thanks for the contribution @trolleyman. Give me a week to carve some time to take a closer look.

}
};

match &ex.node {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this ampersand?

@alkis
Copy link
Owner

alkis commented May 9, 2016

Thanks for the contribution! In addition to the comments above: don't we need some tests?

extern crate decimal;

fn main() {
let a = d128!(0.1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about better tests?

d128!(0.1) should be equal to 1 as dec128 / 10 as dec128, etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I should - was just kinda rushing to get it out the door and working :p I had so much trouble with it

Copy link
Contributor Author

@trolleyman trolleyman May 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose this code because it fails in f32 land: http://0.30000000000000004.com/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fail?

d128!(0.1) == (1 as dec128) / (10 as dec128)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test with this

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is different. Let me explain. What you are testing here is that the result of d128!(0.1) is equal to the result of d128!(1) / d128!(10). This might pass even if d128!(10) does not equal a dec128 of decimal value 10. This is why I suggest that the left hand side uses the macro and the right hand side uses conversion from int to dec128 which are trivial.

Also is this test organization going to work with cargo test? If not can you put these tests inside decimal-macros/src/lib.rs inside a test module and annotate appropriately so that it works?

@trolleyman
Copy link
Contributor Author

Added some commits that polish up the PR a bit


fn d128_lit<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult + 'cx> {
// Turn input into a string literal
// e.g. d128!(0.1) -> "0.1", d128!(NaN) -> "NaN"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this comment. It makes it easier for the syntex noob (me) out there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha so was I before this - this and this really helped. Also the docs.

let ids = vec![cx.ident_of("decimal"), cx.ident_of("d128"), cx.ident_of("from_bytes")];
let ex = cx.expr_call_global(lit.span, ids, vec![vec]);

let ex = cx.expr_call_global(lit.span, ids, vec![vec]); // Call ::decimal::d128::from_bytes with array literal
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is unnecessary (the expr_call_global is pretty obvious).

@trolleyman
Copy link
Contributor Author

Ok, updated

#[doc(hidden)]
pub fn from_bytes(bytes: [u8; 16]) -> d128 {
d128 {
bytes: unsafe { ::std::mem::transmute(bytes) }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work in Big Endian machines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will, as it's just calling the same code that from_str calls.

@alkis
Copy link
Owner

alkis commented Jan 21, 2017

It looks like with the new Macros 1.1 this should be even easier. Do you want to take a stab at it?

tari added a commit to tari/decimal that referenced this pull request Jul 13, 2022
This makes macro invocations free at runtime, whereas the old macro
delegated to from_str at runtime which has small but nonzero cost.

The concept is based on alkis#16
but completely rewritten with the newer (stable) procedural macros which
implies a requirement for rustc 1.31.0 (2018 edition). The
implementation uses the 2021 edition (rustc 1.56.0) for convenience.
tari added a commit to tari/decimal that referenced this pull request Jul 13, 2022
This makes macro invocations free at runtime, whereas the old macro
delegated to from_str at runtime which has small but nonzero cost.

The concept is based on alkis#16
but completely rewritten with the newer (stable) procedural macros which
implies a requirement for rustc 1.31.0 (2018 edition). The
implementation uses the 2021 edition (rustc 1.56.0) for convenience.
This was referenced Jul 13, 2022
@trolleyman
Copy link
Contributor Author

Closing due to #72

@trolleyman trolleyman closed this Jan 17, 2023
tari added a commit to tari/decimal that referenced this pull request Mar 28, 2023
This makes macro invocations free at runtime, whereas the old macro
delegated to from_str at runtime which has small but nonzero cost.

The old decimal::d128 macro is changed to be internal-only so the new
one doesn't have a conflicting name: the type and macro share a name so
it's not possible to import only one of them from a given crate.

The concept is based on alkis#16
but completely rewritten with the newer (stable) procedural macros which
implies a requirement for rustc 1.31.0 (2018 edition). The
implementation uses the 2021 edition (rustc 1.56.0) for convenience.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

d128 constants

2 participants