@@ -989,25 +989,54 @@ bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
989
989
///
990
990
/// # Examples
991
991
///
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 .
994
994
///
995
995
/// ```
996
996
/// use std::ops::Shl;
997
997
///
998
- /// struct Foo;
998
+ /// #[derive(PartialEq, Debug)]
999
+ /// struct Scalar(usize);
999
1000
///
1000
- /// impl Shl<Foo > for Foo {
1001
- /// type Output = Foo ;
1001
+ /// impl Shl<Scalar > for Scalar {
1002
+ /// type Output = Self ;
1002
1003
///
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 }
1006
1034
/// }
1007
1035
/// }
1008
1036
///
1009
1037
/// 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] });
1011
1040
/// }
1012
1041
/// ```
1013
1042
#[ lang = "shl" ]
@@ -1061,25 +1090,54 @@ shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
1061
1090
///
1062
1091
/// # Examples
1063
1092
///
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 .
1066
1095
///
1067
1096
/// ```
1068
1097
/// use std::ops::Shr;
1069
1098
///
1070
- /// struct Foo;
1099
+ /// #[derive(PartialEq, Debug)]
1100
+ /// struct Scalar(usize);
1071
1101
///
1072
- /// impl Shr<Foo > for Foo {
1073
- /// type Output = Foo ;
1102
+ /// impl Shr<Scalar > for Scalar {
1103
+ /// type Output = Self ;
1074
1104
///
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 }
1078
1135
/// }
1079
1136
/// }
1080
1137
///
1081
1138
/// 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] });
1083
1141
/// }
1084
1142
/// ```
1085
1143
#[ lang = "shr" ]
0 commit comments