frunk icon indicating copy to clipboard operation
frunk copied to clipboard

Converting struct with enum members

Open janosimas opened this issue 4 years ago • 1 comments

Hi, I just started using this crate and looks quite promising!

I want to check if there is some limitation with the crate or maybe I'm doing something wrong. Here is what I found in my tests:

  • Enum to enum, works.
  • Struct without enum, works.
  • Struct with enum, do not work.

Is the correct? Is it possible to convert a complex struct with enum fields?

Here is a minimal example. I have a BaseStruct with an BaseEnum field, all of them derive from LabelledGeneric, and I want to convert to a TargetStruct with a TargetEnum field. All of the them have the same names and member order.

extern crate frunk;
extern crate frunk_core;

use frunk::{labelled::Transmogrifier, LabelledGeneric};

fn main() {
    // works
    let target: TargetEnum = frunk::labelled_convert_from(BaseEnum::default());

    // works
    let target: TargetStruct = frunk::labelled_convert_from(BaseStruct::default());
    let target: TargetStruct = frunk::transform_from(BaseStruct::default());
    let target: TargetStruct = BaseStruct::default().transmogrify();
    // Doesn't work
    let target: TargetStruct = frunk::from_labelled_generic(BaseStruct::default());

    // No conversion works for the "complex" struct
    let target: TargetComplexStruct = frunk::labelled_convert_from(BaseComplexStruct::default());
    let target: TargetComplexStruct = frunk::from_labelled_generic(BaseComplexStruct::default());
    let target: TargetComplexStruct = frunk::transform_from(BaseComplexStruct::default());
    let target: TargetComplexStruct = BaseComplexStruct::default().transmogrify();
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
enum BaseEnum {
    SomeValue,
}

impl Default for BaseEnum {
    fn default() -> Self {
        BaseEnum::SomeValue
    }
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
enum TargetEnum {
    SomeValue,
}

#[derive(Default, Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
struct BaseStruct {
    test: String,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
struct TargetStruct {
    test: String,
}

#[derive(Default, Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
struct BaseComplexStruct {
    test: String,
    other: BaseEnum,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
struct TargetComplexStruct {
    test: String,
    other: TargetEnum,
}

janosimas avatar Jul 19 '21 13:07 janosimas

Yes. Looking for the same thing. I tried many approaches but had no luck.

azzamsa avatar Jul 28 '23 19:07 azzamsa