Trait sp_std::ops::BitAnd

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

The bitwise AND operator &.

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

Examples

An implementation of BitAnd for a wrapper around bool.

use std::ops::BitAnd;

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

impl BitAnd for Scalar {
    type Output = Self;

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

assert_eq!(Scalar(true) & Scalar(true), Scalar(true));
assert_eq!(Scalar(true) & Scalar(false), Scalar(false));
assert_eq!(Scalar(false) & Scalar(true), Scalar(false));
assert_eq!(Scalar(false) & Scalar(false), Scalar(false));

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

use std::ops::BitAnd;

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

impl BitAnd for BooleanVector {
    type Output = Self;

    fn bitand(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![true, false, false, 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, false);
assert_eq!(true & true, true);
assert_eq!(5u8 & 1u8, 1);
assert_eq!(5u8 & 2u8, 0);

Implementations on Foreign Types

Returns the intersection 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([2, 3, 4]);

let set = &a & &b;

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

Implementors

impl<O, V, Rhs> BitAnd<Rhs> for BitArray<O, V> where
    O: BitOrder,
    V: BitView,
    BitSlice<O, V::Store>: BitAndAssign<Rhs>, 

impl<R> BitAnd<R> for BitMask<R> where
    R: BitRegister

impl<O, T, Rhs> BitAnd<Rhs> for BitBox<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: BitAndAssign<Rhs>, 

impl<O, T, Rhs> BitAnd<Rhs> for BitVec<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: BitAndAssign<Rhs>, 

impl<T, S, A> BitAnd<&'_ HashSet<T, S, A>> for &HashSet<T, S, A> where
    T: Eq + Hash + Clone,
    S: BuildHasher + Default,
    A: Allocator + Clone

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

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

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

impl BitAnd<BigInt> for BigInt

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

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

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

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

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

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

impl BitAnd<U128> for U128

impl BitAnd<U256> for U256

impl BitAnd<U512> for U512

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

impl BitAnd<H128> for H128

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

impl BitAnd<H160> for H160

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

impl BitAnd<H256> for H256

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

impl BitAnd<H512> for H512

impl BitAnd<Choice> for Choice

impl<Rhs: Bit> BitAnd<Rhs> for B0

impl BitAnd<B0> for B1

impl BitAnd<B1> for B1

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

impl<Ul: Unsigned, Bl: Bit, Ur: Unsigned> BitAnd<Ur> for UInt<Ul, Bl> where
    UInt<Ul, Bl>: PrivateAnd<Ur>,
    PrivateAndOut<UInt<Ul, Bl>, Ur>: Trim,