@@ -1034,25 +1034,54 @@ bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
1034
1034
///
1035
1035
/// # Examples
1036
1036
///
1037
- /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
1038
- /// calling `shl`, and therefore, `main` prints `Shifting left!` .
1037
+ /// An implementation of `Shl` that lifts the `<<` operation on integers to a
1038
+ /// `Scalar` struct .
1039
1039
///
1040
1040
/// ```
1041
1041
/// use std::ops::Shl;
1042
1042
///
1043
- /// struct Foo;
1043
+ /// #[derive(PartialEq, Debug)]
1044
+ /// struct Scalar(usize);
1044
1045
///
1045
- /// impl Shl<Foo > for Foo {
1046
- /// type Output = Foo ;
1046
+ /// impl Shl<Scalar > for Scalar {
1047
+ /// type Output = Self ;
1047
1048
///
1048
- /// fn shl(self, _rhs: Foo) -> Foo {
1049
- /// println!("Shifting left!");
1050
- /// self
1049
+ /// fn shl(self, Scalar(rhs): Self) -> Scalar {
1050
+ /// let Scalar(lhs) = self;
1051
+ /// Scalar(lhs << rhs)
1052
+ /// }
1053
+ /// }
1054
+ /// fn main() {
1055
+ /// assert_eq!(Scalar(4) << Scalar(2), Scalar(16));
1056
+ /// }
1057
+ /// ```
1058
+ ///
1059
+ /// An implementation of `Shl` that spins a vector leftward by a given amount.
1060
+ ///
1061
+ /// ```
1062
+ /// use std::ops::Shl;
1063
+ ///
1064
+ /// #[derive(PartialEq, Debug)]
1065
+ /// struct SpinVector<T: Clone> {
1066
+ /// vec: Vec<T>,
1067
+ /// }
1068
+ ///
1069
+ /// impl<T: Clone> Shl<usize> for SpinVector<T> {
1070
+ /// type Output = Self;
1071
+ ///
1072
+ /// fn shl(self, rhs: usize) -> SpinVector<T> {
1073
+ /// // rotate the vector by `rhs` places
1074
+ /// let (a, b) = self.vec.split_at(rhs);
1075
+ /// let mut spun_vector: Vec<T> = vec![];
1076
+ /// spun_vector.extend_from_slice(b);
1077
+ /// spun_vector.extend_from_slice(a);
1078
+ /// SpinVector { vec: spun_vector }
1051
1079
/// }
1052
1080
/// }
1053
1081
///
1054
1082
/// fn main() {
1055
- /// Foo << Foo;
1083
+ /// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } << 2,
1084
+ /// SpinVector { vec: vec![2, 3, 4, 0, 1] });
1056
1085
/// }
1057
1086
/// ```
1058
1087
#[ lang = "shl" ]
@@ -1106,25 +1135,54 @@ shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
1106
1135
///
1107
1136
/// # Examples
1108
1137
///
1109
- /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
1110
- /// calling `shr`, and therefore, `main` prints `Shifting right!` .
1138
+ /// An implementation of `Shr` that lifts the `>>` operation on integers to a
1139
+ /// `Scalar` struct .
1111
1140
///
1112
1141
/// ```
1113
1142
/// use std::ops::Shr;
1114
1143
///
1115
- /// struct Foo;
1144
+ /// #[derive(PartialEq, Debug)]
1145
+ /// struct Scalar(usize);
1116
1146
///
1117
- /// impl Shr<Foo > for Foo {
1118
- /// type Output = Foo ;
1147
+ /// impl Shr<Scalar > for Scalar {
1148
+ /// type Output = Self ;
1119
1149
///
1120
- /// fn shr(self, _rhs: Foo) -> Foo {
1121
- /// println!("Shifting right!");
1122
- /// self
1150
+ /// fn shr(self, Scalar(rhs): Self) -> Scalar {
1151
+ /// let Scalar(lhs) = self;
1152
+ /// Scalar(lhs >> rhs)
1153
+ /// }
1154
+ /// }
1155
+ /// fn main() {
1156
+ /// assert_eq!(Scalar(16) >> Scalar(2), Scalar(4));
1157
+ /// }
1158
+ /// ```
1159
+ ///
1160
+ /// An implementation of `Shr` that spins a vector rightward by a given amount.
1161
+ ///
1162
+ /// ```
1163
+ /// use std::ops::Shr;
1164
+ ///
1165
+ /// #[derive(PartialEq, Debug)]
1166
+ /// struct SpinVector<T: Clone> {
1167
+ /// vec: Vec<T>,
1168
+ /// }
1169
+ ///
1170
+ /// impl<T: Clone> Shr<usize> for SpinVector<T> {
1171
+ /// type Output = Self;
1172
+ ///
1173
+ /// fn shr(self, rhs: usize) -> SpinVector<T> {
1174
+ /// // rotate the vector by `rhs` places
1175
+ /// let (a, b) = self.vec.split_at(self.vec.len() - rhs);
1176
+ /// let mut spun_vector: Vec<T> = vec![];
1177
+ /// spun_vector.extend_from_slice(b);
1178
+ /// spun_vector.extend_from_slice(a);
1179
+ /// SpinVector { vec: spun_vector }
1123
1180
/// }
1124
1181
/// }
1125
1182
///
1126
1183
/// fn main() {
1127
- /// Foo >> Foo;
1184
+ /// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } >> 2,
1185
+ /// SpinVector { vec: vec![3, 4, 0, 1, 2] });
1128
1186
/// }
1129
1187
/// ```
1130
1188
#[ lang = "shr" ]
0 commit comments