@@ -948,25 +948,55 @@ bitand_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
948948///
949949/// # Examples
950950///
951- /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
952- /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
951+ /// In this example, the `|` operator is lifted to a trivial `Scalar` type.
953952///
954953/// ```
955954/// use std::ops::BitOr;
956955///
957- /// struct Foo;
956+ /// #[derive(Debug, PartialEq)]
957+ /// struct Scalar(bool);
958958///
959- /// impl BitOr for Foo {
960- /// type Output = Foo ;
959+ /// impl BitOr for Scalar {
960+ /// type Output = Self ;
961961///
962- /// fn bitor(self, _rhs: Foo) -> Foo {
963- /// println!("Bitwise Or-ing!");
964- /// self
962+ /// // rhs is the "right-hand side" of the expression `a | b`
963+ /// fn bitor(self, rhs: Self) -> Self {
964+ /// Scalar(self.0 | rhs.0)
965+ /// }
966+ /// }
967+ ///
968+ /// fn main() {
969+ /// assert_eq!(Scalar(true) | Scalar(true), Scalar(true));
970+ /// assert_eq!(Scalar(true) | Scalar(false), Scalar(true));
971+ /// assert_eq!(Scalar(false) | Scalar(true), Scalar(true));
972+ /// assert_eq!(Scalar(false) | Scalar(false), Scalar(false));
973+ /// }
974+ /// ```
975+ ///
976+ /// In this example, the `BitOr` trait is implemented for a `BooleanVector`
977+ /// struct.
978+ ///
979+ /// ```
980+ /// use std::ops::BitOr;
981+ ///
982+ /// #[derive(Debug, PartialEq)]
983+ /// struct BooleanVector(Vec<bool>);
984+ ///
985+ /// impl BitOr for BooleanVector {
986+ /// type Output = Self;
987+ ///
988+ /// fn bitor(self, BooleanVector(rhs): Self) -> Self {
989+ /// let BooleanVector(lhs) = self;
990+ /// assert_eq!(lhs.len(), rhs.len());
991+ /// BooleanVector(lhs.iter().zip(rhs.iter()).map(|(x, y)| *x || *y).collect())
965992/// }
966993/// }
967994///
968995/// fn main() {
969- /// Foo | Foo;
996+ /// let bv1 = BooleanVector(vec![true, true, false, false]);
997+ /// let bv2 = BooleanVector(vec![true, false, true, false]);
998+ /// let expected = BooleanVector(vec![true, true, true, false]);
999+ /// assert_eq!(bv1 | bv2, expected);
9701000/// }
9711001/// ```
9721002#[ lang = "bitor" ]
@@ -1001,25 +1031,58 @@ bitor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
10011031///
10021032/// # Examples
10031033///
1004- /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
1005- /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
1034+ /// In this example, the `^` operator is lifted to a trivial `Scalar` type.
10061035///
10071036/// ```
10081037/// use std::ops::BitXor;
10091038///
1010- /// struct Foo;
1039+ /// #[derive(Debug, PartialEq)]
1040+ /// struct Scalar(bool);
10111041///
1012- /// impl BitXor for Foo {
1013- /// type Output = Foo ;
1042+ /// impl BitXor for Scalar {
1043+ /// type Output = Self ;
10141044///
1015- /// fn bitxor(self, _rhs: Foo) -> Foo {
1016- /// println!("Bitwise Xor-ing!");
1017- /// self
1045+ /// // rhs is the "right-hand side" of the expression `a ^ b`
1046+ /// fn bitxor(self, rhs: Self) -> Self {
1047+ /// Scalar(self.0 ^ rhs.0)
1048+ /// }
1049+ /// }
1050+ ///
1051+ /// fn main() {
1052+ /// assert_eq!(Scalar(true) ^ Scalar(true), Scalar(false));
1053+ /// assert_eq!(Scalar(true) ^ Scalar(false), Scalar(true));
1054+ /// assert_eq!(Scalar(false) ^ Scalar(true), Scalar(true));
1055+ /// assert_eq!(Scalar(false) ^ Scalar(false), Scalar(false));
1056+ /// }
1057+ /// ```
1058+ ///
1059+ /// In this example, the `BitXor` trait is implemented for a `BooleanVector`
1060+ /// struct.
1061+ ///
1062+ /// ```
1063+ /// use std::ops::BitXor;
1064+ ///
1065+ /// #[derive(Debug, PartialEq)]
1066+ /// struct BooleanVector(Vec<bool>);
1067+ ///
1068+ /// impl BitXor for BooleanVector {
1069+ /// type Output = Self;
1070+ ///
1071+ /// fn bitxor(self, BooleanVector(rhs): Self) -> Self {
1072+ /// let BooleanVector(lhs) = self;
1073+ /// assert_eq!(lhs.len(), rhs.len());
1074+ /// BooleanVector(lhs.iter()
1075+ /// .zip(rhs.iter())
1076+ /// .map(|(x, y)| (*x || *y) && !(*x && *y))
1077+ /// .collect())
10181078/// }
10191079/// }
10201080///
10211081/// fn main() {
1022- /// Foo ^ Foo;
1082+ /// let bv1 = BooleanVector(vec![true, true, false, false]);
1083+ /// let bv2 = BooleanVector(vec![true, false, true, false]);
1084+ /// let expected = BooleanVector(vec![false, true, true, false]);
1085+ /// assert_eq!(bv1 ^ bv2, expected);
10231086/// }
10241087/// ```
10251088#[ lang = "bitxor" ]
0 commit comments