Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement conversion traits for primitive integer types #28921
Conversation
rust-highfive
assigned
aturon
Oct 8, 2015
petrochenkov
force-pushed the
petrochenkov:intconv
branch
from
48b66df
to
47f0f2e
Oct 9, 2015
oli-obk
reviewed
Oct 9, 2015
| impl_from! { u32, usize } | ||
| #[cfg(target_pointer_width = "64")] | ||
| impl_from! { u64, usize } | ||
| #[cfg(target_pointer_width = "16")] |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Very cool. This makes it possible to get rid of lots of |
This comment has been minimized.
This comment has been minimized.
this screams for a lint... |
This comment has been minimized.
This comment has been minimized.
Yes, but not right now, because now we can write |
This comment has been minimized.
This comment has been minimized.
But we can write Nice to see lossless int conversions land in std. I've been using my own
I treat |
aturon
reviewed
Oct 9, 2015
| impl_from! { u32, u64 } | ||
| #[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))] | ||
| impl_from! { u32, usize } | ||
| #[cfg(target_pointer_width = "64")] |
This comment has been minimized.
This comment has been minimized.
aturon
Oct 9, 2015
Member
Hmm, I am a bit wary of taking this step: it makes it easy to silently/accidentally introduce arch-specific code. (Contrast this with the approach we took with OS-specific extensions, where you generally have to import a specific trait to "opt in" to platform specificity.)
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 9, 2015
Author
Contributor
It was discussed in the RFC and tested on rustc Windows 64-bit / Linux 32-bit. Yes, porting to a new platform assumes changing some into into cast/as, but potential porting bugs are also uncovered.
This comment has been minimized.
This comment has been minimized.
alexcrichton
Oct 9, 2015
Member
Yeah I would prefer to avoid usize and isize in these "always working" coercions for now. I'm not actually aware of any types which have some traits implemented on some platforms and not others, and that's a great boon for portability so it'd be nice to keep it that way!
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 9, 2015
Author
Contributor
This is significant and important part of the RFC, it's motivated and tested in practice, I won't surrender it just like that. I'd argue that most of integer conversions actually involve usize.
Any comments from @japaric? Do your own conversion traits perform conversions to/from usize?
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 9, 2015
Author
Contributor
Here's some old statistics: https://internals.rust-lang.org/t/the-operator-as-statistics-of-usage/1353
as usize is very popular, including indexing context v[a as usize].
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 9, 2015
Author
Contributor
Too bad implemented trait methods can't be marked with a warning message attribute, a lint.
Something like "you use a method making you code non-portable between 32-bit and 64-bit".
If user isn't interested in this kind of portability (or the types are different on different platforms so the conversion is always lossless even if it would not be lossless if the types were the same, but the lint doesn't know that) he could explicitly silence the warning.
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 9, 2015
Author
Contributor
Hmm, how do you think, if I write a specialized built-in portability lint for this, would it be a good idea? (This lint could detect other 32<->64 portability problems eventually).
This comment has been minimized.
This comment has been minimized.
aturon
Oct 9, 2015
Member
Linting sounds like a reasonable way to approach this problem, for sure; it's an idea that's been tossed around not just for this portability issue, but also to make things like implicit widening part of the language but allow people to "opt out" via a lint.
It'd be awesome if this infrastructure could be used in e.g. the stability lint, which currently is not sensitive to the stability of trait impls. I'm not sure how much work that would be involved.
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 9, 2015
Author
Contributor
Okay, so I'm going to comment out the impls problematic for 32<->64 portability for now to merge this PR and will return later with a lint.
This comment has been minimized.
This comment has been minimized.
aturon
reviewed
Oct 9, 2015
| @@ -1486,7 +1486,7 @@ shr_assign_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize } | |||
| #[lang = "index"] | |||
| #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"] | |||
| #[stable(feature = "rust1", since = "1.0.0")] | |||
| pub trait Index<Idx: ?Sized> { | |||
| pub trait Index<Idx: ?Sized = usize> { | |||
This comment has been minimized.
This comment has been minimized.
aturon
Oct 9, 2015
Member
Can you elaborate on the necessity of this step? I'm slightly wary of changing the global default here, and in principle inference should be able to depend on the impls provided for a particular type.
This comment has been minimized.
This comment has been minimized.
petrochenkov
Oct 9, 2015
Author
Contributor
Type inference is not good enough to infer
let a: u16 = 10;
let b = vector[a.into()];
But... I've just tested it and it turns out the inference is not good enough to infer it even with trait Index<Idx: ?Sized = usize> and #![feature(default_type_parameter_fallback)]! So, this change can be reverted for now.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
cc @rust-lang/libs When we discussed this RFC briefly this week, we didn't talk in detail about how |
petrochenkov
force-pushed the
petrochenkov:intconv
branch
from
47f0f2e
to
5ece8ce
Oct 9, 2015
This comment has been minimized.
This comment has been minimized.
|
With the controversial parts removed, this should be fine to land, right? |
petrochenkov
referenced this pull request
Oct 13, 2015
Closed
Indexed and IndexedMut traits only accept usize #29010
This comment has been minimized.
This comment has been minimized.
|
@tbu- Most likely, though I would like to get broader feedback on the 32bit -> usize conversion. I'm going to flag this for a libs team decision at the meeting tomorrow. |
aturon
added
I-needs-decision
T-libs
labels
Oct 13, 2015
This comment has been minimized.
This comment has been minimized.
|
(fwiw we've long held that 16-bit isn't supported. I can't recall any specific past decisions that legitimately preclude it, but I feel like they exist) |
brson
added
the
relnotes
label
Oct 14, 2015
alexcrichton
removed
the
I-needs-decision
label
Oct 15, 2015
This comment has been minimized.
This comment has been minimized.
|
The libs team talked about this today and our conclusion was that at this time we'd like to avoid any It sounded like one of the main use cases of @petrochenkov could you perhaps elaborate a bit on the use cases you're thinking of for |
petrochenkov
force-pushed the
petrochenkov:intconv
branch
from
f8cac27
to
6f3e84d
Oct 15, 2015
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton
We can always argue later, but it's less obvious that we want to do it right now
Please, no |
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Oct 15, 2015
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
bors
merged commit 6f3e84d
into
rust-lang:master
Oct 15, 2015
This was referenced Oct 16, 2015
This comment has been minimized.
This comment has been minimized.
briansmith
commented on src/libcore/num/mod.rs in 6f3e84d
Oct 20, 2015
|
NIT: I suggest ending these two sentences with a period. More substantially, it would be nice to document more clearly/precisely what the portability concerns are. Is the goal to support platforms where |
This comment has been minimized.
This comment has been minimized.
|
Personally, I have no concerns, besides maybe a lint for 32<->64 portability. According to rust-lang#28921 (comment) libs team has some vague concerns, not clearly articulated, so it's probably not my responsibility to document them. |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Oct 20, 2015
I am not @petrochenkov. But, in some cases in my code I have static size values of type
If so, could we please add the |
This was referenced Oct 20, 2015
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Oct 22, 2015
My PR #29220 adds conversions from |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Oct 22, 2015
I agree. In particular, that shouldn't be added if it means having a runtime check for 32-bit-and-smaller platforms. |
This comment has been minimized.
This comment has been minimized.
|
What are the concerns about u16 -> usize and u32 -> usize? Are these conversions always save? The smallest size of usize is u32? |
petrochenkov commentedOct 8, 2015
Part of rust-lang/rfcs#1218 (comment)
r? @aturon