separable/lib.rs
1//! # Trait to separate an iterator of Enums
2//!
3//! A trait that helps split up an vector of enums into a tuple with a vector per variant. It was kinda inspired by the behaviour of `Result<Vec<_>, _>`, when used with `collect`. Please let me know if such a functionality can be achieved in Rust without the macro.
4//!
5//! With the derive macro, implementations for `Self`, `&Self`, `&mut Self` are produced.
6//!
7//! ```rust
8//! use separable::Separable;
9//!
10//! #[derive(Separable)]
11//! enum Temperature {
12//! Celsius(f64),
13//! Fahrenheit(f64),
14//! Kelvin(f64)
15//! }
16//!
17//! fn main() {
18//! // A bunch of measurements...
19//! let measurements = vec![
20//! Temperature::Celsius(23.0),
21//! Temperature::Fahrenheit(2.0),
22//! Temperature::Celsius(22.5),
23//! Temperature::Kelvin(288.0),
24//! Temperature::Celsius(23.1),
25//! Temperature::Fahrenheit(5.0)
26//! ];
27//!
28//! // We separate the values of each variant, in order
29//! let (celsius, fahrenheit, kelvin) = measurements.into_iter().collect();
30//!
31//! // Quick verification
32//! assert_eq!(celsius, vec![23.0f64, 22.5f64, 23.1f64]);
33//! assert_eq!(fahrenheit, vec![2.0f64, 5.0f64]);
34//! assert_eq!(kelvin, vec![288.0f64]);
35//! }
36//! ```
37
38// Derive macros
39pub use separable_derive::*;
40
41/// Main and only trait
42///
43/// Exists basically to enforce the `FromIterator` trait on the enum
44pub trait Separable: Sized {
45 type Target: FromIterator<Self>;
46}
47
48// impl <E, A: Fromiterator<&E> + Separable<E>> Separable<E> for &A {}