Trait sp_std::ops::BitXor

1.0.0 · source · []
pub trait BitXor<Rhs = Self> {
    type Output;
    fn bitxor(self, rhs: Rhs) -> Self::Output;
}
Expand description

The bitwise XOR operator ^.

Note that Rhs is Self by default, but this is not mandatory.

Examples

An implementation of BitXor that lifts ^ to a wrapper around bool.

use std::ops::BitXor;

#[derive(Debug, PartialEq)]
struct Scalar(bool);

impl BitXor for Scalar {
    type Output = Self;

    // rhs is the "right-hand side" of the expression `a ^ b`
    fn bitxor(self, rhs: Self) -> Self::Output {
        Self(self.0 ^ rhs.0)
    }
}

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));

An implementation of BitXor trait for a wrapper around Vec<bool>.

use std::ops::BitXor;

#[derive(Debug, PartialEq)]
struct BooleanVector(Vec<bool>);

impl BitXor for BooleanVector {
    type Output = Self;

    fn bitxor(self, Self(rhs): Self) -> Self::Output {
        let Self(lhs) = self;
        assert_eq!(lhs.len(), rhs.len());
        Self(
            lhs.iter()
                .zip(rhs.iter())
                .map(|(x, y)| *x ^ *y)
                .collect()
        )
    }
}

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);

Associated Types

The resulting type after applying the ^ operator.

Required methods

Performs the ^ operation.

Examples
assert_eq!(true ^ false, true);
assert_eq!(true ^ true, false);
assert_eq!(5u8 ^ 1u8, 4);
assert_eq!(5u8 ^ 2u8, 7);

Implementations on Foreign Types

Returns the symmetric difference of self and rhs as a new HashSet<T, S>.

Examples
use std::collections::HashSet;

let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([3, 4, 5]);

let set = &a ^ &b;

let mut i = 0;
let expected = [1, 2, 4, 5];
for x in &set {
    assert!(expected.contains(x));
    i += 1;
}
assert_eq!(i, expected.len());

Implementors

impl<O, V, Rhs> BitXor<Rhs> for BitArray<O, V> where
    O: BitOrder,
    V: BitView,
    BitSlice<O, V::Store>: BitXorAssign<Rhs>, 

impl<O, T, Rhs> BitXor<Rhs> for BitBox<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: BitXorAssign<Rhs>, 

impl<O, T, Rhs> BitXor<Rhs> for BitVec<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: BitXorAssign<Rhs>, 

impl<T, S> BitXor<&'_ HashSet<T, S, Global>> for &HashSet<T, S> where
    T: Eq + Hash + Clone,
    S: BuildHasher + Default

impl<T, S1, S2> BitXor<&'_ IndexSet<T, S2>> for &IndexSet<T, S1> where
    T: Eq + Hash + Clone,
    S1: BuildHasher + Default,
    S2: BuildHasher

impl<'a, 'b, T, S> BitXor<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S> where
    T: Eq + Hash + Clone,
    S: BuildHasher + Default

impl<T: Into<Ready>> BitXor<T> for Ready

impl BitXor<BigInt> for BigInt

impl<'a> BitXor<BigInt> for &'a BigInt

impl<'a, 'b> BitXor<&'b BigInt> for &'a BigInt

impl<'a> BitXor<&'a BigInt> for BigInt

impl<'a> BitXor<BigUint> for &'a BigUint

impl<'a, 'b> BitXor<&'b BigUint> for &'a BigUint

impl<'a> BitXor<&'a BigUint> for BigUint

impl BitXor<U128> for U128

impl BitXor<U256> for U256

impl BitXor<U512> for U512

impl<'l, 'r> BitXor<&'r H128> for &'l H128

impl BitXor<H128> for H128

impl<'l, 'r> BitXor<&'r H160> for &'l H160

impl BitXor<H160> for H160

impl<'l, 'r> BitXor<&'r H256> for &'l H256

impl BitXor<H256> for H256

impl<'l, 'r> BitXor<&'r H512> for &'l H512

impl BitXor<H512> for H512

impl BitXor<Choice> for Choice

impl BitXor<B0> for B0

impl BitXor<B0> for B1

impl BitXor<B1> for B0

impl BitXor<B1> for B1

impl<Ur: Unsigned> BitXor<Ur> for UTerm

impl<Ul: Unsigned, Bl: Bit, Ur: Unsigned> BitXor<Ur> for UInt<Ul, Bl> where
    UInt<Ul, Bl>: PrivateXor<Ur>,
    PrivateXorOut<UInt<Ul, Bl>, Ur>: Trim,