5 unstable releases
0.3.0 | May 11, 2022 |
---|---|
0.2.0 | Sep 12, 2021 |
0.1.2 | Sep 11, 2021 |
0.1.1 | Sep 11, 2021 |
0.1.0 | Sep 11, 2021 |
#2937 in Rust patterns
33 downloads per month
8KB
133 lines
thisisplural
#[derive(Plural)]
for creating frictionless new types with a Vec
, HashMap
, etc.
Features
- Automatically implements
From
,Into
,FromIterator
,IntoIterator
. - Supports
Vec
andHashMap
(adding other collections to here is very easy).
Example
// This implements `From`, `Into`, `FromIterator`, `IntoIterator`, `Deref`, and `DerefMut`.
#[derive(Plural)]
struct Numbers(Vec<u32>);
// use `From` trait
let my_favorite_numbers: Numbers = vec![].into();
// `FromIterator` allows this `collect()`
let doubled_numbers: Numbers = my_favorite_numbers.0.iter().map(|x| x * 2).collect();
// `HashMap` is also supported
#[derive(Plural)]
struct FavoriteNumbers(HashMap<&'static str, Numbers>);
// construct the struct with using `FromIterator`
let favorite_numbers =
FavoriteNumbers::from_iter([("ryo33", my_favorite_numbers), ("someone", doubled_numbers)]);
// use it in a `for` loop (`IntoIterator` trait)
for (name, numbers) in favorite_numbers {
// use Deref trait
println!("{} has {} favorite number(s)", name, numbers.0.len());
}
Dependencies
~1.5MB
~33K SLoC