Skip to content

Commit 2178ef8

Browse files
committed
TryFrom for integers: use From instead for truely-infallible impls
There is precendent in C for having a minimum pointer size, but I don’t feel confident enough about the future to mandate a maximum.
1 parent 188e693 commit 2178ef8

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

Diff for: src/libcore/num/mod.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -3804,14 +3804,11 @@ mod ptr_try_from_impls {
38043804
try_from_both_bounded!(isize, i8);
38053805
try_from_unbounded!(isize, i16, i32, i64, i128);
38063806

3807-
rev!(try_from_unbounded, usize, u16);
38083807
rev!(try_from_upper_bounded, usize, u32, u64, u128);
38093808
rev!(try_from_lower_bounded, usize, i8, i16);
38103809
rev!(try_from_both_bounded, usize, i32, i64, i128);
38113810

3812-
rev!(try_from_unbounded, isize, u8);
38133811
rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
3814-
rev!(try_from_unbounded, isize, i16);
38153812
rev!(try_from_both_bounded, isize, i32, i64, i128);
38163813
}
38173814

@@ -3830,14 +3827,14 @@ mod ptr_try_from_impls {
38303827
try_from_both_bounded!(isize, i8, i16);
38313828
try_from_unbounded!(isize, i32, i64, i128);
38323829

3833-
rev!(try_from_unbounded, usize, u16, u32);
3830+
rev!(try_from_unbounded, usize, u32);
38343831
rev!(try_from_upper_bounded, usize, u64, u128);
38353832
rev!(try_from_lower_bounded, usize, i8, i16, i32);
38363833
rev!(try_from_both_bounded, usize, i64, i128);
38373834

3838-
rev!(try_from_unbounded, isize, u8, u16);
3835+
rev!(try_from_unbounded, isize, u16);
38393836
rev!(try_from_upper_bounded, isize, u32, u64, u128);
3840-
rev!(try_from_unbounded, isize, i16, i32);
3837+
rev!(try_from_unbounded, isize, i32);
38413838
rev!(try_from_both_bounded, isize, i64, i128);
38423839
}
38433840

@@ -3856,14 +3853,14 @@ mod ptr_try_from_impls {
38563853
try_from_both_bounded!(isize, i8, i16, i32);
38573854
try_from_unbounded!(isize, i64, i128);
38583855

3859-
rev!(try_from_unbounded, usize, u16, u32, u64);
3856+
rev!(try_from_unbounded, usize, u32, u64);
38603857
rev!(try_from_upper_bounded, usize, u128);
38613858
rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
38623859
rev!(try_from_both_bounded, usize, i128);
38633860

3864-
rev!(try_from_unbounded, isize, u8, u16, u32);
3861+
rev!(try_from_unbounded, isize, u16, u32);
38653862
rev!(try_from_upper_bounded, isize, u64, u128);
3866-
rev!(try_from_unbounded, isize, i16, i32, i64);
3863+
rev!(try_from_unbounded, isize, i32, i64);
38673864
rev!(try_from_both_bounded, isize, i128);
38683865
}
38693866

@@ -4074,6 +4071,20 @@ impl_from! { u32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")]
40744071
impl_from! { u32, i128, #[stable(feature = "i128", since = "1.26.0")] }
40754072
impl_from! { u64, i128, #[stable(feature = "i128", since = "1.26.0")] }
40764073

4074+
// The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
4075+
// which imply that pointer-sized integers must be at least 16 bits:
4076+
// https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
4077+
impl_from! { u16, usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
4078+
impl_from! { u8, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
4079+
impl_from! { i16, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
4080+
4081+
// RISC-V defines the possibility of a 128-bit address space (RV128).
4082+
4083+
// CHERI proposes 256-bit “capabilities”. Unclear if this would be relevant to usize/isize.
4084+
// https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
4085+
// http://www.csl.sri.com/users/neumann/2012resolve-cheri.pdf
4086+
4087+
40774088
// Note: integers can only be represented with full precision in a float if
40784089
// they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
40794090
// Lossy float conversions are not implemented at this time.

0 commit comments

Comments
 (0)