-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Make the documentation of From and Into traits coherent. #59163
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
Comments
I'll respond more thoroughly to this later, but
This is a good thing, not a bad thing! The docs should be legible to those who are not experts. In many ways, I prefer feedback from someone like you than to someone who's been writing Rust for years! |
There is a good blog provide relative knowledge. https://ricardomartins.cc/2016/08/03/convenient_and_idiomatic_conversions_in_rust Might be a good reference here. |
@lwshang Thanks for the link! I hopefully will find some time to read it tomorrow. If the article gives me some insights that require changes in this issue I'll edit the description (Y). |
@lwshang I was reading through the post and some things about costs caught my eye:
So this basically says that
Does this mean that programmers that wish to convert references to references using an expensive conversion way should not use
I have seen this quite often as a good practice! (Depending on the scenario I guess?) Maybe we should include examples of this as well? If you all agree on these points I don't mind doing an attempt to improve it! |
To you last comment
Agree, there is no implication for the cost of the conversion from these traits.
I believe that the reference to reference conversion usually do less copy so that it is cheaper. So if we have to do heavy calculation before we can return a reference of the new type, we should better not to use I went through the implementors of trait Return to your original questions:For the first question, I agree that the Simple is not a clear term. My understanding is that the conversion made by Therefore, there is actually no inference from simple to cheap computation. Of course, we can improve the document by clarifying this distinction. The second and third question is relative. The context there is that the crate writer should implement traits for the struct (T) of that crate. If writer want to convert T to some type (U) outside the crate, it should be |
@steveklabnik I don't want to bother you if you didn't forget it but just have been busy with other stuff, but just in case you forgot here another reminder! I am curious to what your view is on this. |
I had it on my to-do list for this morning, you beat me to it :)
Yes, there's no expensiveness requirement here, so we shouldn't talk about it.
It's due to coherence; this would mean that the implementations would be recursive. Anything that implements from would implement into which implements from which implements into which....
Yeah, I think pointing people to Into makes a lot of sense.
Please do! |
@steveklabnik Thanks for your response! I will put it on my list and hope to create a PR for it somewhere this week! I'll tag you again once it's done! |
@steveklabnik Sorry for bothering you again, but I have a few more questions!: Module DocumentationIn the module description of
Question: In what scenarios should we use implement a reference-to-reference conversion using We can also read there:
Question: What is this "greater flexibility" compared to the Into trait? Apparently it's more then the "free Into implementation" since the text uses 'and offer ...'. AsRef / AsMut DocumentationI see that AsRef and AsMut also make statements about cost, multiple times.
Question: Does this mean that programmers that wish to convert references to references using an expensive conversion way should not use AsRef and AsMut but rather a dedicated method? Otherwise I will probably be nice to add that explicitly to the documentation as well! Just asking these questions such that I improve the docs better! |
I think this is also bad wording; I would just remove this whole sentence.
Since these can fail, you can use them in more situations. That is, you can imagine that every body of From calls TryFrom, and panics on error. Does that make sense? That said, you shouldn't actually do that, as they're for infallible conversions, but since
Yes.
By implementing From on Does that make sense?
Absolutely, these are great questions. I'm looking forward to it! |
…ation, r=steveklabnik Improve the documentation for std::convert (From, Into, AsRef and AsMut) # Description In this PR I updated the documentation of From, Into, AsRef and AsMut, as well as the general std::convert module documentation. The discussion in rust-lang#59163 provided information that was not yet present in the docs, or was not expressed clearly enough. I tried to clarify the examples that were already present in the docs as well as add more information about considered best-practices that came out of the discussion in rust-lang#59163 @steveklabnik I hope I didn't change too much. This is an initial version! I will scan through everything tomorrow as well again to see if I made any typo's or errors, and maybe make some small changes here and there. All suggestions are welcome! closes rust-lang#59163
Description
I was reading on the From and Into traits and I got a little confused:
From
states:whereas
Into
states:The Problem
This makes it seem like implementing
From
should be used for implementing relatively cheap operations (interpreting the word 'simple' as 'cheap') automatically gainingInto
for free. Whereas implementingInto
(and therefore not getting theFrom
trait for free) should be used for expensive conversions.However later in the documentation of
Into
states:which in turns suggests that one should not implement
Into
at all (unless you want to convert into a type outside your crate).Now I have 3 questions:
Is it true, that
From
should only be used for inexpensive conversions? (If not, we should probably change the documentation of bothFrom
andInto
to refrain from saying things about expensiveness).The documentation of
Into
states that one should only implementInto
if you want to convert into a type outside your crate.From
cannot do this as the example shows. However the significant limitation is that you do not get theFrom
trait for free, meaning that library authors usingFrom
as trait bounds cannot automatically use theInto
implementation. Is there a reason why implementingInto
does not automatically implementFrom
as well?Into
nicely states that it should only be used when implementing traits for types in external crates. Maybe the documentation ofFrom
should also note this?I am curious on what you all think!
Disclaimer: I have been using Rust for the last 4 months, so I would not consider myself an expert. So if I am wrong on some points or misunderstand anything, please correct me!
The text was updated successfully, but these errors were encountered: