fixed-macro
This library provides fixed!
, a proc-macro that allows
easily creating fixed-point constants for all of the fixed-point types provided in
fixed
crate.
[]
= "1.1"
Compiler support: rustc 1.61+.
Details
-
The syntax of the macro is as follows:
fixed!
where
<value>
is an integer literal or a float literal, and<type>
is either of the formI<i>F<f>
orU<i>F<f>
, matching one of the type aliases provided infixed::types
. Note in particular that<value>
has to be a literal and not an arbitrary arithmetic expression, and that<type>
is considered a special identifier, so that it doesn't have to be imported first. -
Create a fixed-point constant which is parsed at compile time (the same syntax for int and float literals is supported as in Rust itself, including underscores and scientific notation):
use fixed; use U8F8; let x1 = fixed!; // float literal (note, the type is not in scope) const X2: U8F8 = fixed!; // can be used to initialize const values let x3 = fixed!; // decimal integers work as well let x4 = fixed!; // and hex/oct/bin integers too let x5 = fixed!; // underscores are ignored, same as in rustc let x7 = fixed!; // scientific notation is also supported
-
For each type alias from
fixed::types
, there is a macro with a matching name infixed_macro::types
which you can use without specifying the type name:use I16F48; let a1 = I16F48!;
Both the macro and the type can happily coexist in the same scope:
use I16F48; use I16F48; const B1: I16F48 = I16F48!;
You can choose to import both under different (or same) user-defined names:
use ; use ; let c1 = dec!; const C2: Decimal = dec!;