Skip to content

Commit fd783ae

Browse files
committed
core: Improve the documentation for funnel shifts
Shorten the summary line, cover shifts by zeros, rotates, and panics in examples, and turn the "Panics" section into a complete sentence.
1 parent 25db8d2 commit fd783ae

File tree

1 file changed

+68
-24
lines changed

1 file changed

+68
-24
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -415,29 +415,51 @@ macro_rules! uint_impl {
415415
return intrinsics::rotate_right(self, n);
416416
}
417417

418-
/// Performs a left funnel shift (concatenates `self` with `low`, with `self`
419-
/// making up the high half, then shifts the combined value left
420-
/// by `n`, and the high half is extracted to produce the result).
418+
/// Performs a left funnel shift.
421419
///
422-
/// Please note this isn't the same operation as the `<<` shifting operator or
423-
/// [`rotate_left`](Self::rotate_left), although `a.funnel_shl(a, n)` is *equivalent*
424-
/// to `a.rotate_left(n)`.
420+
/// This operation can be thought of as concatenating `self` and `right` into an
421+
/// integer twice the size of
422+
#[doc = concat!("`", stringify!($SelfT) , "`,")]
423+
/// performing a left shift by `n`, and returning the **upper half** of the result.
424+
///
425+
/// The name comes from "funneling" a wider integer to a narrower integer.
425426
///
426427
/// # Panics
427428
///
428-
/// If `n` is greater than or equal to the number of bits in `self`
429+
/// This function will panic if `n` is greater than or equal to the number of
430+
/// bits in `self`.
429431
///
430432
/// # Examples
431433
///
432-
/// Basic usage:
434+
/// ```
435+
/// #![feature(funnel_shifts)]
436+
///
437+
#[doc = concat!("let a = ", $rot_op, "_", stringify!($SelfT), ";")]
438+
#[doc = concat!("let b = ", $fsh_op, "_", stringify!($SelfT), ";")]
439+
///
440+
#[doc = concat!("assert_eq!(a.funnel_shl(b, ", $rot, "), ", $fshl_result, ");")]
433441
///
442+
/// // Using zeros as the right operand acts as a normal shift
443+
#[doc = concat!("assert_eq!(a.funnel_shl(0, ", $rot, "), a << ", $rot, ");")]
444+
///
445+
/// // Shifting by 0 returns `self` unchanged
446+
#[doc = concat!("assert_eq!(a.funnel_shl(0, 0), a);")]
447+
///
448+
/// // Using the same value as the right operand acts as a rotate
449+
#[doc = concat!("assert_eq!(a.funnel_shl(a, ", $rot, "), a.rotate_left(", $rot, "));")]
434450
/// ```
451+
///
452+
/// Note that while `funnel_shl` can act as a rotate, it does not allow for
453+
/// rotating by an unbounded amount like [`rotate_left`](Self::rotate_left) does:
454+
///
455+
/// ```should_panic
435456
/// #![feature(funnel_shifts)]
436-
#[doc = concat!("let a = ", $rot_op, stringify!($SelfT), ";")]
437-
#[doc = concat!("let b = ", $fsh_op, stringify!($SelfT), ";")]
438-
#[doc = concat!("let m = ", $fshl_result, ";")]
439457
///
440-
#[doc = concat!("assert_eq!(a.funnel_shl(b, ", $rot, "), m);")]
458+
#[doc = concat!("let a = ", stringify!($SelfT), "::MAX;")]
459+
/// // Okay
460+
#[doc = concat!("let _ = a.rotate_left(", stringify!($SelfT), "::BITS);")]
461+
/// // Panics
462+
#[doc = concat!("let _ = a.funnel_shl(a, ", stringify!($SelfT), "::BITS);")]
441463
/// ```
442464
#[rustc_const_unstable(feature = "funnel_shifts", issue = "145686")]
443465
#[unstable(feature = "funnel_shifts", issue = "145686")]
@@ -450,29 +472,51 @@ macro_rules! uint_impl {
450472
unsafe { intrinsics::unchecked_funnel_shl(self, right, n) }
451473
}
452474

453-
/// Performs a right funnel shift (concatenates `self` and `right`, with `self`
454-
/// making up the high half, then shifts the combined value right
455-
/// by `n`, and the low half is extracted to produce the result).
475+
/// Performs a right funnel shift.
456476
///
457-
/// Please note this isn't the same operation as the `>>` shifting operator or
458-
/// [`rotate_right`](Self::rotate_right), although `a.funnel_shr(a, n)` is *equivalent*
459-
/// to `a.rotate_right(n)`.
477+
/// This operation can be thought of as concatenating `self` and `right` into an
478+
/// integer twice the size of
479+
#[doc = concat!("`", stringify!($SelfT) , "`,")]
480+
/// performing a right shift by `n`, and returning the **lower half** of the result.
481+
///
482+
/// The name comes from "funneling" a wider integer to a narrower integer.
460483
///
461484
/// # Panics
462485
///
463-
/// If `n` is greater than or equal to the number of bits in `self`
486+
/// This function will panic if `n` is greater than or equal to the number of
487+
/// bits in `self`.
464488
///
465489
/// # Examples
466490
///
467-
/// Basic usage:
491+
/// ```
492+
/// #![feature(funnel_shifts)]
493+
///
494+
#[doc = concat!("let a = ", $rot_op, "_", stringify!($SelfT), ";")]
495+
#[doc = concat!("let b = ", $fsh_op, "_", stringify!($SelfT), ";")]
496+
///
497+
#[doc = concat!("assert_eq!(a.funnel_shr(b, ", $rot, "), ", $fshr_result, ");")]
468498
///
499+
/// // Using zeros as the high operand acts as a normal shift
500+
#[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".funnel_shr(a, ", $rot, "), a >> ", $rot, ");")]
501+
///
502+
/// // Shifting by 0 returns `right` unchanged
503+
#[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".funnel_shr(a, 0), a);")]
504+
///
505+
/// // Using the same value as the right operand acts as a rotate
506+
#[doc = concat!("assert_eq!(a.funnel_shr(a, ", $rot, "), a.rotate_right(", $rot, "));")]
469507
/// ```
508+
///
509+
/// Note that while `funnel_shr` can act as a rotate, it does not allow for
510+
/// rotating by an unbounded amount like [`rotate_right`](Self::rotate_right) does:
511+
///
512+
/// ```should_panic
470513
/// #![feature(funnel_shifts)]
471-
#[doc = concat!("let a = ", $rot_op, stringify!($SelfT), ";")]
472-
#[doc = concat!("let b = ", $fsh_op, stringify!($SelfT), ";")]
473-
#[doc = concat!("let m = ", $fshr_result, ";")]
474514
///
475-
#[doc = concat!("assert_eq!(a.funnel_shr(b, ", $rot, "), m);")]
515+
#[doc = concat!("let a = ", stringify!($SelfT), "::MAX;")]
516+
/// // Okay
517+
#[doc = concat!("let _ = a.rotate_right(", stringify!($SelfT), "::BITS);")]
518+
/// // Panics
519+
#[doc = concat!("let _ = a.funnel_shr(a, ", stringify!($SelfT), "::BITS);")]
476520
/// ```
477521
#[rustc_const_unstable(feature = "funnel_shifts", issue = "145686")]
478522
#[unstable(feature = "funnel_shifts", issue = "145686")]

0 commit comments

Comments
 (0)