lesurp / OptionalStruct

Macro copying a struct with Option fields. Useful for config initialization
Apache License 2.0
35 stars 11 forks source link

Difficulty with optional nested struct #23

Closed maxwolff closed 1 month ago

maxwolff commented 1 month ago

I'm trying to get something like the following to work, and having trouble. I want the "inner" field on the parent Foo struct to be Option, but want OptionalFoo to be OptionalInner. I think the rename attribute isnt getting inside the option in my inner field declaration?

This all works fine if i just let Foo {inner:Foo}, but curious if there is a way to get this working too.

use optional_struct::*;

#[optional_struct]
#[derive(PartialEq, Debug)]
struct Foo {
    #[optional_rename(OptionalInner)]
    #[optional_skip_wrap]
    inner: Option<Inner>,
}

#[optional_struct]
#[derive(PartialEq, Debug, Clone)]
struct Inner(i8);

fn main() {
    let optional = OptionalFoo {
        inner: OptionalInner(Some(1))
    };
    println!("{:?}", optional);
}```
error[E0308]: mismatched types --> main_crate/src/main.rs:3:1 3 #[optional_struct] ^^^^^^^^^^^^^^^^^^
expected OptionalInner, found Option<_>
this expression has type (&mut Option<Inner>, OptionalInner)

= note: expected struct OptionalInner found enum Option<_> = note: this error originates in the attribute macro optional_struct (in Nightly builds, run with -Z macro-backtrace for more info)

lesurp commented 1 month ago

Hey, thanks for reporting this. It's definitely a bug, the code you posted should work as expected. I'll be on it