lloydmeta / frunk

Funktional generic type-level programming in Rust: HList, Coproduct, Generic, LabelledGeneric, Validated, Monoid and friends.
https://beachape.com/frunk/
MIT License
1.29k stars 58 forks source link

Allow transmogrifying through Vec #142

Closed bossmc closed 5 years ago

bossmc commented 5 years ago

This is more a proof of concept than a fully fledged PR, but I wanted to get feedback early before wandering down a garden path of pain.

This PR specifically allows conversions of the form:

#[derive(Debug, frunk_derives::LabelledGeneric)]
struct Foo {
    bar: Vec<Bar>,
}

#[derive(Debug, frunk_derives::LabelledGeneric)]
struct Foo2 {
    bar: Vec<Bar2>,
}

#[derive(Debug, frunk_derives::LabelledGeneric)]
struct Bar {
    name: String,
}

#[derive(Debug, frunk_derives::LabelledGeneric)]
struct Bar2 {
    name: String
}

fn main() {
    let foo = Foo {
        bar: vec![
            Bar { "Sarah".to_string() },
        ]}
    };
    let foo2: Foo2 = foo.transmogrify();
    println!("{:?}", foo2);
}

Limitations:

Does this look useful/sensible? Have I missed anything in the implementation?

ExpHP commented 5 years ago

Sounds reasonable to me. Please add a test!

ExpHP commented 5 years ago

Re: FromIterator, for things like this I fear it becomes too easy to overlap with other useful impls. I also feel like Vec -> HashMap doesn't make sense. (if there was a HashMap -> HashMap, I'd expect it to only operate on values, since operating on keys could create duplicates)

bossmc commented 5 years ago

Added a test, also moved the marker index to indices for consistency.

I've not done anything clever (e.g. IntoIterator/FromIterator) or added support for other container types, though most would be pretty trivial.

bossmc commented 5 years ago

Can/should this be part of https://github.com/lloydmeta/frunk/milestone/1?

lloydmeta commented 5 years ago

@ExpHP @Centril if you'd like to review this before merging, just shout :), else I'm thinking we merge this sometime this week and push out a release this weekend :)

lloydmeta commented 5 years ago

Released as 0.2.4, thanks again 😄