Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive bitmask parsing support to BinaryParseKit, enabling bit-level field parsing and manipulation for binary protocols. The implementation provides three main macros (@mask(bitCount:), @mask(), and @ParseBitmask) along with supporting infrastructure for MSB-first bit parsing, printing, and round-trip serialization.
Key Changes
- Introduced
RawBitstype for arbitrary-width bit storage and manipulation - Added protocols (
ExpressibleByRawBits,BitCountProviding,RawBitsConvertible) for bit-level parsing - Implemented macro support for bitmask parsing in structs and enums with automatic grouping and padding
Reviewed changes
Copilot reviewed 56 out of 57 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| Sources/BinaryParseKit/Types/RawBits.swift | Core bit manipulation type with slicing, equality, and bitwise operations |
| Sources/BinaryParseKit/Protocols/BitmaskParsable.swift | Protocol definitions for bit-level parsing and conversion |
| Sources/BinaryParseKitMacros/Macros/ParseBitmask/*.swift | Macro implementation for @ParseBitmask struct parsing |
| Sources/BinaryParseKitMacros/Macros/Supports/MaskMacroInfo.swift | Metadata and parsing logic for @mask attributes |
| Sources/BinaryParseKitMacros/Macros/Supports/Utilities.swift | Utility functions for generating mask parsing and printing code |
| Sources/BinaryParseKit/Extensions/ExpressibleByRawBits+.swift | Built-in conformances for Bool, UInt8, Int8 |
| Tests/BinaryParseKitTests/*.swift | Comprehensive test coverage for RawBits, parsing, and printing |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
b684376 to
e66e042
Compare
1ba191f to
e4684eb
Compare
7f54f7f to
8c4e37a
Compare
630ff88 to
d88bed7
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 80 out of 81 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This PR adds bitmask parsing support
Goal
The design of bitmask parsing is to achieve
@ParseEnumand@ParseStruct@ParseBitmaskDesign
We assume that is that each single bitmask are usually less than 64/128 bits. Therefore, when parsing bitmask, we extract bits into integers, and parse based on the extracted integers.
The package provides
ExpressibleByRawBitsprotocol which requires a constructor taking in aFixedWidthInteger.Types who conform to this type can be used with
@mask(bitCount:)macro. If the type additionally conforms toBitCountProviding, it can be used with@mask()where bitCount is inferred fromBitCountProviding.bitCount, or@mask(bitCount:)(where the provided bitCount has to be greater than or equal toBitCountProviding.bitCount).@maskcan be used in@ParseEnum,@ParseStruct, and the new@ParseBitmask.@ParseBitmaskcan be applied on a struct and automatically generateExpressibleByRawBitsconformance.Consecutive bitmasks are parsed "together" and any remaining bits of an unfinished byte will be dropped/padded automatically. That is, say
will take two bytes, where the first 3 bits parsed into Flag; the following 7 bits parsed into Sequence, and the remaining 6 bits dropped.