Skip to content

Commit 8ca9fa1

Browse files
add evocative examples for BitOr and BitXor
These are exactly equivalent to PR #35809, with one caveat: I do not believe there is a non-bitwise binary "xor" operator in Rust, so here it's expressed as (a || b) && !(a && b). r? @GuillaumeGomez improved documentation a la PR #35993
1 parent 43204ff commit 8ca9fa1

File tree

1 file changed

+81
-18
lines changed

1 file changed

+81
-18
lines changed

src/libcore/ops.rs

+81-18
Original file line numberDiff line numberDiff line change
@@ -807,25 +807,55 @@ bitand_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
807807
///
808808
/// # Examples
809809
///
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.
812811
///
813812
/// ```
814813
/// use std::ops::BitOr;
815814
///
816-
/// struct Foo;
815+
/// #[derive(Debug, PartialEq)]
816+
/// struct Scalar(bool);
817817
///
818-
/// impl BitOr for Foo {
819-
/// type Output = Foo;
818+
/// impl BitOr for Scalar {
819+
/// type Output = Self;
820820
///
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())
824851
/// }
825852
/// }
826853
///
827854
/// 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);
829859
/// }
830860
/// ```
831861
#[lang = "bitor"]
@@ -860,25 +890,58 @@ bitor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
860890
///
861891
/// # Examples
862892
///
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.
865894
///
866895
/// ```
867896
/// use std::ops::BitXor;
868897
///
869-
/// struct Foo;
898+
/// #[derive(Debug, PartialEq)]
899+
/// struct Scalar(bool);
870900
///
871-
/// impl BitXor for Foo {
872-
/// type Output = Foo;
901+
/// impl BitXor for Scalar {
902+
/// type Output = Self;
873903
///
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())
877937
/// }
878938
/// }
879939
///
880940
/// 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);
882945
/// }
883946
/// ```
884947
#[lang = "bitxor"]

0 commit comments

Comments
 (0)