Skip to content

Commit 96bf760

Browse files
authored
Rollup merge of rust-lang#59979 - stepancheg:num-size, r=ehuss
to_xe_bytes for isize and usize returns an array of different size ... on different platforms. Official rustdoc of [`usize::to_le_bytes`](https://doc.rust-lang.org/std/primitive.usize.html#method.to_le_bytes) displays signature ``` pub fn to_ne_bytes(self) -> [u8; 8] ``` which might be misleading: this function returns 4 bytes on 32-bit systems. With this commit applied rustdoc for `isize` and `usize` is this: <img width="740" alt="2019-04-15_0020" src="https://user-images.githubusercontent.com/28969/56100765-9f69b380-5f14-11e9-974c-daa25edaa881.png">
2 parents d7f4a86 + 764e366 commit 96bf760

File tree

1 file changed

+83
-30
lines changed

1 file changed

+83
-30
lines changed

src/libcore/num/mod.rs

+83-30
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,31 @@ pub mod diy_float;
214214

215215
mod wrapping;
216216

217+
macro_rules! usize_isize_to_xe_bytes_doc {
218+
() => {"
219+
220+
**Note**: This function returns an array of length 2, 4 or 8 bytes
221+
depending on the target pointer size.
222+
223+
"}
224+
}
225+
226+
227+
macro_rules! usize_isize_from_xe_bytes_doc {
228+
() => {"
229+
230+
**Note**: This function takes an array of length 2, 4 or 8 bytes
231+
depending on the target pointer size.
232+
233+
"}
234+
}
235+
217236
// `Int` + `SignedInt` implemented for signed integers
218237
macro_rules! int_impl {
219238
($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $Min:expr, $Max:expr, $Feature:expr,
220239
$EndFeature:expr, $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
221-
$reversed:expr, $le_bytes:expr, $be_bytes:expr) => {
240+
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
241+
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => {
222242
doc_comment! {
223243
concat!("Returns the smallest value that can be represented by this integer type.
224244
@@ -2023,7 +2043,9 @@ $EndFeature, "
20232043
doc_comment! {
20242044
concat!("Return the memory representation of this integer as a byte array in
20252045
big-endian (network) byte order.
2026-
2046+
",
2047+
$to_xe_bytes_doc,
2048+
"
20272049
# Examples
20282050
20292051
```
@@ -2041,7 +2063,9 @@ assert_eq!(bytes, ", $be_bytes, ");
20412063
doc_comment! {
20422064
concat!("Return the memory representation of this integer as a byte array in
20432065
little-endian byte order.
2044-
2066+
",
2067+
$to_xe_bytes_doc,
2068+
"
20452069
# Examples
20462070
20472071
```
@@ -2064,7 +2088,9 @@ native byte order.
20642088
As the target platform's native endianness is used, portable code
20652089
should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
20662090
instead.
2067-
2091+
",
2092+
$to_xe_bytes_doc,
2093+
"
20682094
[`to_be_bytes`]: #method.to_be_bytes
20692095
[`to_le_bytes`]: #method.to_le_bytes
20702096
@@ -2089,7 +2115,9 @@ assert_eq!(bytes, if cfg!(target_endian = \"big\") {
20892115
doc_comment! {
20902116
concat!("Create an integer value from its representation as a byte array in
20912117
big endian.
2092-
2118+
",
2119+
$from_xe_bytes_doc,
2120+
"
20932121
# Examples
20942122
20952123
```
@@ -2120,7 +2148,9 @@ doc_comment! {
21202148
concat!("
21212149
Create an integer value from its representation as a byte array in
21222150
little endian.
2123-
2151+
",
2152+
$from_xe_bytes_doc,
2153+
"
21242154
# Examples
21252155
21262156
```
@@ -2157,7 +2187,9 @@ appropriate instead.
21572187
21582188
[`from_be_bytes`]: #method.from_be_bytes
21592189
[`from_le_bytes`]: #method.from_le_bytes
2160-
2190+
",
2191+
$from_xe_bytes_doc,
2192+
"
21612193
# Examples
21622194
21632195
```
@@ -2193,28 +2225,28 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
21932225
#[lang = "i8"]
21942226
impl i8 {
21952227
int_impl! { i8, i8, u8, 8, -128, 127, "", "", 2, "-0x7e", "0xa", "0x12", "0x12", "0x48",
2196-
"[0x12]", "[0x12]" }
2228+
"[0x12]", "[0x12]", "", "" }
21972229
}
21982230

21992231
#[lang = "i16"]
22002232
impl i16 {
22012233
int_impl! { i16, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
2202-
"0x2c48", "[0x34, 0x12]", "[0x12, 0x34]" }
2234+
"0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "" }
22032235
}
22042236

22052237
#[lang = "i32"]
22062238
impl i32 {
22072239
int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
22082240
"0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
2209-
"[0x12, 0x34, 0x56, 0x78]" }
2241+
"[0x12, 0x34, 0x56, 0x78]", "", "" }
22102242
}
22112243

22122244
#[lang = "i64"]
22132245
impl i64 {
22142246
int_impl! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "", 12,
22152247
"0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
22162248
"0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
2217-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]" }
2249+
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", "", "" }
22182250
}
22192251

22202252
#[lang = "i128"]
@@ -2226,22 +2258,24 @@ impl i128 {
22262258
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
22272259
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
22282260
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
2229-
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]" }
2261+
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]", "", "" }
22302262
}
22312263

22322264
#[cfg(target_pointer_width = "16")]
22332265
#[lang = "isize"]
22342266
impl isize {
22352267
int_impl! { isize, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234",
2236-
"0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]" }
2268+
"0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]",
2269+
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
22372270
}
22382271

22392272
#[cfg(target_pointer_width = "32")]
22402273
#[lang = "isize"]
22412274
impl isize {
22422275
int_impl! { isize, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
22432276
"0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]",
2244-
"[0x12, 0x34, 0x56, 0x78]" }
2277+
"[0x12, 0x34, 0x56, 0x78]",
2278+
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
22452279
}
22462280

22472281
#[cfg(target_pointer_width = "64")]
@@ -2250,14 +2284,16 @@ impl isize {
22502284
int_impl! { isize, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "",
22512285
12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
22522286
"0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
2253-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]" }
2287+
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
2288+
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
22542289
}
22552290

22562291
// `Int` + `UnsignedInt` implemented for unsigned integers
22572292
macro_rules! uint_impl {
22582293
($SelfT:ty, $ActualT:ty, $BITS:expr, $MaxV:expr, $Feature:expr, $EndFeature:expr,
22592294
$rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
2260-
$reversed:expr, $le_bytes:expr, $be_bytes:expr) => {
2295+
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
2296+
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => {
22612297
doc_comment! {
22622298
concat!("Returns the smallest value that can be represented by this integer type.
22632299
@@ -3817,7 +3853,9 @@ $EndFeature, "
38173853
doc_comment! {
38183854
concat!("Return the memory representation of this integer as a byte array in
38193855
big-endian (network) byte order.
3820-
3856+
",
3857+
$to_xe_bytes_doc,
3858+
"
38213859
# Examples
38223860
38233861
```
@@ -3835,7 +3873,9 @@ assert_eq!(bytes, ", $be_bytes, ");
38353873
doc_comment! {
38363874
concat!("Return the memory representation of this integer as a byte array in
38373875
little-endian byte order.
3838-
3876+
",
3877+
$to_xe_bytes_doc,
3878+
"
38393879
# Examples
38403880
38413881
```
@@ -3858,7 +3898,9 @@ native byte order.
38583898
As the target platform's native endianness is used, portable code
38593899
should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
38603900
instead.
3861-
3901+
",
3902+
$to_xe_bytes_doc,
3903+
"
38623904
[`to_be_bytes`]: #method.to_be_bytes
38633905
[`to_le_bytes`]: #method.to_le_bytes
38643906
@@ -3883,7 +3925,9 @@ assert_eq!(bytes, if cfg!(target_endian = \"big\") {
38833925
doc_comment! {
38843926
concat!("Create an integer value from its representation as a byte array in
38853927
big endian.
3886-
3928+
",
3929+
$from_xe_bytes_doc,
3930+
"
38873931
# Examples
38883932
38893933
```
@@ -3914,7 +3958,9 @@ fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
39143958
concat!("
39153959
Create an integer value from its representation as a byte array in
39163960
little endian.
3917-
3961+
",
3962+
$from_xe_bytes_doc,
3963+
"
39183964
# Examples
39193965
39203966
```
@@ -3951,7 +3997,9 @@ appropriate instead.
39513997
39523998
[`from_be_bytes`]: #method.from_be_bytes
39533999
[`from_le_bytes`]: #method.from_le_bytes
3954-
4000+
",
4001+
$from_xe_bytes_doc,
4002+
"
39554003
# Examples
39564004
39574005
```
@@ -3987,7 +4035,7 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
39874035
#[lang = "u8"]
39884036
impl u8 {
39894037
uint_impl! { u8, u8, 8, 255, "", "", 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
3990-
"[0x12]" }
4038+
"[0x12]", "", "" }
39914039

39924040

39934041
/// Checks if the value is within the ASCII range.
@@ -4506,21 +4554,22 @@ impl u8 {
45064554
#[lang = "u16"]
45074555
impl u16 {
45084556
uint_impl! { u16, u16, 16, 65535, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
4509-
"[0x34, 0x12]", "[0x12, 0x34]" }
4557+
"[0x34, 0x12]", "[0x12, 0x34]", "", "" }
45104558
}
45114559

45124560
#[lang = "u32"]
45134561
impl u32 {
45144562
uint_impl! { u32, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
4515-
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]" }
4563+
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "" }
45164564
}
45174565

45184566
#[lang = "u64"]
45194567
impl u64 {
45204568
uint_impl! { u64, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
45214569
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
45224570
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
4523-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]" }
4571+
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
4572+
"", ""}
45244573
}
45254574

45264575
#[lang = "u128"]
@@ -4531,20 +4580,23 @@ impl u128 {
45314580
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
45324581
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
45334582
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
4534-
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]" }
4583+
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
4584+
"", ""}
45354585
}
45364586

45374587
#[cfg(target_pointer_width = "16")]
45384588
#[lang = "usize"]
45394589
impl usize {
45404590
uint_impl! { usize, u16, 16, 65535, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
4541-
"[0x34, 0x12]", "[0x12, 0x34]" }
4591+
"[0x34, 0x12]", "[0x12, 0x34]",
4592+
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
45424593
}
45434594
#[cfg(target_pointer_width = "32")]
45444595
#[lang = "usize"]
45454596
impl usize {
45464597
uint_impl! { usize, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
4547-
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]" }
4598+
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
4599+
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
45484600
}
45494601

45504602
#[cfg(target_pointer_width = "64")]
@@ -4553,7 +4605,8 @@ impl usize {
45534605
uint_impl! { usize, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
45544606
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
45554607
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
4556-
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]" }
4608+
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
4609+
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
45574610
}
45584611

45594612
/// A classification of floating point numbers.

0 commit comments

Comments
 (0)