Skip to content

Consistent conversion functions #1462

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
leighmcculloch opened this issue Apr 29, 2025 · 0 comments
Open

Consistent conversion functions #1462

leighmcculloch opened this issue Apr 29, 2025 · 0 comments

Comments

@leighmcculloch
Copy link
Member

The SDK contains many conversion functions from one type to another and so on. Some conversions are implemented as .to_*, .into_*, and .from_* functions, while others are implemented as Into/From impls, and then some are implemented as both.

I think we should strive to build some consistency with conversions so that every conversion has:

  • A .to_ on the source type
  • A .into_ on the source type if the source can be consumed and that's a meaningful optimisation
  • A .from_ on the destination type
  • A From impl on the destination type, or if not possible a Into impl on the source type, that call to .from_/.to_ above
  • A From/Into impl on the borrowed type

Some of this naming and pattern is derived from the Rust Naming conventions for conversions:

Prefix Cost Ownership
as_ Free borrowed -> borrowed
to_ Expensive borrowed -> borrowed
borrowed -> owned (non-Copy types)
owned -> owned (Copy types)
into_ Variable owned -> owned (non-Copy types)

Ref: https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv

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

No branches or pull requests

1 participant