Crate structinator_traits
source ·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-definedstruct
s, and this structure was designed for convenience, allowing the implementor to store both aString
ification of the field’s name, and the field’s value. Note that it is also the type that the argument passed tocreate_struct
must iterate over.
Traits
- Any type implementing this trait must be convertable from an
Iterator
over elements of typeNamedField<I>
, whereI
is the same type assigned toInnerIteratorType
.