Trait scale_info::prelude::ops::BitAndAssign
1.8.0 · source · [−]pub trait BitAndAssign<Rhs = Self> {
fn bitand_assign(&mut self, rhs: Rhs);
}
Expand description
The bitwise AND assignment operator &=
.
Examples
An implementation of BitAndAssign
that lifts the &=
operator to a
wrapper around bool
.
use std::ops::BitAndAssign;
#[derive(Debug, PartialEq)]
struct Scalar(bool);
impl BitAndAssign for Scalar {
// rhs is the "right-hand side" of the expression `a &= b`
fn bitand_assign(&mut self, rhs: Self) {
*self = Self(self.0 & rhs.0)
}
}
let mut scalar = Scalar(true);
scalar &= Scalar(true);
assert_eq!(scalar, Scalar(true));
let mut scalar = Scalar(true);
scalar &= Scalar(false);
assert_eq!(scalar, Scalar(false));
let mut scalar = Scalar(false);
scalar &= Scalar(true);
assert_eq!(scalar, Scalar(false));
let mut scalar = Scalar(false);
scalar &= Scalar(false);
assert_eq!(scalar, Scalar(false));
Here, the BitAndAssign
trait is implemented for a wrapper around
Vec<bool>
.
use std::ops::BitAndAssign;
#[derive(Debug, PartialEq)]
struct BooleanVector(Vec<bool>);
impl BitAndAssign for BooleanVector {
// `rhs` is the "right-hand side" of the expression `a &= b`.
fn bitand_assign(&mut self, rhs: Self) {
assert_eq!(self.0.len(), rhs.0.len());
*self = Self(
self.0
.iter()
.zip(rhs.0.iter())
.map(|(x, y)| *x & *y)
.collect()
);
}
}
let mut bv = BooleanVector(vec![true, true, false, false]);
bv &= BooleanVector(vec![true, false, true, false]);
let expected = BooleanVector(vec![true, false, false, false]);
assert_eq!(bv, expected);
Required methods
fn bitand_assign(&mut self, rhs: Rhs)
fn bitand_assign(&mut self, rhs: Rhs)
Performs the &=
operation.
Examples
let mut x = true;
x &= false;
assert_eq!(x, false);
let mut x = true;
x &= true;
assert_eq!(x, true);
let mut x: u8 = 5;
x &= 1;
assert_eq!(x, 1);
let mut x: u8 = 5;
x &= 2;
assert_eq!(x, 0);
Implementations on Foreign Types
const: unstable · sourceimpl BitAndAssign<u8> for u8
impl BitAndAssign<u8> for u8
pub fn bitand_assign(&mut self, other: u8)
const: unstable · sourceimpl BitAndAssign<i128> for i128
impl BitAndAssign<i128> for i128
pub fn bitand_assign(&mut self, other: i128)
const: unstable · sourceimpl BitAndAssign<i8> for i8
impl BitAndAssign<i8> for i8
pub fn bitand_assign(&mut self, other: i8)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ u128> for u128
impl<'_> BitAndAssign<&'_ u128> for u128
pub fn bitand_assign(&mut self, other: &u128)
const: unstable · sourceimpl BitAndAssign<u32> for u32
impl BitAndAssign<u32> for u32
pub fn bitand_assign(&mut self, other: u32)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ i32> for i32
impl<'_> BitAndAssign<&'_ i32> for i32
pub fn bitand_assign(&mut self, other: &i32)
const: unstable · sourceimpl BitAndAssign<u64> for u64
impl BitAndAssign<u64> for u64
pub fn bitand_assign(&mut self, other: u64)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ u32> for u32
impl<'_> BitAndAssign<&'_ u32> for u32
pub fn bitand_assign(&mut self, other: &u32)
const: unstable · sourceimpl BitAndAssign<u16> for u16
impl BitAndAssign<u16> for u16
pub fn bitand_assign(&mut self, other: u16)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ u8> for u8
impl<'_> BitAndAssign<&'_ u8> for u8
pub fn bitand_assign(&mut self, other: &u8)
const: unstable · sourceimpl BitAndAssign<i16> for i16
impl BitAndAssign<i16> for i16
pub fn bitand_assign(&mut self, other: i16)
const: unstable · sourceimpl BitAndAssign<u128> for u128
impl BitAndAssign<u128> for u128
pub fn bitand_assign(&mut self, other: u128)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ i16> for i16
impl<'_> BitAndAssign<&'_ i16> for i16
pub fn bitand_assign(&mut self, other: &i16)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ usize> for usize
impl<'_> BitAndAssign<&'_ usize> for usize
pub fn bitand_assign(&mut self, other: &usize)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ u64> for u64
impl<'_> BitAndAssign<&'_ u64> for u64
pub fn bitand_assign(&mut self, other: &u64)
sourceimpl<T, const LANES: usize> BitAndAssign<bool> for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitAndAssign<bool> for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
pub fn bitand_assign(&mut self, rhs: bool)
const: unstable · sourceimpl BitAndAssign<isize> for isize
impl BitAndAssign<isize> for isize
pub fn bitand_assign(&mut self, other: isize)
const: unstable · sourceimpl BitAndAssign<i64> for i64
impl BitAndAssign<i64> for i64
pub fn bitand_assign(&mut self, other: i64)
const: unstable · sourceimpl BitAndAssign<usize> for usize
impl BitAndAssign<usize> for usize
pub fn bitand_assign(&mut self, other: usize)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ i8> for i8
impl<'_> BitAndAssign<&'_ i8> for i8
pub fn bitand_assign(&mut self, other: &i8)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ i128> for i128
impl<'_> BitAndAssign<&'_ i128> for i128
pub fn bitand_assign(&mut self, other: &i128)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ isize> for isize
impl<'_> BitAndAssign<&'_ isize> for isize
pub fn bitand_assign(&mut self, other: &isize)
sourceimpl<T, U, const LANES: usize> BitAndAssign<U> for Simd<T, LANES> where
T: SimdElement,
Simd<T, LANES>: BitAnd<U>,
LaneCount<LANES>: SupportedLaneCount,
<Simd<T, LANES> as BitAnd<U>>::Output == Simd<T, LANES>,
impl<T, U, const LANES: usize> BitAndAssign<U> for Simd<T, LANES> where
T: SimdElement,
Simd<T, LANES>: BitAnd<U>,
LaneCount<LANES>: SupportedLaneCount,
<Simd<T, LANES> as BitAnd<U>>::Output == Simd<T, LANES>,
pub fn bitand_assign(&mut self, rhs: U)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ u16> for u16
impl<'_> BitAndAssign<&'_ u16> for u16
pub fn bitand_assign(&mut self, other: &u16)
const: unstable · sourceimpl BitAndAssign<i32> for i32
impl BitAndAssign<i32> for i32
pub fn bitand_assign(&mut self, other: i32)
sourceimpl<T, const LANES: usize> BitAndAssign<Mask<T, LANES>> for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitAndAssign<Mask<T, LANES>> for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
pub fn bitand_assign(&mut self, rhs: Mask<T, LANES>)
const: unstable · sourceimpl BitAndAssign<bool> for bool
impl BitAndAssign<bool> for bool
pub fn bitand_assign(&mut self, other: bool)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ bool> for bool
impl<'_> BitAndAssign<&'_ bool> for bool
pub fn bitand_assign(&mut self, other: &bool)
1.22.0 (const: unstable) · sourceimpl<'_> BitAndAssign<&'_ i64> for i64
impl<'_> BitAndAssign<&'_ i64> for i64
pub fn bitand_assign(&mut self, other: &i64)
sourceimpl<'_, T1, T2, O1, O2> BitAndAssign<&'_ BitSlice<T2, O2>> for BitSlice<T1, O1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<'_, T1, T2, O1, O2> BitAndAssign<&'_ BitSlice<T2, O2>> for BitSlice<T1, O1> where
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
sourcepub fn bitand_assign(&mut self, rhs: &BitSlice<T2, O2>)
pub fn bitand_assign(&mut self, rhs: &BitSlice<T2, O2>)
Boolean Arithmetic
This merges another bit-slice into self
with a Boolean arithmetic operation.
If the other bit-slice is shorter than self
, it is zero-extended. For BitAnd
,
this clears all excess bits of self
to 0
; for BitOr
and BitXor
, it
leaves them untouched
Behavior
The Boolean operation proceeds across each bit-slice in iteration order. This is
3O(n)
in the length of the shorter of self
and rhs
. However, it can be
accelerated if rhs
has the same type parameters as self
, and both are using
one of the orderings provided by bitvec
. In this case, the implementation
specializes to use BitField
batch operations to operate on the slices one word
at a time, rather than one bit.
Acceleration is not currently provided for custom bit-orderings that use the same storage type.
Pre-1.0
Behavior
In the 0.
development series, Boolean arithmetic was implemented against all
I: Iterator<Item = bool>
. This allowed code such as bits |= [false, true];
,
but forbad acceleration in the most common use case (combining two bit-slices)
because BitSlice
is not such an iterator.
Usage surveys indicate that it is better for the arithmetic operators to operate
on bit-slices, and to allow the possibility of specialized acceleration, rather
than to allow folding against any iterator of bool
s.
If pre-1.0
code relies on this behavior specifically, and has non-BitSlice
arguments to the Boolean sigils, then they will need to be replaced with the
equivalent loop.
Examples
use bitvec::prelude::*;
let a = bits![mut 0, 0, 1, 1];
let b = bits![ 0, 1, 0, 1];
*a ^= b;
assert_eq!(a, bits![0, 1, 1, 0]);
let c = bits![mut 0, 0, 1, 1];
let d = [false, true, false, true];
// no longer allowed
// c &= d.into_iter().by_vals();
for (mut c, d) in c.iter_mut().zip(d.into_iter())
{
*c ^= d;
}
assert_eq!(c, bits![0, 1, 1, 0]);