Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to use a different wrapper than Option #9

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

roignpar
Copy link
Owner

@roignpar roignpar commented Mar 28, 2024

Adds the wrapper argument to optfield that allows using a different wrapper than Option around the generated fields.
The custom wrapper type must have a signature taking exactly one generic type: MyWrapper<T>. The custom wrapper can be a struct, tuple, or enum as long as the signature is respected.

merge_fn will have a slightly different behavior when wrapper is used: a From<MyWrapper<T>> for Option<T> implementation must exist, either generically or specifically for all the field types used in the target struct.
In case the target struct already has MyWrapper fields and rewrap is not used a From<&MyWrapper<T>> for Option<()> is also required in order to be able to determine whether the valued should be merged or not. This will be explained in detail in the documentation.
If merge_fn is not used then these implementations are not necessary.

Similarly, when from is used, a From<T> for MyWrapper<T> implementation must exist, either generically or specifically for all the field types of the target struct.
If from is not used then these implementations are not necessary.

When used together with wrapper rewrap will target the given wrapper type instead of Option.

A simple example:

struct Wrapper<T>(T);

#[optfield(Opt, wrapper = Wrapper)]
struct MyStruct {
    field: u32,
    wrapped: Wrapper<String>,
};

// will generate:
struct Opt {
    field: Wrapper<u32>,
    wrapped: Wrapper<u32>,
};

More examples in tests/wrapper.rs and docs.

Closes #8

@roignpar
Copy link
Owner Author

roignpar commented Apr 1, 2024

Implementation is done.
@m93a could you please try it out and see if it works as expected?
considering the example from #8 it would be something along these lines:

#[optfield(TraceStyle, wrapper = OrUnset,
  attrs = add(
    optfield(TraceStylePatch, attrs, ...)
  )
]
pub struct ComputedTraceStyle {
  // ...
}

@roignpar roignpar marked this pull request as ready for review April 5, 2024 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support wrapping with a different type
1 participant