Giter Club home page Giter Club logo

fixed-macro's Introduction

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.

fixed-macro's People

Contributors

aldanor avatar kvinwang avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

fixed-macro's Issues

rustc requirement is 1.45 not 1.44

error[E0658]: procedural macros cannot be expanded to statements
  --> /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/fixed-macro-impl-1.0.0/src/dispatch.rs:8:9
   |
8  | /         paste! {
9  | |             if ($int_bits, $frac_bits, $signed) == ($i, $f, true) {
10 | |                 return fixed::types::[<I $i F $f>]::from_str($s)
11 | |                     .map(|x| x.to_bits()).map(Literal::[<i $w _unsuffixed>])
...  |
15 | |             }
16 | |         }
   | |_________^
...
27 |       fixed_to_literal!(int_bits, frac_bits, signed, s, 8, 8, 0);
   |       ----------------------------------------------------------- in this macro invocation
   |
   = note: see issue #54727 <https://github.com/rust-lang/rust/issues/54727> for more information
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Cannot be used under `no_std`

When no_std, the crate will still enable std, causing duplicate lang item in crate ... compiling error, especially when used in a substrate wasm runtime (no_std).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.