API Reference

Relevant source files

This document provides complete reference documentation for the CpuMask<SIZE> struct and its associated types, methods, and trait implementations. The API enables efficient CPU affinity management through bitset operations optimized for different CPU count scenarios.

For architectural details and storage optimization strategies, see Architecture and Design. For practical usage examples and common patterns, see Usage Guide and Examples.

Core Types and Structure

The cpumask library centers around the CpuMask<const SIZE: usize> generic struct, which provides a compile-time sized bitset for representing CPU sets.

flowchart TD
subgraph subGraph3["Iterator Interface"]
    G1["IntoIterator trait"]
    G2["Iter<'a, SIZE>"]
    G3["Iterator + DoubleEndedIterator"]
end
subgraph subGraph2["Query Operations"]
    D1["get(index)"]
    D2["len()"]
    D3["is_empty()"]
    D4["is_full()"]
    D5["first_index()"]
    D6["last_index()"]
    D7["next_index(index)"]
    D8["prev_index(index)"]
    D9["*_false_index variants"]
end
subgraph subGraph1["Construction Methods"]
    C1["new()"]
    C2["full()"]
    C3["mask(bits)"]
    C4["from_value(data)"]
    C5["from_raw_bits(value)"]
    C6["one_shot(index)"]
end
subgraph subGraph0["Core API Structure"]
    A["CpuMask<SIZE>"]
    B["Bitmap<SIZE> value"]
    C["Construction Methods"]
    D["Query Operations"]
    E["Modification Methods"]
    F["Bitwise Operations"]
    G["Iterator Interface"]
end

A --> B
A --> C
A --> D
A --> E
A --> F
A --> G
C --> C1
C --> C2
C --> C3
C --> C4
C --> C5
C --> C6
D --> D1
D --> D2
D --> D3
D --> D4
D --> D5
D --> D6
D --> D7
D --> D8
D --> D9
G --> G1
G --> G2
G --> G3

Sources: src/lib.rs(L18 - L23)  src/lib.rs(L68 - L235) 

Type Definitions and Constraints

TypeDefinitionConstraints
CpuMaskMain bitset structBitsImpl: Bits
Iter<'a, const SIZE: usize>Iterator over set bit indicesBitsImpl: Bits
Storage Type<BitsImpl as Bits>::StoreAutomatically selected based on SIZE

The storage type is automatically optimized based on the SIZE parameter:

SIZE RangeStorage TypeMemory Usage
1bool1 bit
2-8u88 bits
9-16u1616 bits
17-32u3232 bits
33-64u6464 bits
65-128u128128 bits
129-1024[u128; N]N × 128 bits

Sources: src/lib.rs(L12 - L16)  src/lib.rs(L18 - L23) 

Construction and Conversion Methods

The CpuMask type provides multiple ways to create and convert between different representations.

Basic Construction

MethodSignatureDescriptionTime Complexity
new()fn new() -> SelfCreates empty mask (all bits false)O(1)
full()fn full() -> SelfCreates full mask (all bits true)O(1)
mask(bits)fn mask(bits: usize) -> SelfCreates mask with firstbitsset to trueO(1)
one_shot(index)fn one_shot(index: usize) -> SelfCreates mask with single bit set atindexO(1)

Advanced Construction

MethodSignatureDescriptionPanics
from_value(data)fn from_value(data: Store) -> SelfCreates from backing store valueNever
from_raw_bits(value)fn from_raw_bits(value: usize) -> SelfCreates from raw usize bitsIfvalue >= 2^SIZE

Conversion Methods

MethodSignatureDescription
into_value(self)fn into_value(self) -> StoreConverts to backing store value
as_value(&self)fn as_value(&self) -> &StoreGets reference to backing store
as_bytes(&self)fn as_bytes(&self) -> &[u8]Gets byte slice representation

Sources: src/lib.rs(L72 - L146) 

Query and Inspection Operations

flowchart TD
subgraph subGraph2["Index Finding (False Bits)"]
    I["first_false_index()"]
    I1["Option<usize>"]
    J["last_false_index()"]
    J1["Option<usize>"]
    K["next_false_index(idx)"]
    K1["Option<usize>"]
    E["first_index()"]
    E1["Option<usize>"]
    F["last_index()"]
    F1["Option<usize>"]
    G["next_index(idx)"]
    G1["Option<usize>"]
    H["prev_index(idx)"]
    H1["Option<usize>"]
    A["get(index)"]
    A1["Returns bool"]
    B["len()"]
    B1["Count of set bits"]
    C["is_empty()"]
    C1["No bits set"]
    D["is_full()"]
    D1["All bits set"]
    subgraph subGraph1["Index Finding (True Bits)"]
        L["prev_false_index(idx)"]
        L1["Option<usize>"]
        subgraph subGraph0["Bit Query Operations"]
            I["first_false_index()"]
            I1["Option<usize>"]
            J["last_false_index()"]
            J1["Option<usize>"]
            K["next_false_index(idx)"]
            K1["Option<usize>"]
            E["first_index()"]
            E1["Option<usize>"]
            F["last_index()"]
            F1["Option<usize>"]
            G["next_index(idx)"]
            G1["Option<usize>"]
            H["prev_index(idx)"]
            H1["Option<usize>"]
            A["get(index)"]
            A1["Returns bool"]
            B["len()"]
            B1["Count of set bits"]
            C["is_empty()"]
            C1["No bits set"]
            D["is_full()"]
            D1["All bits set"]
        end
    end
end

A --> A1
B --> B1
C --> C1
D --> D1
E --> E1
F --> F1
G --> G1
H --> H1
I --> I1
J --> J1
K --> K1
L --> L1

Bit Testing Operations

MethodReturn TypeDescriptionTime Complexity
get(index)boolTests if bit at index is setO(1)
len()usizeCounts number of set bitsO(n) for arrays, O(1) for primitives
is_empty()boolTests if no bits are setO(log n)
is_full()boolTests if all bits are setO(log n)

Index Finding Operations

MethodReturn TypeDescriptionTime Complexity
first_index()OptionFinds first set bitO(log n)
last_index()OptionFinds last set bitO(log n)
next_index(index)OptionFinds next set bit after indexO(log n)
prev_index(index)OptionFinds previous set bit before indexO(log n)
first_false_index()OptionFinds first unset bitO(log n)
last_false_index()OptionFinds last unset bitO(log n)
next_false_index(index)OptionFinds next unset bit after indexO(log n)
prev_false_index(index)OptionFinds previous unset bit before indexO(log n)

Sources: src/lib.rs(L148 - L228) 

Modification and Iteration

Modification Operations

MethodSignatureDescriptionReturns
set(&mut self, index, value)fn set(&mut self, index: usize, value: bool) -> boolSets bit at indexPrevious value
invert(&mut self)fn invert(&mut self)Inverts all bits()

Iterator Interface

The CpuMask implements IntoIterator for &CpuMask<SIZE>, yielding indices of set bits.

flowchart TD
subgraph subGraph1["Iter Struct Fields"]
    J["head: Option<usize>"]
    K["tail: Option<usize>"]
    L["data: &'a CpuMask<SIZE>"]
end
subgraph subGraph0["Iterator Implementation"]
    A["&CpuMask<SIZE>"]
    B["IntoIterator"]
    C["Iter<'a, SIZE>"]
    D["Iterator"]
    E["DoubleEndedIterator"]
    F["next()"]
    G["Option<usize>"]
    H["next_back()"]
    I["Option<usize>"]
end

A --> B
B --> C
C --> D
C --> E
C --> J
C --> K
C --> L
D --> F
E --> H
F --> G
H --> I
Iterator TypeItem TypeDescription
Iter<'a, SIZE>usizeIterates over indices of set bits
Trait ImplementationMethodsDescription
Iteratornext()Forward iteration through set bit indices
DoubleEndedIteratornext_back()Reverse iteration through set bit indices

Sources: src/lib.rs(L172 - L180)  src/lib.rs(L230 - L234)  src/lib.rs(L237 - L251)  src/lib.rs(L412 - L518) 

Bitwise Operations and Trait Implementations

Binary Bitwise Operations

flowchart TD
subgraph subGraph0["Binary Operations"]
    E["&="]
    E1["BitAndAssign"]
    D["!CpuMask"]
    D1["Not - Complement"]
    A["CpuMask & CpuMask"]
    A1["BitAnd - Intersection"]
    C["CpuMask ^ CpuMask"]
    C1["BitXor - Symmetric Difference"]
    subgraph subGraph2["Assignment Operations"]
        F["|="]
        F1["BitOrAssign"]
        G["^="]
        G1["BitXorAssign"]
        B["CpuMask | CpuMask"]
        B1["BitOr - Union"]
        subgraph subGraph1["Unary Operations"]
            E["&="]
            E1["BitAndAssign"]
            D["!CpuMask"]
            D1["Not - Complement"]
            A["CpuMask & CpuMask"]
            A1["BitAnd - Intersection"]
        end
    end
end

A --> A1
B --> B1
C --> C1
D --> D1
E --> E1
F --> F1
G --> G1
OperatorTraitDescriptionResult
&BitAndIntersection of two masksSet bits present in both
|BitOrUnion of two masksSet bits present in either
^BitXorSymmetric differenceSet bits present in exactly one
!NotComplement of maskInverts all bits
&=BitAndAssignIn-place intersectionModifies left operand
|=BitOrAssignIn-place unionModifies left operand
^=BitXorAssignIn-place symmetric differenceModifies left operand

Standard Trait Implementations

TraitImplementationNotes
CloneDerivedBitwise copy
CopyDerivedTrivial copy semantics
DefaultDerivedCreates empty mask
EqDerivedBitwise equality
PartialEqDerivedBitwise equality
DebugCustomDisplays as "cpumask: [idx1, idx2, ...]"
HashCustomHashes underlying storage value
PartialOrdCustomCompares underlying storage value
OrdCustomCompares underlying storage value

Sources: src/lib.rs(L17)  src/lib.rs(L25 - L66)  src/lib.rs(L253 - L326) 

Array Conversion Implementations

For large CPU counts, the library provides From trait implementations for array conversions:

Array SizeCpuMask SizeConversion
[u128; 2]CpuMask<256>BidirectionalFrom
[u128; 3]CpuMask<384>BidirectionalFrom
[u128; 4]CpuMask<512>BidirectionalFrom
[u128; 5]CpuMask<640>BidirectionalFrom
[u128; 6]CpuMask<768>BidirectionalFrom
[u128; 7]CpuMask<896>BidirectionalFrom
[u128; 8]CpuMask<1024>BidirectionalFrom

These implementations enable direct conversion between CpuMask instances and their underlying array storage for sizes exceeding 128 bits.

Sources: src/lib.rs(L328 - L410)