@@ -987,25 +987,54 @@ bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
987
987
///
988
988
/// # Examples
989
989
///
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 .
992
992
///
993
993
/// ```
994
994
/// use std::ops::Shl;
995
995
///
996
- /// struct Foo;
996
+ /// #[derive(PartialEq, Debug)]
997
+ /// struct Scalar(usize);
997
998
///
998
- /// impl Shl<Foo > for Foo {
999
- /// type Output = Foo ;
999
+ /// impl Shl<Scalar > for Scalar {
1000
+ /// type Output = Self ;
1000
1001
///
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 }
1004
1032
/// }
1005
1033
/// }
1006
1034
///
1007
1035
/// 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] });
1009
1038
/// }
1010
1039
/// ```
1011
1040
#[ lang = "shl" ]
@@ -1059,25 +1088,54 @@ shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
1059
1088
///
1060
1089
/// # Examples
1061
1090
///
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 .
1064
1093
///
1065
1094
/// ```
1066
1095
/// use std::ops::Shr;
1067
1096
///
1068
- /// struct Foo;
1097
+ /// #[derive(PartialEq, Debug)]
1098
+ /// struct Scalar(usize);
1069
1099
///
1070
- /// impl Shr<Foo > for Foo {
1071
- /// type Output = Foo ;
1100
+ /// impl Shr<Scalar > for Scalar {
1101
+ /// type Output = Self ;
1072
1102
///
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 }
1076
1133
/// }
1077
1134
/// }
1078
1135
///
1079
1136
/// 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] });
1081
1139
/// }
1082
1140
/// ```
1083
1141
#[ lang = "shr" ]
0 commit comments