#fixed #numeric

fixed-macro

fixed包中的类型创建定点常数的宏

4个稳定版本

1.2.0 2022年10月16日
1.1.1 2020年10月21日
1.1.0 2020年10月19日
1.0.0 2020年10月18日

#348 in 数据结构

Download history 1614/week @ 2024-03-14 1748/week @ 2024-03-21 1635/week @ 2024-03-28 1868/week @ 2024-04-04 1166/week @ 2024-04-11 1288/week @ 2024-04-18 1233/week @ 2024-04-25 2129/week @ 2024-05-02 1356/week @ 2024-05-09 1247/week @ 2024-05-16 1027/week @ 2024-05-23 1193/week @ 2024-05-30 1220/week @ 2024-06-06 974/week @ 2024-06-13 1169/week @ 2024-06-20 895/week @ 2024-06-27

4,420 每月下载量
用于 5 crate

MIT/Apache

115KB
1K SLoC

fixed-macro

Build Latest Version Documentation Apache 2.0 MIT

此库提供了 fixed!,这是一个proc-macro,允许轻松地为fixed包中提供的所有定点类型创建定点常量。

[dependencies]
fixed-macro = "1.1"

编译器支持:rustc 1.61+。

详情

  • 宏的语法如下

    fixed!(<value>: <type>)
    

    其中<value>是一个整型字面量或浮点字面量,而<type>是以下形式之一:I<i>F<f>U<i>F<f>,与fixed::types中提供的一个类型别名匹配。特别注意的是,<value>必须是一个字面量,而不是任意算术表达式,并且<type>被视为一个特殊标识符,因此不需要先导入。

  • 创建在编译时解析的定点常量(支持与Rust本身相同的整型和浮点字面量语法,包括下划线和科学记数法)

    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
    
  • 对于从fixed::types中的每个类型别名,在fixed_macro::types中都有一个同名宏,您可以使用它而无需指定类型名称

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

    宏和类型可以在同一作用域中愉快地共存

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

    您可以选择在不同的(或相同的)用户定义名称下导入它们

    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);
    

许可证

您可以选择在Apache许可证,版本2.0MIT许可证下进行许可。
除非您明确表示,否则根据Apache-2.0许可证定义,您有意提交给此软件包的任何贡献都应如上所述双重许可,没有任何额外的条款或条件。

依赖关系

~4MB
~81K SLoC