-
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
Conversion traits should emphasize their intended use and limitations #29701
Comments
Sounds fair enough. |
For context: this library has a struct that holds a value of generic type |
part of #29349 |
@gankro Which traits should be included in this audit?
Anything else? |
Discussing Borrow/ToOwned might also be relevant? In particular when to use Borrow vs AsRef. |
There might be some traits that would just require consistency of the results of its method(s) to be deemed "trustworthy", and only the values of the arguments would be enough for calculating the results, neither any side effects need to occur. This looks suspiciously similar to limitations imposed on CTFE code - its results have to be consistent, it only has access to input arguments and can't cause side effects. Basically, once CTFE is introduced in Rust (see rust-lang/rfcs#322), it might be reasonable to mark those methods in such traits as EDIT: Given that the current RFC for |
It is unclear to me how relevant this issue is today. It's been open for a really long time with no comments. We've re-done |
The conversion traits, in my books, serve one primary role: convenient generic one-time conversions to a concrete type to make APIs "just work". For instance,
fn foo<P: AsRef<Path>>(path: P)
"just works" for the various kinds of string.Invoking them in this kind of generic context is pretty much guaranteed to work because of the magic of trait resolution in generic contexts. However outside of this context, they can and will behave in wild and uncontrolled ways. There are two major issues I see:
let x = y.as_ref()
may plausibly yieldy
,&y
,&*y
,&**y
, or some particular concrete impl depending on the type of y (and x?).The text was updated successfully, but these errors were encountered: