1个不稳定版本

0.0.1 2020年12月30日

#291 in #变体

MIT许可

5KB

fnum

Fnum是枚举的实用特质,提供了size_of_variant方法。size_of_variant返回指定变体所需的最小大小。

用法

use fnum::Fnum;

#[derive(Fnum)]
enum MyEnum {
    A(u64),
    B(String),
    C(u64, u32, u32, u32),
    D {
        foo: u32,
        bar: String,
    }
}

fn main() {
    dbg!(MyEnum::size_of_variant(0)); // => 16 (bytes required by `MyEnum::A(..)`)
    dbg!(MyEnum::size_of_variant(1)); // => 32 (bytes required by `MyEnum::B(..)`)
    dbg!(MyEnum::size_of_variant(2)); // => 24 (bytes required by `MyEnum::C(..)`)
    dbg!(MyEnum::size_of_variant(3)); // => 32 (bytes required by `MyEnum::D{..}`)
}

作者

版权(c)2020 carrotflakes ([email protected])

许可

在MIT许可下发布。


lib.rs:

Fnum是枚举的实用特质,提供了Fnum::size_of_variant方法。Fnum::size_of_variant返回指定变体所需的最小大小。

use fnum::Fnum;

#[derive(Fnum)]
enum MyEnum {
    A(u64),
    B(String),
    C(u64, u32, u32, u32),
    D {
        foo: u32,
        bar: String,
    }
}

assert_eq!(MyEnum::size_of_variant(0), 16); // bytes required by `MyEnum::A(..)`
assert_eq!(MyEnum::size_of_variant(1), 32); // bytes required by `MyEnum::B(..)`
assert_eq!(MyEnum::size_of_variant(2), 24); // bytes required by `MyEnum::C(..)`
assert_eq!(MyEnum::size_of_variant(3), 32); // bytes required by `MyEnum::D{..}`

依赖项

~1.5MB
~36K SLoC