#data-transfer #iterator #struct #traits #conversion #convenience

structinator_traits

Library for traits allowing transfer of data from iterators to structs

2 unstable releases

0.2.0 Jul 11, 2024
0.1.5 Aug 4, 2023

#1922 in Rust patterns


Used in 2 crates (via structinator)

Apache-2.0

6KB

structinator_traits is designed to be used with structinator, a library that automatically implements the SpecifyCreatableStruct trait. However, this library is also useful because it contains the type that the argument passed to create_struct must iterate over, or can be used to mix custom and automatic implementations of SpecifyCreatableStruct. For more information, check the documentation below.


lib.rs:

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);
}

No runtime deps