@@ -807,25 +807,55 @@ bitand_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
807
807
///
808
808
/// # Examples
809
809
///
810
- /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
811
- /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
810
+ /// In this example, the `|` operator is lifted to a trivial `Scalar` type.
812
811
///
813
812
/// ```
814
813
/// use std::ops::BitOr;
815
814
///
816
- /// struct Foo;
815
+ /// #[derive(Debug, PartialEq)]
816
+ /// struct Scalar(bool);
817
817
///
818
- /// impl BitOr for Foo {
819
- /// type Output = Foo ;
818
+ /// impl BitOr for Scalar {
819
+ /// type Output = Self ;
820
820
///
821
- /// fn bitor(self, _rhs: Foo) -> Foo {
822
- /// println!("Bitwise Or-ing!");
823
- /// self
821
+ /// // rhs is the "right-hand side" of the expression `a | b`
822
+ /// fn bitor(self, rhs: Self) -> Self {
823
+ /// Scalar(self.0 | rhs.0)
824
+ /// }
825
+ /// }
826
+ ///
827
+ /// fn main() {
828
+ /// assert_eq!(Scalar(true) | Scalar(true), Scalar(true));
829
+ /// assert_eq!(Scalar(true) | Scalar(false), Scalar(true));
830
+ /// assert_eq!(Scalar(false) | Scalar(true), Scalar(true));
831
+ /// assert_eq!(Scalar(false) | Scalar(false), Scalar(false));
832
+ /// }
833
+ /// ```
834
+ ///
835
+ /// In this example, the `BitOr` trait is implemented for a `BooleanVector`
836
+ /// struct.
837
+ ///
838
+ /// ```
839
+ /// use std::ops::BitOr;
840
+ ///
841
+ /// #[derive(Debug, PartialEq)]
842
+ /// struct BooleanVector(Vec<bool>);
843
+ ///
844
+ /// impl BitOr for BooleanVector {
845
+ /// type Output = Self;
846
+ ///
847
+ /// fn bitor(self, BooleanVector(rhs): Self) -> Self {
848
+ /// let BooleanVector(lhs) = self;
849
+ /// assert_eq!(lhs.len(), rhs.len());
850
+ /// BooleanVector(lhs.iter().zip(rhs.iter()).map(|(x, y)| *x || *y).collect())
824
851
/// }
825
852
/// }
826
853
///
827
854
/// fn main() {
828
- /// Foo | Foo;
855
+ /// let bv1 = BooleanVector(vec![true, true, false, false]);
856
+ /// let bv2 = BooleanVector(vec![true, false, true, false]);
857
+ /// let expected = BooleanVector(vec![true, true, true, false]);
858
+ /// assert_eq!(bv1 | bv2, expected);
829
859
/// }
830
860
/// ```
831
861
#[ lang = "bitor" ]
@@ -860,25 +890,58 @@ bitor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
860
890
///
861
891
/// # Examples
862
892
///
863
- /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
864
- /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
893
+ /// In this example, the `^` operator is lifted to a trivial `Scalar` type.
865
894
///
866
895
/// ```
867
896
/// use std::ops::BitXor;
868
897
///
869
- /// struct Foo;
898
+ /// #[derive(Debug, PartialEq)]
899
+ /// struct Scalar(bool);
870
900
///
871
- /// impl BitXor for Foo {
872
- /// type Output = Foo ;
901
+ /// impl BitXor for Scalar {
902
+ /// type Output = Self ;
873
903
///
874
- /// fn bitxor(self, _rhs: Foo) -> Foo {
875
- /// println!("Bitwise Xor-ing!");
876
- /// self
904
+ /// // rhs is the "right-hand side" of the expression `a ^ b`
905
+ /// fn bitxor(self, rhs: Self) -> Self {
906
+ /// Scalar(self.0 ^ rhs.0)
907
+ /// }
908
+ /// }
909
+ ///
910
+ /// fn main() {
911
+ /// assert_eq!(Scalar(true) ^ Scalar(true), Scalar(false));
912
+ /// assert_eq!(Scalar(true) ^ Scalar(false), Scalar(true));
913
+ /// assert_eq!(Scalar(false) ^ Scalar(true), Scalar(true));
914
+ /// assert_eq!(Scalar(false) ^ Scalar(false), Scalar(false));
915
+ /// }
916
+ /// ```
917
+ ///
918
+ /// In this example, the `BitXor` trait is implemented for a `BooleanVector`
919
+ /// struct.
920
+ ///
921
+ /// ```
922
+ /// use std::ops::BitXor;
923
+ ///
924
+ /// #[derive(Debug, PartialEq)]
925
+ /// struct BooleanVector(Vec<bool>);
926
+ ///
927
+ /// impl BitXor for BooleanVector {
928
+ /// type Output = Self;
929
+ ///
930
+ /// fn bitxor(self, BooleanVector(rhs): Self) -> Self {
931
+ /// let BooleanVector(lhs) = self;
932
+ /// assert_eq!(lhs.len(), rhs.len());
933
+ /// BooleanVector(lhs.iter()
934
+ /// .zip(rhs.iter())
935
+ /// .map(|(x, y)| (*x || *y) && !(*x && *y))
936
+ /// .collect())
877
937
/// }
878
938
/// }
879
939
///
880
940
/// fn main() {
881
- /// Foo ^ Foo;
941
+ /// let bv1 = BooleanVector(vec![true, true, false, false]);
942
+ /// let bv2 = BooleanVector(vec![true, false, true, false]);
943
+ /// let expected = BooleanVector(vec![false, true, true, false]);
944
+ /// assert_eq!(bv1 ^ bv2, expected);
882
945
/// }
883
946
/// ```
884
947
#[ lang = "bitxor" ]
0 commit comments