You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
HashFactory hand-wrote an 8-arm match for each of the 10 Hash trait methods plus the string-name lookup in AlgorithmFactory::new, so adding an algorithm meant touching a dozen places. Add a bouncycastle-factory-macros crate providing two derives that generate all of it:
Hash - the Hash impl, forwarding every method to the
variant currently held.
AlgorithmFactory - Default and AlgorithmFactory (new, default_128_bit,
default_256_bit), driven by a #[factory(name = ...)]
helper attribute on each variant.
Adding an algorithm is now one variant plus one attribute. hash_factory.rs drops from 254 to 79 lines with no behaviour change; existing tests and doctests cover the generated code.
Each derive is named after the trait it implements - derives live in the macro namespace and traits in the type namespace, so they don't collide (same convention as serde::Serialize).
Also marks HashFactory #[non_exhaustive] so future variants are additive rather than breaking for downstream matches
I didn't know about the #[non_exhaustive] attribute. That's great. Regardless of whether we macro the factories or not, we should apply that, and I will especially be adding #[non_exhaustive] to all the error type enums.
I particularly like that it doesn't prevent you, inside the crate, from writing exhaustive matches
for example:
which will break and force you to add a new branch if you add a new type to the struct, but any caller outside the library would be required to add the _ = branch if they tried to do the same thing. Brilliant! Thanks for pointing that out.
HashFactory hand-wrote an 8-arm match for each of the 10 Hash trait
methods plus the string-name lookup in AlgorithmFactory::new, so adding
an algorithm meant touching a dozen places. Add a bouncycastle-factory-macros
crate providing two derives that generate all of it:
* Hash - the Hash impl, forwarding every method to the
variant currently held.
* AlgorithmFactory - Default and AlgorithmFactory (new, default_128_bit,
default_256_bit), driven by a #[factory(name = ...)]
helper attribute on each variant.
Adding an algorithm is now one variant plus one attribute. hash_factory.rs
drops from 254 to 79 lines with no behaviour change; existing tests and
doctests cover the generated code.
Each derive is named after the trait it implements - derives live in the
macro namespace and traits in the type namespace, so they don't collide
(same convention as serde::Serialize).
Also marks HashFactory #[non_exhaustive] so future variants are additive
rather than breaking for downstream matches
Fixes: bcgit#66
Signed-off-by: Nikola Pajkovsky <nikolap@openssl.org>
npajkovsky
changed the title
Replace HashFactory boilerplate with derive macrosReplace Factory boilerplate with derive macrosAug 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
HashFactory hand-wrote an 8-arm match for each of the 10 Hash trait methods plus the string-name lookup in AlgorithmFactory::new, so adding an algorithm meant touching a dozen places. Add a bouncycastle-factory-macros crate providing two derives that generate all of it:
variant currently held.
default_256_bit), driven by a #[factory(name = ...)]
helper attribute on each variant.
Adding an algorithm is now one variant plus one attribute. hash_factory.rs drops from 254 to 79 lines with no behaviour change; existing tests and doctests cover the generated code.
Each derive is named after the trait it implements - derives live in the macro namespace and traits in the type namespace, so they don't collide (same convention as serde::Serialize).
Also marks HashFactory #[non_exhaustive] so future variants are additive rather than breaking for downstream matches
Fixes: #66