separable 0.2.1

Separable trait for enums
Documentation
  • Coverage
  • 50%
    2 out of 4 items documented1 out of 2 items with examples
  • Size
  • Source code size: 4.93 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 258.54 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 15s Average build duration of successful builds.
  • all releases: 15s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Malanche/separable
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Malanche

Trait to separate an iterator of Enums

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.

With the derive macro, implementations for Self, &Self, &mut Self are produced.

use separable::Separable;

#[derive(Separable)]
enum Temperature {
    Celsius(f64),
    Fahrenheit(f64),
    Kelvin(f64)
}

fn main() {
    // A bunch of measurements...
    let measurements = vec![
        Temperature::Celsius(23.0),
        Temperature::Fahrenheit(2.0),
        Temperature::Celsius(22.5),
        Temperature::Kelvin(288.0),
        Temperature::Celsius(23.1),
        Temperature::Fahrenheit(5.0)
    ];
    
    // We separate the values of each variant, in order
    let (celsius, fahrenheit, kelvin) = measurements.into_iter().collect();
    
    // Quick verification
    assert_eq!(celsius, vec![23.0f64, 22.5f64, 23.1f64]);
    assert_eq!(fahrenheit, vec![2.0f64, 5.0f64]);
    assert_eq!(kelvin, vec![288.0f64]);
}