-
Notifications
You must be signed in to change notification settings - Fork 13k
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
[�CP] Option
: add by-{ref,mut} variants to the most pervasive adapters
#98536
[�CP] Option
: add by-{ref,mut} variants to the most pervasive adapters
#98536
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
(rust-highfive has picked a reviewer for you, use r? to override) |
I don't think we should have more wrappers around the panicking No objections to the |
I agree with @joshtriplett; I think we already have more adapters on Option/Result than is probably really necessary, and I'd rather not add new ones to be fully generic over ref/ref-mut. It's possible that with rust-lang/lang-team#162 we might be able to do so at no name/discovery cost, if that initiative materializes, but for now I think they're pretty unlikely to pull their weight. (And even with that initiative I think it's an unclear win). r? @joshtriplett since I'd personally not r+ this but T-libs-api is the team to make a decision here |
Since the existing methods already are orthogonal and composable (via
Adding more API surface to handle a transient learning hurdle seems not great to me, especially since it creates its own hurdle (making Option a bigger thing to learn). Imo that's better solved through better documentation or teaching materials? Maybe a cheat-sheet/dense table? "I havea an X, I want to do Y and get a Z" -> use Currently rustdoc is bad at providing this kind of mechanical overview. In the expanded view you get the whole doc comment. In the collapsed view you get the signature (with where clauses, which can be a lot to take in) but zero explanation. The left-aligned formatting also makes it more difficult to scan for return values. I think javadoc method summary tables would provide more utility here. They allow quickly scanning for signatures that have the right shape and provide the headline summary of the method without all the details. |
It looks like I may have misjudged the pervasiveness of these patterns, and/or their convenience: they may indeed not carry enough weight after all. |
Add by-ref & by-mut variants to the most pervasive adapters of
Option
borrowed_struct.field.as_ref().unwrap()
(8382 instances of single-line.as_ref().unwrap()
on grep.app as of this writing);.as_{ref,mut}()
adapters beforehand).…{∅,_ref}
/…_mut
naming convention for such methods;API Overview
#[unstable(feature = "option_borrowing_adapters", …)]
Questions
Should we also offer some of these for
Result
?Option
s is very typical due to struct fields having them, whereasResult
is almost exclusively used by-value / in an owned fashion. Thence left for a potential follow-up PR.would there be other
Option
methods that could benefit from_{ref,mut}
flavors (e.g.,.unwrap_or{,_else}()
)?bikeshedding names etc.
nightly
experimentation. It may not be stabilized or even be removed from future nightlies should the experimentation yield mixed results).Alternatives
.map()
(and so on) in-prelude extension trait for&Option
and&mut Option
, so thatborrowed_opt.map(…)
(and so on) would Just Work™ (but then for a struct field we'd need some postfix borrowing thing, such asborrowed_struct.field&.map(…)
or what not… which is way beyond the scope of this API suggestion);@rustbot modify labels: +T-libs-api -T-libs