pub trait Bits {
type Bits;
const N_BITS: u32;
// Required methods
fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,
Self: Sized;
fn bits<I, R>(self, range: R) -> Self::Bits
where I: BitsIndex<Self>,
R: RangeBounds<I>,
Self: Sized;
fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,
Self: Sized;
fn set_bits<I, R>(&mut self, range: R, bits: Self::Bits)
where I: BitsIndex<Self>,
R: RangeBounds<I>,
Self: Sized;
fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,
Self: Sized;
fn with_bits<I, R>(self, range: R, bits: Self::Bits) -> Self
where I: BitsIndex<Self>,
R: RangeBounds<I>,
Self: Sized;
}
Expand description
Extension trait to provide access to individual bits of integers.
Required Associated Constants§
Required Associated Types§
Sourcetype Bits
type Bits
The (unsigned) type used to represent bits of this type.
For unsigned integers, this is Self
.
For signed integers, this is the unsigned variant of Self
.
§Example
assert_eq!(0 as <u8 as Bits>::Bits, 0u8);
assert_eq!(0 as <i64 as Bits>::Bits, 0u64);
assert_eq!(0 as <usize as Bits>::Bits, 0usize);
assert_eq!(0 as <isize as Bits>::Bits, 0usize);
Required Methods§
Sourcefn bit<I>(self, i: I) -> bool
fn bit<I>(self, i: I) -> bool
Get a specific bit.
Panics if the index is out of range.
§Example
assert_eq!(2u8.bit(0), false);
assert_eq!(2u8.bit(1), true);
assert_eq!(2u8.bit(2), false);
Sourcefn bits<I, R>(self, range: R) -> Self::Bits
fn bits<I, R>(self, range: R) -> Self::Bits
Get a range of bits.
The bits are returned in the least significant bits of the return value. The other bits, if any, will be 0.
Empty ranges are allowed, and will result in 0.
Panics when the range bounds are out of range.
§Example
assert_eq!(0x45u8.bits(0..4), 5);
assert_eq!(0x45u8.bits(4..8), 4);
assert_eq!(0xF1u8.bits(1..), 0x78);
assert_eq!(0xF1u8.bits(..7), 0x71);
assert_eq!(0xF1u8.bits(8..), 0);
assert_eq!(0xF1u8.bits(..0), 0);
Sourcefn set_bit<I>(&mut self, i: I, bit: bool)
fn set_bit<I>(&mut self, i: I, bit: bool)
Set a specific bit.
Panics if the index is out of range.
§Example
let mut a = 0xFFu8;
a.set_bit(3, false);
assert_eq!(a, 0xF7);
Sourcefn set_bits<I, R>(&mut self, range: R, bits: Self::Bits)
fn set_bits<I, R>(&mut self, range: R, bits: Self::Bits)
Set a range of bits.
The bits should be given in the least significant bits of the second argument. The other bits should be 0.
Panics when the range bounds are out of range or when the irrelevant bits of the second argument are not 0.
§Example
let mut a = 0xFFu8;
a.set_bits(4..8, 3);
assert_eq!(a, 0x3F);
Sourcefn with_bit<I>(self, i: I, bit: bool) -> Self
fn with_bit<I>(self, i: I, bit: bool) -> Self
Get a new integer with one bit set to a specific value.
Panics if the index is out of range.
§Example
assert_eq!(0xFFu8.with_bit(3, false), 0xF7);
Sourcefn with_bits<I, R>(self, range: R, bits: Self::Bits) -> Self
fn with_bits<I, R>(self, range: R, bits: Self::Bits) -> Self
Get a new integer with a range of bits set to specific values.
The bits should be given in the least significant bits of the second argument. The other bits should be 0.
Panics when the range bounds are out of range or when the irrelevant bits of the second argument are not 0.
§Example
assert_eq!(0xFFu8.with_bits(4..8, 3), 0x3F);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.