-
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
Add additional automatic conversions to usize/isize. #29220
Conversation
Also, improve the documentation related to the range of usize and isize.
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
See https://internals.rust-lang.org/t/adding-16-bit-pointer-support/2484 where somebody is trying to add support for a 16-bit platform that (IMO) Rust should support. This is why I didn't add conversions from |
When discussing #28921 the libs team decided to punt on Also guaranteeing that |
@alexcrichton |
I mostly deal with networking protocols, and networking protocols frequently specify that certain quantities are 8 or 16 bits wide. For example., the TLS 1.2 spec has several quantities defined as
I will post an RFC.
Like I mentioned, I purposely didn't include those because they are not AVR-compatible. Regardless, you are right that there is a need for a |
I would like an |
Portability to 16-bit is a pretty niche concern, unlike portability between 32-bit and 64-bit. In my proposed lint-based solution (#28921 (comment), #28921 (comment)) we can have a lint for 16-bit portability too, but disabled by default. |
Even in |
@@ -413,6 +415,8 @@ mod prim_isize { } | |||
// | |||
/// The pointer-sized unsigned integer type. | |||
/// | |||
/// `usize` is guaranteed to be at least as large as `u16`. |
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.
I don't think this change to the language (and therefore RFC) is required. If we ever support a platform with 8-bit pointers (which is unlikely, but still) it would be much easier to just insert a cfg
before impl_from! { u16, usize }
.
For reference:
For the smallest AVR devices with less than 256 bytes of SRAM the tiny memory model
can be used in many cases. When using tiny memory model, all variables in SRAM are
accessed with 8-bit pointers instead of 16-bit pointers. This reduces the code size for
loading pointer values.
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.
don't think this change to the language (and therefore RFC) is required. If we ever support a platform with 8-bit pointers (which is unlikely, but still) it would be much easier to just insert a cfg before impl_from! { u16, usize }.
Even if we wanted to support the tiny memory model, usize
would still have to be at least 16 bits, because size_t
would still be 16 bits, and usize
being smaller than size_t
would mean that usize
is smaller than size_t
, which would make dealing with the FFI in a generic way very complicated.
I also personally want u32/i32 -> usize/isize conversions, but that still doesn't mean it can be changed here without an RFC. Thanks for the use cases though, they sound like good candidates for motivation in the RFC! |
The "automatic" in the title is confusing, since this isn't some sort of a coercion, just a set of library trait impls. |
☔ The latest upstream changes (presumably #29129) made this pull request unmergeable. Please resolve the merge conflicts. |
What's the status of this PR? |
Closing due to inactivity, but I think that we'll likely want to decide about usize/isize separately before taking further action on this PR. |
Also, improve the documentation related to the range of usize and isize.
See rust-lang/rfcs#1218 (comment) and #28921 (comment).
I decided to be conservative and assume that 16-bit platform support might be added in the future, because C99 supports platforms with 16-bit
size_t
. In particular, C99 specifies that the range ofsize_t
is at least [0, 65535]; i.e.SIZE_MAX
must be at least65,535
and sosizeof(size_t) >= 2
.