@@ -852,25 +852,54 @@ bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
852
852
///
853
853
/// # Examples
854
854
///
855
- /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
856
- /// calling `shl`, and therefore, `main` prints `Shifting left!` .
855
+ /// An implementation of `Shl` that lifts the `<<` operation on integers to a
856
+ /// `Scalar` struct .
857
857
///
858
858
/// ```
859
859
/// use std::ops::Shl;
860
860
///
861
- /// struct Foo;
861
+ /// #[derive(PartialEq, Debug)]
862
+ /// struct Scalar(usize);
862
863
///
863
- /// impl Shl<Foo > for Foo {
864
- /// type Output = Foo ;
864
+ /// impl Shl<Scalar > for Scalar {
865
+ /// type Output = Self ;
865
866
///
866
- /// fn shl(self, _rhs: Foo) -> Foo {
867
- /// println!("Shifting left!");
868
- /// self
867
+ /// fn shl(self, Scalar(rhs): Self) -> Scalar {
868
+ /// let Scalar(lhs) = self;
869
+ /// Scalar(lhs << rhs)
870
+ /// }
871
+ /// }
872
+ /// fn main() {
873
+ /// assert_eq!(Scalar(4) << Scalar(2), Scalar(16));
874
+ /// }
875
+ /// ```
876
+ ///
877
+ /// An implementation of `Shl` that spins a vector leftward by a given amount.
878
+ ///
879
+ /// ```
880
+ /// use std::ops::Shl;
881
+ ///
882
+ /// #[derive(PartialEq, Debug)]
883
+ /// struct SpinVector<T: Clone> {
884
+ /// vec: Vec<T>,
885
+ /// }
886
+ ///
887
+ /// impl<T: Clone> Shl<usize> for SpinVector<T> {
888
+ /// type Output = Self;
889
+ ///
890
+ /// fn shl(self, rhs: usize) -> SpinVector<T> {
891
+ /// // rotate the vector by `rhs` places
892
+ /// let (a, b) = self.vec.split_at(rhs);
893
+ /// let mut spun_vector: Vec<T> = vec![];
894
+ /// spun_vector.extend_from_slice(b);
895
+ /// spun_vector.extend_from_slice(a);
896
+ /// SpinVector { vec: spun_vector }
869
897
/// }
870
898
/// }
871
899
///
872
900
/// fn main() {
873
- /// Foo << Foo;
901
+ /// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } << 2,
902
+ /// SpinVector { vec: vec![2, 3, 4, 0, 1] });
874
903
/// }
875
904
/// ```
876
905
#[ lang = "shl" ]
@@ -924,25 +953,54 @@ shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
924
953
///
925
954
/// # Examples
926
955
///
927
- /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
928
- /// calling `shr`, and therefore, `main` prints `Shifting right!` .
956
+ /// An implementation of `Shr` that lifts the `>>` operation on integers to a
957
+ /// `Scalar` struct .
929
958
///
930
959
/// ```
931
960
/// use std::ops::Shr;
932
961
///
933
- /// struct Foo;
962
+ /// #[derive(PartialEq, Debug)]
963
+ /// struct Scalar(usize);
934
964
///
935
- /// impl Shr<Foo > for Foo {
936
- /// type Output = Foo ;
965
+ /// impl Shr<Scalar > for Scalar {
966
+ /// type Output = Self ;
937
967
///
938
- /// fn shr(self, _rhs: Foo) -> Foo {
939
- /// println!("Shifting right!");
940
- /// self
968
+ /// fn shr(self, Scalar(rhs): Self) -> Scalar {
969
+ /// let Scalar(lhs) = self;
970
+ /// Scalar(lhs >> rhs)
971
+ /// }
972
+ /// }
973
+ /// fn main() {
974
+ /// assert_eq!(Scalar(16) >> Scalar(2), Scalar(4));
975
+ /// }
976
+ /// ```
977
+ ///
978
+ /// An implementation of `Shr` that spins a vector rightward by a given amount.
979
+ ///
980
+ /// ```
981
+ /// use std::ops::Shr;
982
+ ///
983
+ /// #[derive(PartialEq, Debug)]
984
+ /// struct SpinVector<T: Clone> {
985
+ /// vec: Vec<T>,
986
+ /// }
987
+ ///
988
+ /// impl<T: Clone> Shr<usize> for SpinVector<T> {
989
+ /// type Output = Self;
990
+ ///
991
+ /// fn shr(self, rhs: usize) -> SpinVector<T> {
992
+ /// // rotate the vector by `rhs` places
993
+ /// let (a, b) = self.vec.split_at(self.vec.len() - rhs);
994
+ /// let mut spun_vector: Vec<T> = vec![];
995
+ /// spun_vector.extend_from_slice(b);
996
+ /// spun_vector.extend_from_slice(a);
997
+ /// SpinVector { vec: spun_vector }
941
998
/// }
942
999
/// }
943
1000
///
944
1001
/// fn main() {
945
- /// Foo >> Foo;
1002
+ /// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } >> 2,
1003
+ /// SpinVector { vec: vec![3, 4, 0, 1, 2] });
946
1004
/// }
947
1005
/// ```
948
1006
#[ lang = "shr" ]
0 commit comments