awslabs / awslabs/aws-sdk-rust

[request]: Accept borrowed data types as inputs

Open
#297 2 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request p2
Dominant language
Rust
Stars
3.3k
Forks
290
Avg merge
1d 12h
Merged PRs (30d)
3

Description

Community Note
  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue, please leave a comment

Tell us about your request
Would it be possible to accept borrowed inputs for builders? That is, instead of always making an allocation when accepting a string, make it possible for the caller to provide a borrowed type.

The allocations may seem negligible, but as a foundational library, aws-sdk-rust might as well design its APIs to be as low-cost as possible, while still being ergonomic.

Concrete example

https://docs.rs/aws-sdk-dynamodb/0.0.25-alpha/aws_sdk_dynamodb/client/fluent_builders/struct.GetItem.html#method.table_name

Every time GetItem::table_name is called, a String allocation needs to be made. If I have a &'static str it feels wasteful to incur an allocation for a String.

Tell us about the problem you're trying to solve. What are you trying to do, and why is it hard?
I would like to not pay the price of allocations when that is not needed.

AFAICT there is no inherent need for the builder to own the input data. The builder only needs read-access for constructing a request to the AWS API.

Solution ideas

I'll use &str and String as examples, but also applies for other data types.

Made a small Playground link for exploring different ideas: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e61a33a1aa8b631aa6b328ea93fd6b4b

For all the new designs, the builder struct will need a lifetime parameter, which is a bit less ergonomic versus the status-quo.

  • set_string is the current API. Storage in builder is 24 bytes.
  • set_cow uses a Cow<'a, str> type as storage, which is 32 bytes. The caller can provide both owned types and borrowed types.
  • set_borrowed uses a &str type as storage, which is 16 bytes. I don't know how to lose the &* in the call using a String.
  • set_str arguable the simplest implementation, but also least generic. Only allows passing borrowed data. It does clearly tell the caller that the library only needs read-access.

Are you currently working around this issue?
The current workaround is incurring the allocation cost.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked aws_sdk_dynamodb GetItem fluent builder example and the Rust Playground comparison of String, Cow, and borrowed storage. Determine which borrowed-input API and lifetime trade-offs maintainers would accept; done means an agreed design for affected builders, with its scope and validation approach identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.