small_iter_fields

Crates.iosmall_iter_fields
lib.rssmall_iter_fields
version0.1.5
sourcesrc
created_at2024-08-10 20:42:31.063764
updated_at2024-09-25 13:16:55.143193
descriptionget an iterator, or get the length, or convert to a hashmap from an enum
homepage
repositoryhttps://github.com/Ike-l/iter_fields
max_upload_size
id1332697
size6,892
(Ike-l)

documentation

README

small_iter_fields

This crate adds #[derive(IterFields)], #[derive(LenFields)] and #[derive(HashFields)].

It works for:

  • Enums

Known Limitations

  • Enum variants must have no data

Example

use std::collections::HashMap;
use small_iter_fields::{IterFields, LenFields, HashFields};
 
#[derive(IterFields, LenFields, HashFields, Hash, PartialEq, Eq)]
enum Stage {
  Start,
  Middle,
  End,
}

let mut vec: Vec<Stage> = Vec::with_capacity(Stage::len());
assert!(vec.capacity() >= 3);

for stage in Stage::iter_fields() {
    vec.push(stage);
};

assert!(vec.contains(&Stage::Start));
assert!(vec.contains(&Stage::Middle));
assert!(vec.contains(&Stage::End));

let map: HashMap<Stage, Vec<i32>> = Stage::to_hashmap(Vec::new());
assert!(map.capacity() >= 3);

assert_eq!(map.get(&Stage::Start), Some(&Vec::new()));

License

MIT or Apache-2.0

Commit count: 10

cargo fmt