#collection #hash-map #newtype #vec #derive

macro thisisplural

#[derive(Plural)] for creating frictionless new types with any collection type like Vec or HashMap

6 releases (3 breaking)

0.4.0 Apr 3, 2024
0.3.0 May 11, 2022
0.2.0 Sep 12, 2021
0.1.2 Sep 11, 2021

#1530 in Rust patterns

Download history 9/week @ 2024-02-26 5/week @ 2024-03-04 16/week @ 2024-03-11 7/week @ 2024-03-18 194/week @ 2024-04-01 9/week @ 2024-04-08 7/week @ 2024-04-15 30/week @ 2024-04-22 7/week @ 2024-04-29 3/week @ 2024-05-06 13/week @ 2024-05-13 16/week @ 2024-05-20 39/week @ 2024-05-27 28/week @ 2024-06-03 15/week @ 2024-06-10

99 downloads per month

MIT/Apache

13KB
270 lines

thisisplural

GitHub MIT/Apache 2.0 Crates.io docs.rs

#[derive(Plural)] for creating frictionless new types with any collection type.

Features

  • Automatically implements From, Into, FromIterator, IntoIterator, and methods like .len() or ::with_capacity.
  • Supports any collection that behave like Vec and HashMap.

Example

// This implements `From`, `Into`, `FromIterator`, `IntoIterator`.
#[derive(Plural)]
struct Numbers(Vec<u32>);

// use `From` trait
let my_favorite_numbers: Numbers = vec![].into();

// methods like `len()` are implemented
assert_eq!(my_favorite_numbers.len(), 0);
assert!(my_favorite_numbers.is_empty());

// `FromIterator` allows this `collect()`
let doubled_numbers: Numbers = my_favorite_numbers.iter().map(|x| x * 2).collect();

// `HashMap` like collections are 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

~295–750KB
~18K SLoC