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

Could Default be implemented for Field? #234

Open misos1 opened 5 months ago

misos1 commented 5 months ago

So it would be possible to use lift_from and lift_into also with labelled generic.

lloydmeta commented 5 months ago

I'm not sure I understand how this could be implemented in a way that could be useful in the LabelledGeneric context.

Can you please either elaborate or send a PR with a test showing your proposal in action?

misos1 commented 5 months ago

I had in mind something like reverse transform_from where subset can be transformed into superset while filling the rest from default, something like this:

#[derive(LabelledGeneric, Default, Debug)]
struct A {d: i8, b: String, c: f32, a: i32}
#[derive(LabelledGeneric, Default, Debug)]
struct B {a: i32, b: String}
let (_, remainder): (<B as LabelledGeneric>::Repr, _) = into_labelled_generic(A::default()).sculpt();
let a: A = from_labelled_generic(into_labelled_generic(B {a: 1, b: "2".into()}).extend(remainder).sculpt().0);

Maybe transform_from and transmogrify could support that or there could be some other more "general" versions of these functions for that. And maybe there could be something which can flatten or unflatten nested structs. (Or even transform between structs where one is not a subset of another but they have just some common fields like struct A {a: i32, b: f32} and struct B {c: u8, a: i32}.)

Also maybe if the associated type for IntoLabelledGeneric trait was named little differently than Repr like for example IntoRepr then there would not be this clash and need to specify B as LabelledGeneric here. I also noticed that I can call sculpt on any struct X and it just returns (HNil, X), I wonder if this is functionally needed for something to work? Why is implementation for empty target (impl<Source> Sculptor<HNil, HNil> for Source) not specific about Source to be just HNil and nothing else to avoid such clutter? - Oh I see why it is in such a way, but maybe Source could be at least bound to the HList trait.