@@ -852,25 +852,54 @@ bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
852852///
853853/// # Examples
854854///
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 .
857857///
858858/// ```
859859/// use std::ops::Shl;
860860///
861- /// struct Foo;
861+ /// #[derive(PartialEq, Debug)]
862+ /// struct Scalar(usize);
862863///
863- /// impl Shl<Foo > for Foo {
864- /// type Output = Foo ;
864+ /// impl Shl<Scalar > for Scalar {
865+ /// type Output = Self ;
865866///
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 }
869897/// }
870898/// }
871899///
872900/// 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] });
874903/// }
875904/// ```
876905#[ lang = "shl" ]
@@ -924,25 +953,54 @@ shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
924953///
925954/// # Examples
926955///
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 .
929958///
930959/// ```
931960/// use std::ops::Shr;
932961///
933- /// struct Foo;
962+ /// #[derive(PartialEq, Debug)]
963+ /// struct Scalar(usize);
934964///
935- /// impl Shr<Foo > for Foo {
936- /// type Output = Foo ;
965+ /// impl Shr<Scalar > for Scalar {
966+ /// type Output = Self ;
937967///
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 }
941998/// }
942999/// }
9431000///
9441001/// 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] });
9461004/// }
9471005/// ```
9481006#[ lang = "shr" ]
0 commit comments