Expand description

Struct-inator Traits

This is a library for implementing a trait, SpecifyCreatableStruct - which, when implemented, alliows for quick and easy conversion between rust Iterators and types that implement SpecifyCreatableStruct.

More specifically, any type implementing SpecifyCreatableStruct must implement create_struct, a function converting directly from an Iterator over items of type NamedField<I>, where I is the same type as InnerIteratorType

You can implement this trait yourself, or you can automatically implement it for a user-defined struct using structinator

Example

use structinator_traits::*;
use structinator::iter_convertable;
use enum_unwrapper::unique_try_froms;
#[unique_try_froms()]
enum WaffleInfo {
    Topping(u8),
    Layers(u16),
}
#[iter_convertable(WaffleInfo)]
struct Waffles {
    syrup_amount: u8,
    butter_amount: u8,
    layer_count: u16,
}
fn main() {
    let mut iterator = [NamedField::<WaffleInfo> {
        name: String::from("butter_amount"),
       wrapped_value: WaffleInfo::Topping(44),
    }, NamedField::<WaffleInfo> {
        name: String::from("layer_count"),
        wrapped_value: WaffleInfo::Layers(444),
    }, NamedField::<WaffleInfo> {
        name: String::from("syrup_amount"),
        wrapped_value: WaffleInfo::Topping(4),
    }].into_iter();
    let generated_struct = Waffles::create_struct(&mut iterator).unwrap();
    assert_eq!(4,generated_struct.syrup_amount);
    assert_eq!(44,generated_struct.butter_amount);
    assert_eq!(444,generated_struct.layer_count);
}

Structs

  • SpecifyCreatableStruct’s original intended use case was with user-defined structs, and this structure was designed for convenience, allowing the implementor to store both a Stringification of the field’s name, and the field’s value. Note that it is also the type that the argument passed to create_struct must iterate over.

Traits