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
This commit is contained in:
Matthew Piziak 2016-08-23 11:23:42 -04:00
parent 43204fff5d
commit 8ca9fa11f9

View File

@ -807,25 +807,55 @@ bitand_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
/// ///
/// # Examples /// # Examples
/// ///
/// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up /// In this example, the `|` operator is lifted to a trivial `Scalar` type.
/// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
/// ///
/// ``` /// ```
/// use std::ops::BitOr; /// use std::ops::BitOr;
/// ///
/// struct Foo; /// #[derive(Debug, PartialEq)]
/// struct Scalar(bool);
/// ///
/// impl BitOr for Foo { /// impl BitOr for Scalar {
/// type Output = Foo; /// type Output = Self;
/// ///
/// fn bitor(self, _rhs: Foo) -> Foo { /// // rhs is the "right-hand side" of the expression `a | b`
/// println!("Bitwise Or-ing!"); /// fn bitor(self, rhs: Self) -> Self {
/// self /// Scalar(self.0 | rhs.0)
/// } /// }
/// } /// }
/// ///
/// fn main() { /// fn main() {
/// Foo | Foo; /// assert_eq!(Scalar(true) | Scalar(true), Scalar(true));
/// assert_eq!(Scalar(true) | Scalar(false), Scalar(true));
/// assert_eq!(Scalar(false) | Scalar(true), Scalar(true));
/// assert_eq!(Scalar(false) | Scalar(false), Scalar(false));
/// }
/// ```
///
/// In this example, the `BitOr` trait is implemented for a `BooleanVector`
/// struct.
///
/// ```
/// use std::ops::BitOr;
///
/// #[derive(Debug, PartialEq)]
/// struct BooleanVector(Vec<bool>);
///
/// impl BitOr for BooleanVector {
/// type Output = Self;
///
/// fn bitor(self, BooleanVector(rhs): Self) -> Self {
/// let BooleanVector(lhs) = self;
/// assert_eq!(lhs.len(), rhs.len());
/// BooleanVector(lhs.iter().zip(rhs.iter()).map(|(x, y)| *x || *y).collect())
/// }
/// }
///
/// fn main() {
/// let bv1 = BooleanVector(vec![true, true, false, false]);
/// let bv2 = BooleanVector(vec![true, false, true, false]);
/// let expected = BooleanVector(vec![true, true, true, false]);
/// assert_eq!(bv1 | bv2, expected);
/// } /// }
/// ``` /// ```
#[lang = "bitor"] #[lang = "bitor"]
@ -860,25 +890,58 @@ bitor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
/// ///
/// # Examples /// # Examples
/// ///
/// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up /// In this example, the `^` operator is lifted to a trivial `Scalar` type.
/// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
/// ///
/// ``` /// ```
/// use std::ops::BitXor; /// use std::ops::BitXor;
/// ///
/// struct Foo; /// #[derive(Debug, PartialEq)]
/// struct Scalar(bool);
/// ///
/// impl BitXor for Foo { /// impl BitXor for Scalar {
/// type Output = Foo; /// type Output = Self;
/// ///
/// fn bitxor(self, _rhs: Foo) -> Foo { /// // rhs is the "right-hand side" of the expression `a ^ b`
/// println!("Bitwise Xor-ing!"); /// fn bitxor(self, rhs: Self) -> Self {
/// self /// Scalar(self.0 ^ rhs.0)
/// } /// }
/// } /// }
/// ///
/// fn main() { /// fn main() {
/// Foo ^ Foo; /// assert_eq!(Scalar(true) ^ Scalar(true), Scalar(false));
/// assert_eq!(Scalar(true) ^ Scalar(false), Scalar(true));
/// assert_eq!(Scalar(false) ^ Scalar(true), Scalar(true));
/// assert_eq!(Scalar(false) ^ Scalar(false), Scalar(false));
/// }
/// ```
///
/// In this example, the `BitXor` trait is implemented for a `BooleanVector`
/// struct.
///
/// ```
/// use std::ops::BitXor;
///
/// #[derive(Debug, PartialEq)]
/// struct BooleanVector(Vec<bool>);
///
/// impl BitXor for BooleanVector {
/// type Output = Self;
///
/// fn bitxor(self, BooleanVector(rhs): Self) -> Self {
/// let BooleanVector(lhs) = self;
/// assert_eq!(lhs.len(), rhs.len());
/// BooleanVector(lhs.iter()
/// .zip(rhs.iter())
/// .map(|(x, y)| (*x || *y) && !(*x && *y))
/// .collect())
/// }
/// }
///
/// fn main() {
/// let bv1 = BooleanVector(vec![true, true, false, false]);
/// let bv2 = BooleanVector(vec![true, false, true, false]);
/// let expected = BooleanVector(vec![false, true, true, false]);
/// assert_eq!(bv1 ^ bv2, expected);
/// } /// }
/// ``` /// ```
#[lang = "bitxor"] #[lang = "bitxor"]