awslabs / awslabs/aws-sdk-rust
[request]: Accept borrowed data types as inputs
Nobody has claimed this yet.
- 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
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_stringis the current API. Storage in builder is 24 bytes.set_cowuses aCow<'a, str>type as storage, which is 32 bytes. The caller can provide both owned types and borrowed types.set_borroweduses a&strtype as storage, which is 16 bytes. I don't know how to lose the&*in the call using aString.set_strarguable 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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