Skip to content

Commit 9b75bd7

Browse files
authored
Unrolled build for #149295
Rollup merge of #149295 - kornelski:endian_bytes, r=Mark-Simulacrum Suggest _bytes versions of endian-converting methods As pointed out [in this article](https://purplesyringa.moe/blog/ntoh-hton-is-a-bad-api/), the `int.to_be()`/`int.to_le()` functions are technically returning a wrong type, because endian-specific representations of integers and Rust's always-native-endian integers are different things. I wanted to make the docs state more directly that byte swapping will result in the type having a "wrong" value, but I wasn't sure whether to delve into the special case of palindrome-like values (`0x12343412`) not changing. I've updated the docs to suggest `{to,from}_[lb]e_bytes()` instead. These methods use `[u8; _]` for endian-specific representations, which is a different type than the native-endian `u16`/`u32`/`u64`, and this is a type-safety improvement.
2 parents a417515 + 3faad7c commit 9b75bd7

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

library/core/src/num/int_macros.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ macro_rules! int_impl {
372372
///
373373
/// On big endian this is a no-op. On little endian the bytes are swapped.
374374
///
375+
/// See also [from_be_bytes()](Self::from_be_bytes).
376+
///
375377
/// # Examples
376378
///
377379
/// ```
@@ -402,6 +404,8 @@ macro_rules! int_impl {
402404
///
403405
/// On little endian this is a no-op. On big endian the bytes are swapped.
404406
///
407+
/// See also [from_le_bytes()](Self::from_le_bytes).
408+
///
405409
/// # Examples
406410
///
407411
/// ```
@@ -428,9 +432,15 @@ macro_rules! int_impl {
428432
}
429433
}
430434

431-
/// Converts `self` to big endian from the target's endianness.
435+
/// Swaps bytes of `self` on little endian targets.
432436
///
433-
/// On big endian this is a no-op. On little endian the bytes are swapped.
437+
/// On big endian this is a no-op.
438+
///
439+
/// The returned value has the same type as `self`, and will be interpreted
440+
/// as (a potentially different) value of a native-endian
441+
#[doc = concat!("`", stringify!($SelfT), "`.")]
442+
///
443+
/// See [`to_be_bytes()`](Self::to_be_bytes) for a type-safe alternative.
434444
///
435445
/// # Examples
436446
///
@@ -459,9 +469,15 @@ macro_rules! int_impl {
459469
}
460470
}
461471

462-
/// Converts `self` to little endian from the target's endianness.
472+
/// Swaps bytes of `self` on big endian targets.
463473
///
464-
/// On little endian this is a no-op. On big endian the bytes are swapped.
474+
/// On little endian this is a no-op.
475+
///
476+
/// The returned value has the same type as `self`, and will be interpreted
477+
/// as (a potentially different) value of a native-endian
478+
#[doc = concat!("`", stringify!($SelfT), "`.")]
479+
///
480+
/// See [`to_le_bytes()`](Self::to_le_bytes) for a type-safe alternative.
465481
///
466482
/// # Examples
467483
///

0 commit comments

Comments
 (0)