Struct BitPattern

Source
pub struct BitPattern<T: BitPatternType> { /* private fields */ }
Expand description

A pattern of bits that integers can be matched against.

A BitPattern requires specific bits to be set and specific bits to be cleared, with others ignored. It can be constructed using the set_and_cleared method or the bitpattern! macro. The is_match method can be used for matching numbers against the pattern.

There is also a constant set_and_cleared_const method, which does not support type inference, due to a lack of support for generic const functions.

See the bitpatterns crate documentation for more details.

§Examples

use bitpatterns::*;

// Create a bitpattern using the set_and_cleared methods
let pattern_1 = BitPattern::set_and_cleared(0b1100, 0b0001);
const PATTERN_2: BitPattern<u8> = BitPattern::<u8>::set_and_cleared_const(0b0010, 0b1001);
// Create equivalent patterns with the bitpattern! macro
// Creating patterns this way is typically clearer
let macro_pattern_1 = bitpattern!("0b11.0");
const MACRO_PATTERN_2: BitPattern<u8> = bitpattern!("0b0.10", u8);

// Now check for matches
// Note that bits which weren't specified in the patterns are ignored by default
assert!(pattern_1.is_match(30)); // 30 == 0b11110
assert!(!PATTERN_2.is_match(30));

assert!(!pattern_1.is_match(2)); // 2 == 0b0010
assert!(PATTERN_2.is_match(2));

Implementations§

Source§

impl<T: BitPatternType> BitPattern<T>

Source

pub fn is_match(&self, other: T) -> bool

Match a number against the BitPattern

See the BitPattern type documentation, the bitpattern! macro documentation, or the bitpatterns crate documentation for more details about constructing patterns and the corresponding matching rules. Also see the is_bit_match! macro documentation for details on matching against a temporary/single-use BitPattern.

§Examples
use bitpatterns::*;

let pattern_1 = bitpattern!("0b10..");
// Compare numbers against pattern_1
assert!(pattern_1.is_match(8));   // 8  == 0b1000
assert!(pattern_1.is_match(11));  // 11 == 0b1011
assert!(!pattern_1.is_match(7));  // 7  == 0b0111
assert!(!pattern_1.is_match(16)); // 16 == 0b10000
  
// Digits which are more significant than those in the pattern are always ignored
let pattern_2 = bitpattern!("0b1");
assert!(pattern_2.is_match(3));  // 3 == 0b11
assert!(!pattern_2.is_match(2)); // 2 == 0b10
Source

pub fn set_and_cleared(set: T, cleared: T) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a BitPattern, or set_and_cleared_const on the various BitPattern implementations for a constant version of this function.

Source§

impl BitPattern<u8>

Source

pub const fn set_and_cleared_const(set: u8, cleared: u8) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Source§

impl BitPattern<u16>

Source

pub const fn set_and_cleared_const(set: u16, cleared: u16) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Source§

impl BitPattern<u32>

Source

pub const fn set_and_cleared_const(set: u32, cleared: u32) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Source§

impl BitPattern<u64>

Source

pub const fn set_and_cleared_const(set: u64, cleared: u64) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Source§

impl BitPattern<u128>

Source

pub const fn set_and_cleared_const(set: u128, cleared: u128) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Source§

impl BitPattern<i8>

Source

pub const fn set_and_cleared_const(set: i8, cleared: i8) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Source§

impl BitPattern<i16>

Source

pub const fn set_and_cleared_const(set: i16, cleared: i16) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Source§

impl BitPattern<i32>

Source

pub const fn set_and_cleared_const(set: i32, cleared: i32) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Source§

impl BitPattern<i64>

Source

pub const fn set_and_cleared_const(set: i64, cleared: i64) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Source§

impl BitPattern<i128>

Source

pub const fn set_and_cleared_const(set: i128, cleared: i128) -> Self

Create a BitPattern by specifying the bits that must be set and cleared to match it

The bits that are set in set must be set in a matching number, and the bits that are set in cleared must not be set in a matching number. Any bits not specified in either will be ignored when matching numbers.

The behavior of a BitPattern in which a particular bit is set in both set and cleared (i.e. set & cleared != 0) is guaranteed to be deterministic for a particular version of this crate, but should not be considered stable across versions. Presently, the bits in set take precedence over those in cleared.

See the bitpattern! macro for a simpler way to construct a constant BitPattern, or set_and_cleared for a generic (but not constant) version of this function.

Trait Implementations§

Source§

impl<T: Clone + BitPatternType> Clone for BitPattern<T>

Source§

fn clone(&self) -> BitPattern<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: BitPatternType> Debug for BitPattern<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: BitPatternType> Display for BitPattern<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Hash + BitPatternType> Hash for BitPattern<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq + BitPatternType> PartialEq for BitPattern<T>

Source§

fn eq(&self, other: &BitPattern<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy + BitPatternType> Copy for BitPattern<T>

Source§

impl<T: Eq + BitPatternType> Eq for BitPattern<T>

Source§

impl<T: BitPatternType> StructuralPartialEq for BitPattern<T>

Auto Trait Implementations§

§

impl<T> Freeze for BitPattern<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for BitPattern<T>
where T: RefUnwindSafe,

§

impl<T> Send for BitPattern<T>
where T: Send,

§

impl<T> Sync for BitPattern<T>
where T: Sync,

§

impl<T> Unpin for BitPattern<T>
where T: Unpin,

§

impl<T> UnwindSafe for BitPattern<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.