Skip to content

rust: forbid late registration after resolver snapshot initialization #3417

@chaokunyang

Description

@chaokunyang

Feature Request

Add an explicit lifecycle guard in Fory Rust to forbid type registrations after the resolver snapshot is initialized.

Is your feature request related to a problem? Please describe

Yes. In Rust, Fory caches a finalized type resolver in OnceLock (final_type_resolver).
After the first serialize/deserialize call, that snapshot is fixed. If users call register* methods later, those new registrations do not affect the already-initialized resolver snapshot, which can lead to confusing behavior (types appear registered but runtime still fails to resolve them).

This creates a lifecycle footgun because registration order silently changes behavior.

Describe the solution you'd like

Forbid late registration with an explicit error.

Specifically:

  • In all registration entry points (register, register_union, register_by_namespace, register_by_name, register_serializer*, register_generic_trait), detect whether final_type_resolver has already been initialized.
  • If initialized, return a clear error (for example Error::not_allowed) explaining that registrations must be completed before first serialization/deserialization.
  • Keep behavior deterministic and fail-fast instead of silently ignoring post-initialization registrations.

Describe alternatives you've considered

  • Rebuild the final resolver whenever new registrations are added after initialization.
    • This is more complex, risks performance regressions, and can introduce thread-safety/lifecycle complexity.
  • Keep current behavior and document it only.
    • This still leaves a sharp edge and does not protect users from accidental misuse.

Additional context

Relevant code path is in Rust fory-core where Fory uses OnceLock<Result<TypeResolver, Error>> for final_type_resolver and registration APIs mutate type_resolver.

A fail-fast guard would make lifecycle semantics explicit and safer for production usage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions