-
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
RFC: Consider turning as
into a user-implementable Cast trait
#7080
Comments
You can find a version that's using moves here: https://gist.github.com/erickt/5762933, but unfortunately I'm getting this llvm bug
I expect it's related to #4759. |
Note that the reason for a build-in
However, seeing how
It might be sensible to replace the constexpr-use case with a
which would be hard-coded to expand to a literal of the right type. |
Note that I've been thinking about CTFE and how it can be used with static items. I think @bblum's effect proposal could easily express this: only functions/methods without certain effects are valid for consideration for CTFE. This would fit in nicely with I didn't consider how this would interact with trait objects and I don't know enough about them to evaluate its effect on them. |
@Kimundi: curse you, constant expressions! Bane of my existence. We could tone this down, and provide these traits to standardize how we transform from one type to another, and keep Another option is to copy the
Either way, it's not that common to use these static casts, at least in rust proper. So I wouldn't be too upset if it was a little uglier to use if it allowed us to take better advantage of the
|
Heh, I already left a comment to that end, nevermind 😊 |
A constexpr effect would be tractable, I think. Might be an issue with asserts -- if you wanted to write asserts in your function but have it be constexpr anyway, either you would have to pick one, or the compiler would be smart enough to turn the assert into a compile fail (which seems both possible and pretty leet, but difficult). The latter strikes me as a far-future feature. Also keep in mind effects won't be in 1.0, so it would have to be backwards-compatible. |
@erickt I think hardcoding in for the existing cases would be fine. |
Nominating for well-defined: this is a language feature. |
I'm against doing this because I don't think one type of conversion should be elevated above the others at a language level. For example, you can downcast to a smaller integer type by truncating, or you can clamp to the max value. |
That's a good argument. Having multiple types of conversions makes an overloadable |
I would be happiest if we lived in a world where Rust had CTFE, and we didn't need |
Declining for milestone. We decided this is part of the larger story about constant evaluation, and there are already other bugs open on that. |
My two cents: The reason we have |
@catamorphism which bugs? |
@cmr, @huonw and I were talking in irc about how to name functions that allow you to convert from one type to another. For example, consider these str methods:
These implement a common pattern, where in the
to
case, we are copying the string into the new vec. In theas
case, we are making a no-copy cast from a string to a vector.It'd be nice if we could standardize this pattern, and one way we could do this is to turn the
as
operator into a trait a user can implement, like theNeg
trait. We can do this if we follow the pattern @nikomatsakis laid out on his blog. Here's a working example implementation:While it's a bit wordy, it does work.
Unfortunately there's a third conversion option that I couldn't figure out how to fit into this paradigm, where we consume the input to produce the output:
Other places this would be useful is when we can cheaply transform
~str
into~[u8]
, or consume all the elements from aHashMap
and move them into a~[T]
. Perhaps a separate function would be best to capture this case. Or we do the reverse, and sayCast
consumes the input, but since going from one reference type to another is cheap, we optimize that specific case. I'm not sure.The text was updated successfully, but these errors were encountered: