1 unstable release

0.0.1 Dec 30, 2020

#280 in #variant

MIT license

5KB

fnum

Fnum is an utility trait for enum, that provides size_of_variant method. size_of_variant returns the minimum size that specified variant requiring.

Usage

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{..}`)
}

Author

Copyright (c) 2020 carrotflakes (carrotflakes@gmail.com)

License

Licensed under the MIT License.


lib.rs:

Fnum is an utility trait for enum, that provides Fnum::size_of_variant method. Fnum::size_of_variant returns the minimum size that specified variant requiring.

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{..}`

Dependencies

~1.5MB
~38K SLoC