Skip to content

Commit 945f032

Browse files
committed
Add individual documentation for <integer>.swap_bytes/.reverse_bits
1 parent f6d43ed commit 945f032

File tree

1 file changed

+112
-113
lines changed

1 file changed

+112
-113
lines changed

src/libcore/num/mod.rs

+112-113
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ mod wrapping;
188188
// `Int` + `SignedInt` implemented for signed integers
189189
macro_rules! int_impl {
190190
($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $Min:expr, $Max:expr, $Feature:expr,
191-
$EndFeature:expr, $rot:expr, $rot_op:expr, $rot_result:expr) => {
191+
$EndFeature:expr, $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
192+
$reversed:expr) => {
192193
doc_comment! {
193194
concat!("Returns the smallest value that can be represented by this integer type.
194195
@@ -380,55 +381,48 @@ assert_eq!(n.rotate_right(", $rot, "), m);
380381
(self as $UnsignedT).rotate_right(n) as Self
381382
}
382383
}
383-
/// Reverses the byte order of the integer.
384-
///
385-
/// # Examples
386-
///
387-
/// Please note that this example is shared between integer types.
388-
/// Which explains why `i16` is used here.
389-
///
390-
/// Basic usage:
391-
///
392-
/// ```
393-
/// let n: i16 = 0b0000000_01010101;
394-
/// assert_eq!(n, 85);
395-
///
396-
/// let m = n.swap_bytes();
397-
///
398-
/// assert_eq!(m, 0b01010101_00000000);
399-
/// assert_eq!(m, 21760);
400-
/// ```
401-
#[stable(feature = "rust1", since = "1.0.0")]
402-
#[rustc_const_unstable(feature = "const_int_ops")]
403-
#[inline]
404-
pub const fn swap_bytes(self) -> Self {
405-
(self as $UnsignedT).swap_bytes() as Self
384+
doc_comment! {
385+
concat!("Reverses the byte order of the integer.
386+
387+
# Examples
388+
389+
Basic usage:
390+
391+
```
392+
let n = ", $swap_op, stringify!($SelfT), ";
393+
394+
let m = n.swap_bytes();
395+
396+
assert_eq!(m, ", $swapped, ");
397+
```"),
398+
#[stable(feature = "rust1", since = "1.0.0")]
399+
#[rustc_const_unstable(feature = "const_int_ops")]
400+
#[inline]
401+
pub const fn swap_bytes(self) -> Self {
402+
(self as $UnsignedT).swap_bytes() as Self
403+
}
406404
}
407405

408-
/// Reverses the bit pattern of the integer.
409-
///
410-
/// # Examples
411-
///
412-
/// Please note that this example is shared between integer types.
413-
/// Which explains why `i16` is used here.
414-
///
415-
/// Basic usage:
416-
///
417-
/// ```
418-
/// #![feature(reverse_bits)]
419-
///
420-
/// let n: i16 = 0b0000000_01010101;
421-
/// assert_eq!(n, 85);
422-
///
423-
/// let m = n.reverse_bits();
424-
///
425-
/// assert_eq!(m as u16, 0b10101010_00000000);
426-
/// assert_eq!(m, -22016);
427-
/// ```
428-
#[unstable(feature = "reverse_bits", issue = "48763")]
429-
#[inline]
430-
pub fn reverse_bits(self) -> Self {
431-
(self as $UnsignedT).reverse_bits() as Self
406+
doc_comment! {
407+
concat!("Reverses the bit pattern of the integer.
408+
409+
# Examples
410+
411+
Basic usage:
412+
413+
```
414+
#![feature(reverse_bits)]
415+
416+
let n = ", $swap_op, stringify!($SelfT), ";
417+
let m = n.reverse_bits();
418+
419+
assert_eq!(m, ", $reversed, ");
420+
```"),
421+
#[unstable(feature = "reverse_bits", issue = "48763")]
422+
#[inline]
423+
pub fn reverse_bits(self) -> Self {
424+
(self as $UnsignedT).reverse_bits() as Self
425+
}
432426
}
433427

434428
doc_comment! {
@@ -2009,50 +2003,57 @@ $EndFeature, "
20092003

20102004
#[lang = "i8"]
20112005
impl i8 {
2012-
int_impl! { i8, i8, u8, 8, -128, 127, "", "", 2, "-0x7e", "0xa" }
2006+
int_impl! { i8, i8, u8, 8, -128, 127, "", "", 2, "-0x7e", "0xa", "0x12", "0x12", "0x48" }
20132007
}
20142008

20152009
#[lang = "i16"]
20162010
impl i16 {
2017-
int_impl! { i16, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a" }
2011+
int_impl! { i16, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234", "0x3412",
2012+
"0x2c48" }
20182013
}
20192014

20202015
#[lang = "i32"]
20212016
impl i32 {
2022-
int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301" }
2017+
int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
2018+
"0x12345678", "0x78563412", "0x1e6a2c48" }
20232019
}
20242020

20252021
#[lang = "i64"]
20262022
impl i64 {
20272023
int_impl! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "", 12,
2028-
"0xaa00000000006e1", "0x6e10aa" }
2024+
"0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
2025+
"0x6a2c48091e6a2c48" }
20292026
}
20302027

20312028
#[lang = "i128"]
20322029
impl i128 {
20332030
int_impl! { i128, i128, u128, 128, -170141183460469231731687303715884105728,
20342031
170141183460469231731687303715884105727, "", "", 16,
2035-
"0x13f40000000000000000000000004f76", "0x4f7613f4"
2032+
"0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
2033+
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48"
20362034
}
20372035
}
20382036

20392037
#[cfg(target_pointer_width = "16")]
20402038
#[lang = "isize"]
20412039
impl isize {
2042-
int_impl! { isize, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a" }
2040+
int_impl! { isize, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a", "0x1234",
2041+
"0x3412", "0x2c48" }
20432042
}
20442043

20452044
#[cfg(target_pointer_width = "32")]
20462045
#[lang = "isize"]
20472046
impl isize {
2048-
int_impl! { isize, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301" }
2047+
int_impl! { isize, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301",
2048+
"0x12345678", "0x78563412", "0x1e6a2c48" }
20492049
}
20502050

20512051
#[cfg(target_pointer_width = "64")]
20522052
#[lang = "isize"]
20532053
impl isize {
20542054
int_impl! { isize, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "",
2055-
12, "0xaa00000000006e1", "0x6e10aa" }
2055+
12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412",
2056+
"0x6a2c48091e6a2c48" }
20562057
}
20572058

20582059
// Emits the correct `cttz` call, depending on the size of the type.
@@ -2071,7 +2072,8 @@ macro_rules! uint_cttz_call {
20712072
// `Int` + `UnsignedInt` implemented for unsigned integers
20722073
macro_rules! uint_impl {
20732074
($SelfT:ty, $ActualT:ty, $BITS:expr, $MaxV:expr, $Feature:expr, $EndFeature:expr,
2074-
$rot:expr, $rot_op:expr, $rot_result:expr) => {
2075+
$rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
2076+
$reversed:expr ) => {
20752077
doc_comment! {
20762078
concat!("Returns the smallest value that can be represented by this integer type.
20772079
@@ -2263,55 +2265,48 @@ assert_eq!(n.rotate_right(", $rot, "), m);
22632265
}
22642266
}
22652267

2266-
/// Reverses the byte order of the integer.
2267-
///
2268-
/// # Examples
2269-
///
2270-
/// Basic usage:
2271-
///
2272-
/// Please note that this example is shared between integer types.
2273-
/// Which explains why `u16` is used here.
2274-
///
2275-
/// ```
2276-
/// let n: u16 = 0b0000000_01010101;
2277-
/// assert_eq!(n, 85);
2278-
///
2279-
/// let m = n.swap_bytes();
2280-
///
2281-
/// assert_eq!(m, 0b01010101_00000000);
2282-
/// assert_eq!(m, 21760);
2283-
/// ```
2284-
#[stable(feature = "rust1", since = "1.0.0")]
2285-
#[rustc_const_unstable(feature = "const_int_ops")]
2286-
#[inline]
2287-
pub const fn swap_bytes(self) -> Self {
2288-
unsafe { intrinsics::bswap(self as $ActualT) as Self }
2268+
doc_comment! {
2269+
concat!("
2270+
Reverses the byte order of the integer.
2271+
2272+
# Examples
2273+
2274+
Basic usage:
2275+
2276+
```
2277+
let n = ", $swap_op, stringify!($SelfT), ";
2278+
let m = n.swap_bytes();
2279+
2280+
assert_eq!(m, ", $swapped, ");
2281+
```"),
2282+
#[stable(feature = "rust1", since = "1.0.0")]
2283+
#[rustc_const_unstable(feature = "const_int_ops")]
2284+
#[inline]
2285+
pub const fn swap_bytes(self) -> Self {
2286+
unsafe { intrinsics::bswap(self as $ActualT) as Self }
2287+
}
22892288
}
22902289

2291-
/// Reverses the bit pattern of the integer.
2292-
///
2293-
/// # Examples
2294-
///
2295-
/// Basic usage:
2296-
///
2297-
/// Please note that this example is shared between integer types.
2298-
/// Which explains why `u16` is used here.
2299-
///
2300-
/// ```
2301-
/// #![feature(reverse_bits)]
2302-
///
2303-
/// let n: u16 = 0b0000000_01010101;
2304-
/// assert_eq!(n, 85);
2305-
///
2306-
/// let m = n.reverse_bits();
2307-
///
2308-
/// assert_eq!(m, 0b10101010_00000000);
2309-
/// assert_eq!(m, 43520);
2310-
/// ```
2311-
#[unstable(feature = "reverse_bits", issue = "48763")]
2312-
#[inline]
2313-
pub fn reverse_bits(self) -> Self {
2314-
unsafe { intrinsics::bitreverse(self as $ActualT) as Self }
2290+
doc_comment! {
2291+
concat!("Reverses the bit pattern of the integer.
2292+
2293+
# Examples
2294+
2295+
Basic usage:
2296+
2297+
```
2298+
#![feature(reverse_bits)]
2299+
2300+
let n = ", $swap_op, stringify!($SelfT), ";
2301+
let m = n.reverse_bits();
2302+
2303+
assert_eq!(m, ", $reversed, ");
2304+
```"),
2305+
#[unstable(feature = "reverse_bits", issue = "48763")]
2306+
#[inline]
2307+
pub fn reverse_bits(self) -> Self {
2308+
unsafe { intrinsics::bitreverse(self as $ActualT) as Self }
2309+
}
23152310
}
23162311

23172312
doc_comment! {
@@ -3621,7 +3616,7 @@ $EndFeature, "
36213616

36223617
#[lang = "u8"]
36233618
impl u8 {
3624-
uint_impl! { u8, u8, 8, 255, "", "", 2, "0x82", "0xa" }
3619+
uint_impl! { u8, u8, 8, 255, "", "", 2, "0x82", "0xa", "0x12", "0x12", "0x48" }
36253620

36263621

36273622
/// Checks if the value is within the ASCII range.
@@ -4147,41 +4142,45 @@ impl u8 {
41474142

41484143
#[lang = "u16"]
41494144
impl u16 {
4150-
uint_impl! { u16, u16, 16, 65535, "", "", 4, "0xa003", "0x3a" }
4145+
uint_impl! { u16, u16, 16, 65535, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48" }
41514146
}
41524147

41534148
#[lang = "u32"]
41544149
impl u32 {
4155-
uint_impl! { u32, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301" }
4150+
uint_impl! { u32, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
4151+
"0x78563412", "0x1e6a2c48" }
41564152
}
41574153

41584154
#[lang = "u64"]
41594155
impl u64 {
4160-
uint_impl! { u64, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa" }
4156+
uint_impl! { u64, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
4157+
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48" }
41614158
}
41624159

41634160
#[lang = "u128"]
41644161
impl u128 {
41654162
uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, "", "", 16,
4166-
"0x13f40000000000000000000000004f76", "0x4f7613f4" }
4163+
"0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
4164+
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48" }
41674165
}
41684166

41694167
#[cfg(target_pointer_width = "16")]
41704168
#[lang = "usize"]
41714169
impl usize {
4172-
uint_impl! { usize, u16, 16, 65536, "", "", 4, "0xa003", "0x3a" }
4170+
uint_impl! { usize, u16, 16, 65536, "", "", 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48" }
41734171
}
41744172
#[cfg(target_pointer_width = "32")]
41754173
#[lang = "usize"]
41764174
impl usize {
4177-
uint_impl! { usize, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301" }
4175+
uint_impl! { usize, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301", "0x12345678",
4176+
"0x78563412", "0x1e6a2c48" }
41784177
}
41794178

41804179
#[cfg(target_pointer_width = "64")]
41814180
#[lang = "usize"]
41824181
impl usize {
4183-
uint_impl! { usize, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1",
4184-
"0x6e10aa" }
4182+
uint_impl! { usize, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa",
4183+
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48" }
41854184
}
41864185

41874186
/// A classification of floating point numbers.

0 commit comments

Comments
 (0)