Skip to content

Commit d743df3

Browse files
authored
Rollup merge of #35863 - matthew-piziak:shl-example, r=steveklabnik
add evocative examples for `Shl` and `Shr` r? @steveklabnik
2 parents 0d1a4db + ff3a761 commit d743df3

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

src/libcore/ops.rs

+76-18
Original file line numberDiff line numberDiff line change
@@ -987,25 +987,54 @@ bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
987987
///
988988
/// # Examples
989989
///
990-
/// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
991-
/// calling `shl`, and therefore, `main` prints `Shifting left!`.
990+
/// An implementation of `Shl` that lifts the `<<` operation on integers to a
991+
/// `Scalar` struct.
992992
///
993993
/// ```
994994
/// use std::ops::Shl;
995995
///
996-
/// struct Foo;
996+
/// #[derive(PartialEq, Debug)]
997+
/// struct Scalar(usize);
997998
///
998-
/// impl Shl<Foo> for Foo {
999-
/// type Output = Foo;
999+
/// impl Shl<Scalar> for Scalar {
1000+
/// type Output = Self;
10001001
///
1001-
/// fn shl(self, _rhs: Foo) -> Foo {
1002-
/// println!("Shifting left!");
1003-
/// self
1002+
/// fn shl(self, Scalar(rhs): Self) -> Scalar {
1003+
/// let Scalar(lhs) = self;
1004+
/// Scalar(lhs << rhs)
1005+
/// }
1006+
/// }
1007+
/// fn main() {
1008+
/// assert_eq!(Scalar(4) << Scalar(2), Scalar(16));
1009+
/// }
1010+
/// ```
1011+
///
1012+
/// An implementation of `Shl` that spins a vector leftward by a given amount.
1013+
///
1014+
/// ```
1015+
/// use std::ops::Shl;
1016+
///
1017+
/// #[derive(PartialEq, Debug)]
1018+
/// struct SpinVector<T: Clone> {
1019+
/// vec: Vec<T>,
1020+
/// }
1021+
///
1022+
/// impl<T: Clone> Shl<usize> for SpinVector<T> {
1023+
/// type Output = Self;
1024+
///
1025+
/// fn shl(self, rhs: usize) -> SpinVector<T> {
1026+
/// // rotate the vector by `rhs` places
1027+
/// let (a, b) = self.vec.split_at(rhs);
1028+
/// let mut spun_vector: Vec<T> = vec![];
1029+
/// spun_vector.extend_from_slice(b);
1030+
/// spun_vector.extend_from_slice(a);
1031+
/// SpinVector { vec: spun_vector }
10041032
/// }
10051033
/// }
10061034
///
10071035
/// fn main() {
1008-
/// Foo << Foo;
1036+
/// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } << 2,
1037+
/// SpinVector { vec: vec![2, 3, 4, 0, 1] });
10091038
/// }
10101039
/// ```
10111040
#[lang = "shl"]
@@ -1059,25 +1088,54 @@ shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
10591088
///
10601089
/// # Examples
10611090
///
1062-
/// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
1063-
/// calling `shr`, and therefore, `main` prints `Shifting right!`.
1091+
/// An implementation of `Shr` that lifts the `>>` operation on integers to a
1092+
/// `Scalar` struct.
10641093
///
10651094
/// ```
10661095
/// use std::ops::Shr;
10671096
///
1068-
/// struct Foo;
1097+
/// #[derive(PartialEq, Debug)]
1098+
/// struct Scalar(usize);
10691099
///
1070-
/// impl Shr<Foo> for Foo {
1071-
/// type Output = Foo;
1100+
/// impl Shr<Scalar> for Scalar {
1101+
/// type Output = Self;
10721102
///
1073-
/// fn shr(self, _rhs: Foo) -> Foo {
1074-
/// println!("Shifting right!");
1075-
/// self
1103+
/// fn shr(self, Scalar(rhs): Self) -> Scalar {
1104+
/// let Scalar(lhs) = self;
1105+
/// Scalar(lhs >> rhs)
1106+
/// }
1107+
/// }
1108+
/// fn main() {
1109+
/// assert_eq!(Scalar(16) >> Scalar(2), Scalar(4));
1110+
/// }
1111+
/// ```
1112+
///
1113+
/// An implementation of `Shr` that spins a vector rightward by a given amount.
1114+
///
1115+
/// ```
1116+
/// use std::ops::Shr;
1117+
///
1118+
/// #[derive(PartialEq, Debug)]
1119+
/// struct SpinVector<T: Clone> {
1120+
/// vec: Vec<T>,
1121+
/// }
1122+
///
1123+
/// impl<T: Clone> Shr<usize> for SpinVector<T> {
1124+
/// type Output = Self;
1125+
///
1126+
/// fn shr(self, rhs: usize) -> SpinVector<T> {
1127+
/// // rotate the vector by `rhs` places
1128+
/// let (a, b) = self.vec.split_at(self.vec.len() - rhs);
1129+
/// let mut spun_vector: Vec<T> = vec![];
1130+
/// spun_vector.extend_from_slice(b);
1131+
/// spun_vector.extend_from_slice(a);
1132+
/// SpinVector { vec: spun_vector }
10761133
/// }
10771134
/// }
10781135
///
10791136
/// fn main() {
1080-
/// Foo >> Foo;
1137+
/// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } >> 2,
1138+
/// SpinVector { vec: vec![3, 4, 0, 1, 2] });
10811139
/// }
10821140
/// ```
10831141
#[lang = "shr"]

0 commit comments

Comments
 (0)