-
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
Borrow
and AsRef
are redundant
#24140
Comments
There are a few really important differences:
This should be better documented, but there's definitely a reason that things are set up this way. As to I'm going to close this issue for now, since the traits are not in fact redundant. But it may be worth opening a separate issue for improved documentation and missing |
For the sake of cross-posting, I just asked this same question in Discourse without knowing this Issue existed. |
Still not properly documented (I think.) Edit: The only explanation I can find is on the |
https://doc.rust-lang.org/book/first-edition/borrow-and-asref.html exists, but it's not going to in the second edition |
@aturon - Thanks for a better explanation -
PS: I could be wrong, but my brain is likely misleading me into thinking I have good understanding after reading the above statement, but I'm not entirely sure if I'm clear on the kind of consequences on implementing one over the other would cause. Would be better if there are side-by-side comparisons of both, in the documentation to express it clearly. |
Ah. Alright, I realize I was wrong - and my above statement is incorrect. Finally, this was one only post that provided the most sensible documentation so far: Thanks to @stjepang for this. PS: All other documentation including the above comment just focuses so much only |
I’m not sure if this is the best place to discuss this, but as part of working on #44868, I’d like to verify the precise guarantees made by In other words: Has |
Thinking about this some more: The most strict guarantee would probably be: ‘If While not stated currently in the documentation, this is perhaps what is intended? |
I'm a bit troubled by @aturon's comment about
I was surprised to hear that types implementing I realize it's not possible to add another trait bound to
|
@mikeyhew If you don’t want to make the trait-related guarantees, you probably should implement I tried to make the distinction more clear in the proposed new text in #46518. |
@partim |
Improve documentation for Borrow This is the first step in improving the documentation for all the reference conversion traits. It proposes new text for the trait documentation of `Borrow`. Since I feel it is a somewhat radical rewrite and includes a stricter contract for `Borrow` then the previous text—namely that *all* shared traits need to behave the same, not just a select few—, I wanted to get some feedback before continuing. Apart from the ‘normative’ description, the new text also includes a fairly extensive explanation of how the trait is used in the examples section. I included it because every time I look at how `HashMap` uses the trait, I need to think for a while as the use is a bit twisted. So, I thought having this thinking written down as part of the trait itself might be useful. One could argue that this should go into The Book, and, while I really like having everything important in the docs, I can see the text moved there, too. So, before I move on: is this new text any good? Do we feel it is correct, useful, comprehensive, and understandable? (This PR is in response to rust-lang#44868 and rust-lang#24140.)
Triage: Now that #46518 has landed, can this be closed or at least de-prioritized? |
Yes, closing in favor of that. If there are still improvements to be made, please open new issues! |
These traits have exactly the same signature, both are stable and both should be implemented by the same types. One of them should go.
Furthermore
AsRef
is not implemented byBox
etc…The text was updated successfully, but these errors were encountered: