Skip to content

Commit 5729fc6

Browse files
authored
Rollup merge of rust-lang#35863 - matthew-piziak:shl-example, r=steveklabnik
add evocative examples for `Shl` and `Shr` r? @steveklabnik
2 parents 04a53e5 + ff3a761 commit 5729fc6

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

src/libcore/ops.rs

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -989,25 +989,54 @@ bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
989989
///
990990
/// # Examples
991991
///
992-
/// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
993-
/// calling `shl`, and therefore, `main` prints `Shifting left!`.
992+
/// An implementation of `Shl` that lifts the `<<` operation on integers to a
993+
/// `Scalar` struct.
994994
///
995995
/// ```
996996
/// use std::ops::Shl;
997997
///
998-
/// struct Foo;
998+
/// #[derive(PartialEq, Debug)]
999+
/// struct Scalar(usize);
9991000
///
1000-
/// impl Shl<Foo> for Foo {
1001-
/// type Output = Foo;
1001+
/// impl Shl<Scalar> for Scalar {
1002+
/// type Output = Self;
10021003
///
1003-
/// fn shl(self, _rhs: Foo) -> Foo {
1004-
/// println!("Shifting left!");
1005-
/// self
1004+
/// fn shl(self, Scalar(rhs): Self) -> Scalar {
1005+
/// let Scalar(lhs) = self;
1006+
/// Scalar(lhs << rhs)
1007+
/// }
1008+
/// }
1009+
/// fn main() {
1010+
/// assert_eq!(Scalar(4) << Scalar(2), Scalar(16));
1011+
/// }
1012+
/// ```
1013+
///
1014+
/// An implementation of `Shl` that spins a vector leftward by a given amount.
1015+
///
1016+
/// ```
1017+
/// use std::ops::Shl;
1018+
///
1019+
/// #[derive(PartialEq, Debug)]
1020+
/// struct SpinVector<T: Clone> {
1021+
/// vec: Vec<T>,
1022+
/// }
1023+
///
1024+
/// impl<T: Clone> Shl<usize> for SpinVector<T> {
1025+
/// type Output = Self;
1026+
///
1027+
/// fn shl(self, rhs: usize) -> SpinVector<T> {
1028+
/// // rotate the vector by `rhs` places
1029+
/// let (a, b) = self.vec.split_at(rhs);
1030+
/// let mut spun_vector: Vec<T> = vec![];
1031+
/// spun_vector.extend_from_slice(b);
1032+
/// spun_vector.extend_from_slice(a);
1033+
/// SpinVector { vec: spun_vector }
10061034
/// }
10071035
/// }
10081036
///
10091037
/// fn main() {
1010-
/// Foo << Foo;
1038+
/// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } << 2,
1039+
/// SpinVector { vec: vec![2, 3, 4, 0, 1] });
10111040
/// }
10121041
/// ```
10131042
#[lang = "shl"]
@@ -1061,25 +1090,54 @@ shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
10611090
///
10621091
/// # Examples
10631092
///
1064-
/// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
1065-
/// calling `shr`, and therefore, `main` prints `Shifting right!`.
1093+
/// An implementation of `Shr` that lifts the `>>` operation on integers to a
1094+
/// `Scalar` struct.
10661095
///
10671096
/// ```
10681097
/// use std::ops::Shr;
10691098
///
1070-
/// struct Foo;
1099+
/// #[derive(PartialEq, Debug)]
1100+
/// struct Scalar(usize);
10711101
///
1072-
/// impl Shr<Foo> for Foo {
1073-
/// type Output = Foo;
1102+
/// impl Shr<Scalar> for Scalar {
1103+
/// type Output = Self;
10741104
///
1075-
/// fn shr(self, _rhs: Foo) -> Foo {
1076-
/// println!("Shifting right!");
1077-
/// self
1105+
/// fn shr(self, Scalar(rhs): Self) -> Scalar {
1106+
/// let Scalar(lhs) = self;
1107+
/// Scalar(lhs >> rhs)
1108+
/// }
1109+
/// }
1110+
/// fn main() {
1111+
/// assert_eq!(Scalar(16) >> Scalar(2), Scalar(4));
1112+
/// }
1113+
/// ```
1114+
///
1115+
/// An implementation of `Shr` that spins a vector rightward by a given amount.
1116+
///
1117+
/// ```
1118+
/// use std::ops::Shr;
1119+
///
1120+
/// #[derive(PartialEq, Debug)]
1121+
/// struct SpinVector<T: Clone> {
1122+
/// vec: Vec<T>,
1123+
/// }
1124+
///
1125+
/// impl<T: Clone> Shr<usize> for SpinVector<T> {
1126+
/// type Output = Self;
1127+
///
1128+
/// fn shr(self, rhs: usize) -> SpinVector<T> {
1129+
/// // rotate the vector by `rhs` places
1130+
/// let (a, b) = self.vec.split_at(self.vec.len() - rhs);
1131+
/// let mut spun_vector: Vec<T> = vec![];
1132+
/// spun_vector.extend_from_slice(b);
1133+
/// spun_vector.extend_from_slice(a);
1134+
/// SpinVector { vec: spun_vector }
10781135
/// }
10791136
/// }
10801137
///
10811138
/// fn main() {
1082-
/// Foo >> Foo;
1139+
/// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } >> 2,
1140+
/// SpinVector { vec: vec![3, 4, 0, 1, 2] });
10831141
/// }
10841142
/// ```
10851143
#[lang = "shr"]

0 commit comments

Comments
 (0)