Is there any `#![no_std]` crate that evaluates arithmetic expressions involving 1. only arbitrary sized integers and 2. that with arbitrary sized floats

Imagine I am doing an embedded-only physical calculator.

Actually, what I am trying to do here is to write a uconst![12345] that gets promoted (pasted into the source code) to ::typenum::consts::U12345 at compile time.

This part is not so difficult.

The more difficult part is to support #![no_std] evaluator of arithmetic operations on &'source str expressions (e.g. 5 + 67; should return 72 as what your faithful calculator does). Hence, something like this should work:

struct Wrapper<T: typenum::Unsigned>(PhantomData<T>);

let _wrapper = Wrapper::<uconst![ 5 + 67 ]>(PhantomData);

assert_eq!(72_usize, uconst![ 5 + 67 ]::USIZE);

Most bombastic expression-evaluation libraries in Rust are very featureful with std.

I'm not quite up-to-date about your current project. This problem could be done in generic_const_exprs, I think, if that's acceptable to you.

Or maybe op in typenum - Rust can help?

Hi, thank you for your prompt reply.

I have finished writing the part of the proc-macro where it parses a literal integer and returns a typenum::Uint/PInt/NInt depending on the sign of the literal number you had passed into the macro. The macro can also be used in #![no_std].

typenum::assert_type_eq!(tnconst![ 69 ], typenum::consts::U69);

I have noted typenum::op, it uses the type level integers to do operations on them, which is cool, but I am just thinking if there is some #![no_std] arithmetic expressions-evaluation libraries that I can bootstrap on to enable tnconst![+ { 5 + 11 }] to give typenum::consts::P16.

Anyway, a follow-up question is:

I am using syn, quote and proc-macro2 crates, they do not seem to support #![no_std] but I aspire my attribute macro to be #![no_std], is it even possible?

The code for those crates will only run on the host system during compilation, which needs to have std support anyway. As long as your proc macro emits nostd compatible code, the macro should be usable in nostd projects without issue.

Hi @2e71828

Thank you for your prompt reply.

Can I interpret your response as: a proc-macro crate can depend on std even if downstream crates that use the said proc-macro crate use #![no_std] in their lib.rs? (because the compilation of #![no_std] crates will eventually rely on rustc on host machine during codegen phase and would require std anyway)

Another question that I have this:

Can I eagerly invoke any procedural macros that the user of tnconst![...] passed into it?

e.g.

tnconst![ #[cfg(feature = "hey")] 5 #[cfg(feature = "you") 69] ]

gets expanded to

tnconst![5]

iff feature = "hey"?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.