mask.rs

Overview

This file defines a generic Bitmask struct that provides functionality to perform bitwise masking operations on a given bit source type TBitsSource. It encapsulates a mask and a meaningful mask, allowing selective comparison of bits for matching purposes. This is useful in scenarios requiring filtering or matching entities based on specific bit patterns, such as permissions, flags, or address matching.

The Bitmask struct supports generic bit sources as long as they implement certain bitwise and cloning traits. It implements methods to check if a given bit source matches the mask under the constraints of the meaningful mask bits.

Structs and Implementations

Bitmask<TBitsSource>

A generic container that holds two bitmasks:

Type Parameters

Derives

Fields

Field

Type

Description

mask_bits

TBitsSource

Bit values that represent the mask pattern.

meaningful_mask_bits

TBitsSource

Bit positions that are considered meaningful in the mask. Bits outside this mask are ignored in comparisons.

Methods

Implementation Details

The is_match method uses bitwise AND to isolate the meaningful bits in both the stored mask and the input address. It then compares these masked values for equality. This design allows bits outside the meaningful mask to be ignored, enabling flexible partial matching of bit patterns.

The trait bounds on TBitsSource ensure that bitwise AND and cloning operations are supported on the references to the bit source, facilitating generic usage with types that represent bitfields.

fmt::Display Implementation for Bitmask<TBitsSource> (Test-Only)

An inefficient but convenient Display trait implementation exists in the test module for debugging purposes. It prints the mask as a string of bits where:

This makes the bitmask human-readable during testing.

Test Module

The tests module includes unit tests to verify the correctness of the Bitmask's behavior:

These tests use integer types (u8, u16, u64) to exercise the generic implementation.

Interaction with Other System Components

This file is focused on bitmask matching logic and is designed to be generic over bit source types. It can be used wherever bitwise filtering or matching is required, such as:

No direct dependencies on other application-specific modules are present in this file, but it likely interacts with components that require bit pattern matching or filtering.

Key Algorithms and Concepts

Diagram: Bitmask Struct and Method Overview

classDiagram
class Bitmask {
-mask_bits
-meaningful_mask_bits
+is_match()
+mask_bits()
+meaningful_mask_bits()
}

This diagram shows the Bitmask struct encapsulating two private fields representing bit patterns and three public methods: is_match for matching logic, and accessors for the stored masks.