Skip to content

blobby: replace iterators with const fn parsing #1187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

newpavlov
Copy link
Member

@newpavlov newpavlov commented Jun 23, 2025

The new approach parses blb files into statics which allows to use the crate in no_std contexts. This PR additionally introduces the parse_into_structs macro which defines a struct with byte slice fields and transforms blobby data into list of these structs.

For example, it allows us to write:

blobby::parse_into_structs!(
    include_bytes!("/path/to/file.blb");
    static TEST_VECTORS;
    struct TestVector { key, nonce, aad, plaintext, ciphertext }
);

// transforms into:
struct TestVector {
    key: &'static [u8],
    nonce: &'static [u8],
    aad: &'static [u8],
    plaintext: &'static [u8],
    ciphertext: &'static [u8]
}

static TEST_VECTORS: &[TestVector] = { ... };

I also thought about adding support for byte array fields, but with the current Rust capabilities (i.e. without const traits) the resulting code becomes annoyingly complex, so I decided against it in the current version.

@newpavlov newpavlov requested a review from tarcieri June 23, 2025 15:40
read_vlq(&mut data)
}

pub const fn parse_items_len(mut data: &[u8]) -> Result<usize, Error> {
Copy link
Member Author

@newpavlov newpavlov Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, with the current blb format to get number of items stored in a blb file we have to fully parse it. In other words, parse_into_slice effectively parses the file twice, which may be noticeably slow on large enough files. We could resolve this by adding number of stored items to the start of the file, but it would mean that we have to re-generate our existing blb files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant