table.rs

Overview

This file defines a generic data structure, BitmasksTable<TBitsSource, TTarget>, which manages a collection of bitmask-to-target mappings. It allows for efficient pattern matching of bit patterns (TBitsSource) against defined bitmasks, returning a corresponding target value (TTarget). The table ensures that there is always a default fallback rule that matches any bit pattern, preventing unmatched cases.

The primary purpose is to store and query ordered bitmask rules, where each bitmask represents a pattern of bits to match, and the associated target is a value or identifier linked to that pattern. This structure is useful in scenarios where decisions or routing depend on bit-level pattern matching.

The file depends on the Bitmask type from the mask module, which encapsulates bitmask logic and matching. It also uses third-party crates such as anyhow for error handling and serde for serialization.

Types and Structures

BitmasksTable<TBitsSource, TTarget>

A generic table storing an ordered list of pairs (Bitmask<TBitsSource>, TTarget). This table is designed to:

Type Parameters:

Fields:

Trait Implementations

Default for BitmasksTable

This implementation delegates the default construction to the new method, which initializes a table with a single default rule using default values for both bitmask and target.

Methods

new() -> Self

Creates a new BitmasksTable with a single default rule at the end. The default rule uses the default bitmask (matching all inputs) and the default target value.

Usage example:

let table: BitmasksTable<u16, u8> = BitmasksTable::new();

rows(&self) -> impl Iterator<Item = &(Bitmask<TBitsSource>, TTarget)>

Returns an iterator over all the (Bitmask, Target) pairs in the table, in order.

Usage example:

for (bitmask, target) in table.rows() {
    println!("{:?} -> {:?}", bitmask, target);
}

find_match(&self, bits: &TBitsSource) -> TTarget

Scans the table from top to bottom and returns a clone of the target associated with the first bitmask that matches the given bits. The method panics if no match is found, which should not happen due to the presence of the default rule.

Parameters:

Returns:

Usage example:

let target = table.find_match(&some_bits);

is_match(&self, bits: &TBitsSource, target: TTarget) -> bool

Checks if the given bits match the specified target by internally finding the matched target and comparing it.

Parameters:

Returns:

insert_above(&mut self, row_index: usize, mask: Bitmask<TBitsSource>, thread: TTarget) -> anyhow::Result<()>

Inserts a new bitmask-target rule just above the specified row_index. This allows ordered insertion of rules with priority.

Parameters:

Returns:

Usage example:

table.insert_above(0, some_bitmask, some_target)?;

remove(&mut self, row_index: usize) -> anyhow::Result<(Bitmask<TBitsSource>, TTarget)>

Removes the rule at the specified row_index and returns it. Removing the default rule (the last one) is forbidden and returns an error.

Parameters:

Returns:

len(&self) -> usize

Returns the number of rules currently in the table, including the default rule.

Important Implementation Details

Interaction with Other Parts of the System

Usage Context

BitmasksTable can be used in systems that require:

Example Usage Snippet

use table::BitmasksTable;
use mask::Bitmask;

let mut table = BitmasksTable::<u16, u8>::new();

let mask = Bitmask::<u16>::builder()
    .meaningful_mask_bits(!0u16) // all bits considered
    .mask_bits(0b0100_0000_0000_0000)
    .build();

table.insert_above(0, mask, 1).unwrap();

let matched_target = table.find_match(&0b0100_0000_0000_0000);
assert_eq!(matched_target, 1);

Mermaid Diagram: Class Structure of BitmasksTable

classDiagram
class BitmasksTable {
-masks: Vec<(Bitmask<TBitsSource>, TTarget)>
+new()
+rows()
+find_match()
+is_match()
+insert_above()
+remove()
+len()
}

This diagram shows the core data structure and its public methods, encapsulating the bitmask-to-target mapping logic.