Skip to content

Commit b4a6b6b

Browse files
Rollup merge of rust-lang#35863 - matthew-piziak:shl-example, r=steveklabnik
add evocative examples for `Shl` and `Shr` r? @steveklabnik
2 parents d33e191 + ff3a761 commit b4a6b6b

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

src/libcore/ops.rs

+76-18
Original file line numberDiff line numberDiff line change
@@ -1034,25 +1034,54 @@ bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
10341034
///
10351035
/// # Examples
10361036
///
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.
10391039
///
10401040
/// ```
10411041
/// use std::ops::Shl;
10421042
///
1043-
/// struct Foo;
1043+
/// #[derive(PartialEq, Debug)]
1044+
/// struct Scalar(usize);
10441045
///
1045-
/// impl Shl<Foo> for Foo {
1046-
/// type Output = Foo;
1046+
/// impl Shl<Scalar> for Scalar {
1047+
/// type Output = Self;
10471048
///
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 }
10511079
/// }
10521080
/// }
10531081
///
10541082
/// 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] });
10561085
/// }
10571086
/// ```
10581087
#[lang = "shl"]
@@ -1106,25 +1135,54 @@ shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
11061135
///
11071136
/// # Examples
11081137
///
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.
11111140
///
11121141
/// ```
11131142
/// use std::ops::Shr;
11141143
///
1115-
/// struct Foo;
1144+
/// #[derive(PartialEq, Debug)]
1145+
/// struct Scalar(usize);
11161146
///
1117-
/// impl Shr<Foo> for Foo {
1118-
/// type Output = Foo;
1147+
/// impl Shr<Scalar> for Scalar {
1148+
/// type Output = Self;
11191149
///
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 }
11231180
/// }
11241181
/// }
11251182
///
11261183
/// 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] });
11281186
/// }
11291187
/// ```
11301188
#[lang = "shr"]

0 commit comments

Comments
 (0)