@@ -948,25 +948,55 @@ bitand_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
948
948
///
949
949
/// # Examples
950
950
///
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.
953
952
///
954
953
/// ```
955
954
/// use std::ops::BitOr;
956
955
///
957
- /// struct Foo;
956
+ /// #[derive(Debug, PartialEq)]
957
+ /// struct Scalar(bool);
958
958
///
959
- /// impl BitOr for Foo {
960
- /// type Output = Foo ;
959
+ /// impl BitOr for Scalar {
960
+ /// type Output = Self ;
961
961
///
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())
965
992
/// }
966
993
/// }
967
994
///
968
995
/// 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);
970
1000
/// }
971
1001
/// ```
972
1002
#[ lang = "bitor" ]
@@ -1001,25 +1031,58 @@ bitor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
1001
1031
///
1002
1032
/// # Examples
1003
1033
///
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.
1006
1035
///
1007
1036
/// ```
1008
1037
/// use std::ops::BitXor;
1009
1038
///
1010
- /// struct Foo;
1039
+ /// #[derive(Debug, PartialEq)]
1040
+ /// struct Scalar(bool);
1011
1041
///
1012
- /// impl BitXor for Foo {
1013
- /// type Output = Foo ;
1042
+ /// impl BitXor for Scalar {
1043
+ /// type Output = Self ;
1014
1044
///
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())
1018
1078
/// }
1019
1079
/// }
1020
1080
///
1021
1081
/// 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);
1023
1086
/// }
1024
1087
/// ```
1025
1088
#[ lang = "bitxor" ]
0 commit comments