Skip to content

API Design: Factory Pattern Friction With Rust #23

@officialfrancismendoza

Description

@officialfrancismendoza

Problem

Rust is a multi-paradigm systems programming language that is not purely object-oriented. In comparison to other Bouncy Castle cryptography libraries that are expressed in native object-oriented languages (C# and Java, for example), Rust is unique given its strict nature.

As part of API design, factories are a contentious issue. In C# and Java counterparts, they are a utility class that creates cryptographic objects from encoded data without the caller needing to know the exact implementation type, which follows a factory pattern. They both use runtime data-driven dispatch (read and parse metadata, find discriminator field, choose constructor based on that value) to determine what specific cryptographic constructor to use.

Rust does not naturally model this pattern in the same way as Java or C#. As part of API design, we need to determine how factory-style decoding should be represented:

  • Enum-based return types
  • Trait objects
  • Algorithm-specific constructors only
  • Some hybrid approach

The goal is to provide a Rust-idiomatic API while preserving support for decoding algorithm-agnostic encoded data.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionThis will involve some community discussion to choose the right designdocumentationImprovements or additions to documentationhelp wantedCould be picked up by anyone in the community

    Type

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions