-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Improve the documentation of Display
and FromStr
, and their interactions
#136687
base: master
Are you sure you want to change the base?
Improve the documentation of Display
and FromStr
, and their interactions
#136687
Conversation
…actions In particular: - `Display` is not necessarily lossless - The output of `Display` might not be parseable by `FromStr`, and might not produce the same value if it is. - Calling `.parse()` on the output of `Display` is usually a mistake unless a type's documented output and input formats match. - The input formats accepted by `FromStr` depend on the type.
I think I generally agree here, but I do think making |
@BurntSushi wrote:
I'm not sure if even a description of "good practice" matches widespread community practice. "Where possible" excludes a large number of things, including things like Path/PathBuf, OsStr/OsString, BStr/BString, and many types whose In addition to that, a That said, I can attempt to tweak the language here, without making any new guarantees. |
I think there might be a misunderstanding? I'm saying that if your type provides both I agree about the delimiters and what not. But I think that's a different can of worms. :-) |
Aside: Lacking only
Personally I don't love the tight coupling between |
I added a note that "Having both |
Thanks! I'm happy with that. |
(Is that an r= or would you prefer someone else to review? :) ) |
I did a bit more of a careful review. I think the phrasing here is still a bit too pessimistic for my liking. |
5192570
to
5f82fb7
Compare
Apparently my attempt to push the change I mentioned in #136687 (comment) failed. Hopefully that helps. |
@BurntSushi I think I've addressed your concern here; please let me know if the current version doesn't address your concern. |
/// If a type happens to have a lossless `Display` implementation whose output is meant to be | ||
/// conveniently machine-parseable and not just meant for human consumption, then the type may wish | ||
/// to accept the same format in `FromStr`, and document that usage. Having both `Display` and | ||
/// `FromStr` implementations where the result of `Display` cannot be parsed with `FromStr` may |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels weaker than I'd expect, TBH. I'd have expected to see a "if you implement both, then you should make the display parsable, ideally to an equivalent (but perhaps not identical) value".
Not a "must", but more than a "if you feel like it I guess you could".
Maybe it'd be worth saying something more directly that this isn't a serialization API, and that people wanting that should check crates.io?
TBH, the amount of back-and-forth makes me think the text here might as well go through a libs-api FCP, to give it more weight. Otherwise it has to be really vague and not really say anything.
One example in the standard library where a type implements both Display and FromStr but doesn't roundtrip is SocketAddrV6. The flowinfo and possibly the scopeinfo aren't preserved. |
Reading through this thread, it seems that the discussion fundamentally boils down to 2 questions:
Obviously a type may "override" these guarantees with documentation. We only need to specify the default expectations in the absence of specific documentation to the contrary. I believe that the answer to the first question is yes: the default expectation should be that |
Having Maybe in the 2027 edition we can have edition-specific traits (I'm looking at you, Or we could rename Both of these would mean a semantic difference between |
So err maybe we could add some docs to |
I don't see how. The guidelines don't say or recommend that
My 2c is that |
In particular:
Display
is not necessarily losslessDisplay
might not be parseable byFromStr
, and mightnot produce the same value if it is.
.parse()
on the output ofDisplay
is usually a mistakeunless a type's documented output and input formats match.
FromStr
depend on the type.This documentation adds no API surface area and makes no guarantees about stability. To the best of my knowledge, everything it says is already established to be true. As such, I don't think it needs an FCP.