Trait Bits

Source
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§

Source

const N_BITS: u32

The number of bits this type has.

§Example
assert_eq!(u8::N_BITS, 8);
assert_eq!(i64::N_BITS, 64);

Required Associated Types§

Source

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§

Source

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>, Self: Sized,

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

fn bits<I, R>(self, range: R) -> Self::Bits
where I: BitsIndex<Self>, R: RangeBounds<I>, Self: Sized,

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

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>, Self: Sized,

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

fn set_bits<I, R>(&mut self, range: R, bits: Self::Bits)
where I: BitsIndex<Self>, R: RangeBounds<I>, Self: Sized,

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

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>, Self: Sized,

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

fn with_bits<I, R>(self, range: R, bits: Self::Bits) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>, Self: Sized,

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.

Implementations on Foreign Types§

Source§

impl Bits for i8

Source§

const N_BITS: u32 = 8u32

Source§

type Bits = u8

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u8
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u8)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u8) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for i16

Source§

const N_BITS: u32 = 16u32

Source§

type Bits = u16

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u16
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u16)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u16) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for i32

Source§

const N_BITS: u32 = 32u32

Source§

type Bits = u32

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u32
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u32)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u32) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for i64

Source§

const N_BITS: u32 = 64u32

Source§

type Bits = u64

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u64
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u64)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u64) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for i128

Source§

const N_BITS: u32 = 128u32

Source§

type Bits = u128

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u128
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u128)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u128) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for isize

Source§

const N_BITS: u32 = 64u32

Source§

type Bits = usize

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> usize
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: usize)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: usize) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for u8

Source§

const N_BITS: u32 = 8u32

Source§

type Bits = u8

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u8
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u8)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u8) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for u16

Source§

const N_BITS: u32 = 16u32

Source§

type Bits = u16

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u16
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u16)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u16) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for u32

Source§

const N_BITS: u32 = 32u32

Source§

type Bits = u32

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u32
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u32)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u32) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for u64

Source§

const N_BITS: u32 = 64u32

Source§

type Bits = u64

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u64
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u64)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u64) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for u128

Source§

const N_BITS: u32 = 128u32

Source§

type Bits = u128

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> u128
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: u128)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: u128) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

impl Bits for usize

Source§

const N_BITS: u32 = 64u32

Source§

type Bits = usize

Source§

fn bit<I>(self, i: I) -> bool
where I: BitsIndex<Self>,

Source§

fn bits<I, R>(self, range: R) -> usize
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn set_bit<I>(&mut self, i: I, bit: bool)
where I: BitsIndex<Self>,

Source§

fn set_bits<I, R>(&mut self, range: R, bits: usize)
where I: BitsIndex<Self>, R: RangeBounds<I>,

Source§

fn with_bit<I>(self, i: I, bit: bool) -> Self
where I: BitsIndex<Self>,

Source§

fn with_bits<I, R>(self, range: R, bits: usize) -> Self
where I: BitsIndex<Self>, R: RangeBounds<I>,

Implementors§