vectorize_struct

Crates.iovectorize_struct
lib.rsvectorize_struct
version0.1.3
sourcesrc
created_at2018-09-24 23:17:38.468452
updated_at2018-09-25 07:39:44.463035
descriptionvectorize_struct adds an procedural macro attribute that makes it possible to iterate over Trait Objects of every field of a Struct that implements a specific trait.
homepage
repositoryhttps://gitlab.com/XBagon/vectorize_struct/tree/attribute
max_upload_size
id86387
size18,900
XBagon (XBagon)

documentation

README

vectorize_struct adds an procedural macro attribute that makes it possible to iterate over Trait Objects of every field of a Struct that implements a specific trait.

It only works on nightly and requires #![feature(specialization)].

Example

#![feature(specialization)]

extern crate vectorize_struct;
use vectorize_struct::vectorize_struct;

use std::fmt::{Debug, Display};

#[vectorize_struct(std::fmt::Display, Debug)]
struct Struct {
    i: i32,
    u: u32,
    b: bool,
    s: String,
    buui: (bool, u32, u8, i16),
    s2: String,
    nd: NoDisplay,
}

struct NoDisplay {}

fn main() {
    use vectorized_Struct::*;

    let s = Struct {
        i: -12,
        u: 61,
        b: false,
        s: String::from("asd"),
        buui: (true, 1, 5, -2),
        s2: String::from("fgh"),
        nd: NoDisplay {},
    };

    for (name, field) in s.vectorize() as Vec<(Fields, Option<&Display>)> {
        if let Some(field) = field {
            println!("{:?} can display: {}", name, field);
        }
    }

    for (name, field) in s.vectorize() as Vec<(Fields, Option<&Debug>)> {
        if let Some(field) = field {
            println!("{:?} can debug: {:?}", name, field);
        }
    }
}
Commit count: 0

cargo fmt