Skip to content

feat: Implement opt-in as_ref/as_mut support in Getters/MutGetters macros #120

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

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

Conversation

ry-sun
Copy link

@ry-sun ry-sun commented Jun 16, 2025

Implementation of #119

in this PR:

  1. we add opt new attributes as_ref/'as_mut to make the Option/Result can return Option<&T>/Result<&T, &E> instead of &Option<T>/&Result<T, E>.
  2. We add new test cases for new features
  3. Modified docs and readme.

Maintainer edit:
I believe this will also close #78

@jbaublitz
Copy link
Owner

Thanks for your contribution! For now, take a look at the CI errors. I will do a more in depth review when I get the chance.

@ry-sun
Copy link
Author

ry-sun commented Jun 19, 2025

@jbaublitz Thanks for your reply. I've fixed the lint errors and rebased to the latest HEAD branch.

Looking forward to your review!

Copy link
Owner

@jbaublitz jbaublitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm currently making my way through the PR and so far it looks reasonable. I need to familiarize myself a bit more with the string arguments to Getters and Setters only because I feel like as I've been approving and merging PRs, there's been a bit of a split between what gets implemented as an extension of get vs. what gets implemented as its own derive macro such as CloneGetters and CopyGetters. It may be worth putting up an issue to gather feedback about this and take a more disciplined approach to these sorts of issues in v0.2.0.

Copy link
Owner

@jbaublitz jbaublitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few code level comments that I'm happy to discuss with you. In particular the visibility parsing currently works and I don't want to hold up this PR when this might be better addressed by #124.

As for higher level feedback, I'm concerned that the only types supported are Result and Option largely because the pattern:

type MyResult<O> = Result<O, MyError>

This PR would not be able to generate any as_ref/as_mut getters in this case. I'm okay with supporting this just for Option, Result, and type aliases for these two types as the docs seem to indicate that these types' as_ref implementations are separate from the AsRef trait. Focusing on just covering all of those cases for now is fine with me.

Comment on lines +99 to +102
let value_str = parse_named_value(attr, meta_name)?;
let vis_str = value_str.split(' ').find(|v| v.starts_with("pub"))?;

Some(parse_vis_str(vis_str, attr.span()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the potential problem I was worried about with this approach. I think in terms of an API, it looks very good and I prefer this approach ergonomically but I'm worried about being able to reliably parse things moving forward if other visibility modifiers are added in. It seems a bit fragile for future compatibility.

};
value_str.split(' ').any(|v| v == "as_ref")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're splitting the string twice for each of these invocations. I think it would make sense to split is above line 276 and pass the split string as an argument to any parser that needs it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide ability to create getters that return A<&T> instead of &A<T>
2 participants