Skip to content

Add additional automatic conversions to usize/isize. #29220

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,9 +1473,12 @@ impl fmt::Display for ParseIntError {

pub use num::dec2flt::ParseFloatError;

// Conversion traits for primitive integer types
// Conversions T -> T are covered by a blanket impl and therefore excluded
// Some conversions from and to usize/isize are not implemented due to portability concerns
// Conversion traits for primitive integer types (only) where the conversion is
// guaranteed to be lossless on all platforms. Conversions T -> T are covered
// by a blanket impl and therefore excluded. Conversions from `usize` and
// `isize` to other types are not implemented because there are no primitive
// integer types that are defined to be at least as large as them on all
// architectures.
macro_rules! impl_from {
($Small: ty, $Large: ty) => {
#[stable(feature = "lossless_int_conv", since = "1.5.0")]
Expand All @@ -1496,6 +1499,7 @@ impl_from! { u8, u64 }
impl_from! { u8, usize }
impl_from! { u16, u32 }
impl_from! { u16, u64 }
impl_from! { u16, usize }
impl_from! { u32, u64 }

// Signed -> Signed
Expand All @@ -1505,12 +1509,14 @@ impl_from! { i8, i64 }
impl_from! { i8, isize }
impl_from! { i16, i32 }
impl_from! { i16, i64 }
impl_from! { i16, isize }
impl_from! { i32, i64 }

// Unsigned -> Signed
impl_from! { u8, i16 }
impl_from! { u8, i32 }
impl_from! { u8, i64 }
impl_from! { u8, isize }
impl_from! { u16, i32 }
impl_from! { u16, i64 }
impl_from! { u32, i64 }
4 changes: 4 additions & 0 deletions src/libstd/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ mod prim_u64 { }
//
/// The pointer-sized signed integer type.
///
/// `isize` is guaranteed to at least as large as `i16`.
///
/// *[See also the `std::isize` module](isize/index.html).*
///
mod prim_isize { }
Expand All @@ -413,6 +415,8 @@ mod prim_isize { }
//
/// The pointer-sized unsigned integer type.
///
/// `usize` is guaranteed to be at least as large as `u16`.
Copy link
Contributor

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.

http://www.atmel.com/images/doc1497.pdf

Copy link
Contributor Author

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.

///
/// *[See also the `std::usize` module](usize/index.html).*
///
mod prim_usize { }
Expand Down