Skip to content

Commit 9f55705

Browse files
committed
Rollup merge of #53207 - llogiq:num-rotate-docs, r=QuietMisdreavus
Add individual docs for rotate_{left, right}
2 parents f067f3a + cc2503a commit 9f55705

File tree

1 file changed

+115
-113
lines changed

1 file changed

+115
-113
lines changed

src/libcore/num/mod.rs

+115-113
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ 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) => {
191+
$EndFeature:expr, $rot:expr, $rot_op:expr, $rot_result:expr) => {
192192
doc_comment! {
193193
concat!("Returns the smallest value that can be represented by this integer type.
194194
@@ -334,55 +334,52 @@ $EndFeature, "
334334
}
335335
}
336336

337-
/// Shifts the bits to the left by a specified amount, `n`,
338-
/// wrapping the truncated bits to the end of the resulting integer.
339-
///
340-
/// Please note this isn't the same operation as `<<`!
341-
///
342-
/// # Examples
343-
///
344-
/// Please note that this example is shared between integer types.
345-
/// Which explains why `i64` is used here.
346-
///
347-
/// Basic usage:
348-
///
349-
/// ```
350-
/// let n = 0x0123456789ABCDEFi64;
351-
/// let m = -0x76543210FEDCBA99i64;
352-
///
353-
/// assert_eq!(n.rotate_left(32), m);
354-
/// ```
355-
#[stable(feature = "rust1", since = "1.0.0")]
356-
#[inline]
357-
pub fn rotate_left(self, n: u32) -> Self {
358-
(self as $UnsignedT).rotate_left(n) as Self
359-
}
337+
doc_comment! {
338+
concat!("Shifts the bits to the left by a specified amount, `n`,
339+
wrapping the truncated bits to the end of the resulting integer.
360340
361-
/// Shifts the bits to the right by a specified amount, `n`,
362-
/// wrapping the truncated bits to the beginning of the resulting
363-
/// integer.
364-
///
365-
/// Please note this isn't the same operation as `>>`!
366-
///
367-
/// # Examples
368-
///
369-
/// Please note that this example is shared between integer types.
370-
/// Which explains why `i64` is used here.
371-
///
372-
/// Basic usage:
373-
///
374-
/// ```
375-
/// let n = 0x0123456789ABCDEFi64;
376-
/// let m = -0xFEDCBA987654322i64;
377-
///
378-
/// assert_eq!(n.rotate_right(4), m);
379-
/// ```
380-
#[stable(feature = "rust1", since = "1.0.0")]
381-
#[inline]
382-
pub fn rotate_right(self, n: u32) -> Self {
383-
(self as $UnsignedT).rotate_right(n) as Self
341+
Please note this isn't the same operation as `<<`!
342+
343+
# Examples
344+
345+
Basic usage:
346+
347+
```
348+
let n = ", $rot_op, stringify!($SelfT), ";
349+
let m = ", $rot_result, ";
350+
351+
assert_eq!(n.rotate_left(", $rot, "), m);
352+
```"),
353+
#[stable(feature = "rust1", since = "1.0.0")]
354+
#[inline]
355+
pub fn rotate_left(self, n: u32) -> Self {
356+
(self as $UnsignedT).rotate_left(n) as Self
357+
}
384358
}
385359

360+
doc_comment! {
361+
concat!("Shifts the bits to the right by a specified amount, `n`,
362+
wrapping the truncated bits to the beginning of the resulting
363+
integer.
364+
365+
Please note this isn't the same operation as `>>`!
366+
367+
# Examples
368+
369+
Basic usage:
370+
371+
```
372+
let n = ", $rot_result, stringify!($SelfT), ";
373+
let m = ", $rot_op, ";
374+
375+
assert_eq!(n.rotate_right(", $rot, "), m);
376+
```"),
377+
#[stable(feature = "rust1", since = "1.0.0")]
378+
#[inline]
379+
pub fn rotate_right(self, n: u32) -> Self {
380+
(self as $UnsignedT).rotate_right(n) as Self
381+
}
382+
}
386383
/// Reverses the byte order of the integer.
387384
///
388385
/// # Examples
@@ -2012,46 +2009,50 @@ $EndFeature, "
20122009

20132010
#[lang = "i8"]
20142011
impl i8 {
2015-
int_impl! { i8, i8, u8, 8, -128, 127, "", "" }
2012+
int_impl! { i8, i8, u8, 8, -128, 127, "", "", 2, "-0x7e", "0xa" }
20162013
}
20172014

20182015
#[lang = "i16"]
20192016
impl i16 {
2020-
int_impl! { i16, i16, u16, 16, -32768, 32767, "", "" }
2017+
int_impl! { i16, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a" }
20212018
}
20222019

20232020
#[lang = "i32"]
20242021
impl i32 {
2025-
int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, "", "" }
2022+
int_impl! { i32, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301" }
20262023
}
20272024

20282025
#[lang = "i64"]
20292026
impl i64 {
2030-
int_impl! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "" }
2027+
int_impl! { i64, i64, u64, 64, -9223372036854775808, 9223372036854775807, "", "", 12,
2028+
"0xaa00000000006e1", "0x6e10aa" }
20312029
}
20322030

20332031
#[lang = "i128"]
20342032
impl i128 {
20352033
int_impl! { i128, i128, u128, 128, -170141183460469231731687303715884105728,
2036-
170141183460469231731687303715884105727, "", "" }
2034+
170141183460469231731687303715884105727, "", "", 16,
2035+
"0x13f40000000000000000000000004f76", "0x4f7613f4"
2036+
}
20372037
}
20382038

20392039
#[cfg(target_pointer_width = "16")]
20402040
#[lang = "isize"]
20412041
impl isize {
2042-
int_impl! { isize, i16, u16, 16, -32768, 32767, "", "" }
2042+
int_impl! { isize, i16, u16, 16, -32768, 32767, "", "", 4, "-0x5ffd", "0x3a" }
20432043
}
20442044

20452045
#[cfg(target_pointer_width = "32")]
20462046
#[lang = "isize"]
20472047
impl isize {
2048-
int_impl! { isize, i32, u32, 32, -2147483648, 2147483647, "", "" }
2048+
int_impl! { isize, i32, u32, 32, -2147483648, 2147483647, "", "", 8, "0x10000b3", "0xb301" }
20492049
}
20502050

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

20572058
// Emits the correct `cttz` call, depending on the size of the type.
@@ -2069,7 +2070,8 @@ macro_rules! uint_cttz_call {
20692070

20702071
// `Int` + `UnsignedInt` implemented for unsigned integers
20712072
macro_rules! uint_impl {
2072-
($SelfT:ty, $ActualT:ty, $BITS:expr, $MaxV:expr, $Feature:expr, $EndFeature:expr) => {
2073+
($SelfT:ty, $ActualT:ty, $BITS:expr, $MaxV:expr, $Feature:expr, $EndFeature:expr,
2074+
$rot:expr, $rot_op:expr, $rot_result:expr) => {
20732075
doc_comment! {
20742076
concat!("Returns the smallest value that can be represented by this integer type.
20752077
@@ -2210,57 +2212,55 @@ assert_eq!(n.trailing_zeros(), 3);", $EndFeature, "
22102212
}
22112213
}
22122214

2213-
/// Shifts the bits to the left by a specified amount, `n`,
2214-
/// wrapping the truncated bits to the end of the resulting integer.
2215-
///
2216-
/// Please note this isn't the same operation as `<<`!
2217-
///
2218-
/// # Examples
2219-
///
2220-
/// Basic usage:
2221-
///
2222-
/// Please note that this example is shared between integer types.
2223-
/// Which explains why `u64` is used here.
2224-
///
2225-
/// ```
2226-
/// let n = 0x0123456789ABCDEFu64;
2227-
/// let m = 0x3456789ABCDEF012u64;
2228-
///
2229-
/// assert_eq!(n.rotate_left(12), m);
2230-
/// ```
2231-
#[stable(feature = "rust1", since = "1.0.0")]
2232-
#[inline]
2233-
pub fn rotate_left(self, n: u32) -> Self {
2234-
// Protect against undefined behaviour for over-long bit shifts
2235-
let n = n % $BITS;
2236-
(self << n) | (self >> (($BITS - n) % $BITS))
2215+
doc_comment! {
2216+
concat!("Shifts the bits to the left by a specified amount, `n`,
2217+
wrapping the truncated bits to the end of the resulting integer.
2218+
2219+
Please note this isn't the same operation as `<<`!
2220+
2221+
# Examples
2222+
2223+
Basic usage:
2224+
2225+
```
2226+
let n = ", $rot_op, stringify!($SelfT), ";
2227+
let m = ", $rot_result, ";
2228+
2229+
assert_eq!(n.rotate_left(", $rot, "), m);
2230+
```"),
2231+
#[stable(feature = "rust1", since = "1.0.0")]
2232+
#[inline]
2233+
pub fn rotate_left(self, n: u32) -> Self {
2234+
// Protect against undefined behaviour for over-long bit shifts
2235+
let n = n % $BITS;
2236+
(self << n) | (self >> (($BITS - n) % $BITS))
2237+
}
22372238
}
22382239

2239-
/// Shifts the bits to the right by a specified amount, `n`,
2240-
/// wrapping the truncated bits to the beginning of the resulting
2241-
/// integer.
2242-
///
2243-
/// Please note this isn't the same operation as `>>`!
2244-
///
2245-
/// # Examples
2246-
///
2247-
/// Basic usage:
2248-
///
2249-
/// Please note that this example is shared between integer types.
2250-
/// Which explains why `u64` is used here.
2251-
///
2252-
/// ```
2253-
/// let n = 0x0123456789ABCDEFu64;
2254-
/// let m = 0xDEF0123456789ABCu64;
2255-
///
2256-
/// assert_eq!(n.rotate_right(12), m);
2257-
/// ```
2258-
#[stable(feature = "rust1", since = "1.0.0")]
2259-
#[inline]
2260-
pub fn rotate_right(self, n: u32) -> Self {
2261-
// Protect against undefined behaviour for over-long bit shifts
2262-
let n = n % $BITS;
2263-
(self >> n) | (self << (($BITS - n) % $BITS))
2240+
doc_comment! {
2241+
concat!("Shifts the bits to the right by a specified amount, `n`,
2242+
wrapping the truncated bits to the beginning of the resulting
2243+
integer.
2244+
2245+
Please note this isn't the same operation as `>>`!
2246+
2247+
# Examples
2248+
2249+
Basic usage:
2250+
2251+
```
2252+
let n = ", $rot_result, stringify!($SelfT), ";
2253+
let m = ", $rot_op, ";
2254+
2255+
assert_eq!(n.rotate_right(", $rot, "), m);
2256+
```"),
2257+
#[stable(feature = "rust1", since = "1.0.0")]
2258+
#[inline]
2259+
pub fn rotate_right(self, n: u32) -> Self {
2260+
// Protect against undefined behaviour for over-long bit shifts
2261+
let n = n % $BITS;
2262+
(self >> n) | (self << (($BITS - n) % $BITS))
2263+
}
22642264
}
22652265

22662266
/// Reverses the byte order of the integer.
@@ -3621,7 +3621,7 @@ $EndFeature, "
36213621

36223622
#[lang = "u8"]
36233623
impl u8 {
3624-
uint_impl! { u8, u8, 8, 255, "", "" }
3624+
uint_impl! { u8, u8, 8, 255, "", "", 2, "0x82", "0xa" }
36253625

36263626

36273627
/// Checks if the value is within the ASCII range.
@@ -4147,39 +4147,41 @@ impl u8 {
41474147

41484148
#[lang = "u16"]
41494149
impl u16 {
4150-
uint_impl! { u16, u16, 16, 65535, "", "" }
4150+
uint_impl! { u16, u16, 16, 65535, "", "", 4, "0xa003", "0x3a" }
41514151
}
41524152

41534153
#[lang = "u32"]
41544154
impl u32 {
4155-
uint_impl! { u32, u32, 32, 4294967295, "", "" }
4155+
uint_impl! { u32, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301" }
41564156
}
41574157

41584158
#[lang = "u64"]
41594159
impl u64 {
4160-
uint_impl! { u64, u64, 64, 18446744073709551615, "", "" }
4160+
uint_impl! { u64, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1", "0x6e10aa" }
41614161
}
41624162

41634163
#[lang = "u128"]
41644164
impl u128 {
4165-
uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, "", "" }
4165+
uint_impl! { u128, u128, 128, 340282366920938463463374607431768211455, "", "", 16,
4166+
"0x13f40000000000000000000000004f76", "0x4f7613f4" }
41664167
}
41674168

41684169
#[cfg(target_pointer_width = "16")]
41694170
#[lang = "usize"]
41704171
impl usize {
4171-
uint_impl! { usize, u16, 16, 65536, "", "" }
4172+
uint_impl! { usize, u16, 16, 65536, "", "", 4, "0xa003", "0x3a" }
41724173
}
41734174
#[cfg(target_pointer_width = "32")]
41744175
#[lang = "usize"]
41754176
impl usize {
4176-
uint_impl! { usize, u32, 32, 4294967295, "", "" }
4177+
uint_impl! { usize, u32, 32, 4294967295, "", "", 8, "0x10000b3", "0xb301" }
41774178
}
41784179

41794180
#[cfg(target_pointer_width = "64")]
41804181
#[lang = "usize"]
41814182
impl usize {
4182-
uint_impl! { usize, u64, 64, 18446744073709551615, "", "" }
4183+
uint_impl! { usize, u64, 64, 18446744073709551615, "", "", 12, "0xaa00000000006e1",
4184+
"0x6e10aa" }
41834185
}
41844186

41854187
/// A classification of floating point numbers.

0 commit comments

Comments
 (0)