4 stable releases

1.2.0 Oct 16, 2022
1.1.1 Oct 21, 2020
1.1.0 Oct 19, 2020
1.0.0 Oct 18, 2020

#2498 in Rust patterns

Download history 903/week @ 2023-11-23 1234/week @ 2023-11-30 1142/week @ 2023-12-07 899/week @ 2023-12-14 736/week @ 2023-12-21 1122/week @ 2023-12-28 1100/week @ 2024-01-04 1194/week @ 2024-01-11 935/week @ 2024-01-18 1540/week @ 2024-01-25 1267/week @ 2024-02-01 1466/week @ 2024-02-08 1232/week @ 2024-02-15 941/week @ 2024-02-22 1418/week @ 2024-02-29 891/week @ 2024-03-07

4,675 downloads per month
Used in 7 crates (via fixed-macro)

MIT/Apache

105KB
1K SLoC

fixed-macro

Build Latest Version Documentation Apache 2.0 MIT

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.

[dependencies]
fixed-macro = "1.1"

Compiler support: rustc 1.61+.

Details

  • The syntax of the macro is as follows:

    fixed!(<value>: <type>)
    

    where <value> is an integer literal or a float literal, and <type> is either of the form I<i>F<f> or U<i>F<f>, matching one of the type aliases provided in fixed::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_macro::fixed;
    use fixed::types::U8F8;
    
    let x1 = fixed!(-1.23: I32F32);         // float literal (note, the type is not in scope)
    const X2: U8F8 = fixed!(1.2: U8F8);     // can be used to initialize const values
    let x3 = fixed!(123: U8F8);             // decimal integers work as well
    let x4 = fixed!(0x7B: U8F8);            // and hex/oct/bin integers too
    let x5 = fixed!(1_234.567_890: I32F32); // underscores are ignored, same as in rustc
    let x7 = fixed!(0.12e+01: U8F8);        // scientific notation is also supported
    
  • For each type alias from fixed::types, there is a macro with a matching name in fixed_macro::types which you can use without specifying the type name:

    use fixed_macro::types::I16F48;
    
    let a1 = I16F48!(-1.23);
    

    Both the macro and the type can happily coexist in the same scope:

    use fixed::types::I16F48;
    use fixed_macro::types::I16F48;
    
    const B1: I16F48 = I16F48!(1.23e-2);
    

    You can choose to import both under different (or same) user-defined names:

    use fixed::types::{I16F48 as Decimal};
    use fixed_macro::types::{I16F48 as dec};
    
    let c1 = dec!(12_345);
    const C2: Decimal = dec!(-0.123_456);
    

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

lib.rs:

Macros allowing to create constants for each available fixed-point type.

Dependencies

~3.5MB
~77K SLoC