Skip to content
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

Trait for async API clients #85

Merged
merged 1 commit into from
Jan 10, 2020
Merged

Trait for async API clients #85

merged 1 commit into from
Jan 10, 2020

Conversation

adamchalmers
Copy link
Contributor

@adamchalmers adamchalmers commented Jan 9, 2020

This PR

  • Adds an async API trait
  • Makes the async API client impl it
  • Makes the mock API client impl it
  • Updates the e2e tests, to demonstrate that functions can take the async API trait as a parameter (instead of the async API client).

Note this adds a slight bit of overhead for using the async client, because of dyn, which has to box the concrete type and put it on the heap (I think). But that's going to be dominated by the cost of the network calls and possibly the serializing/deserializing JSON

// Currently, futures/async fns aren't supported in traits, and won't be for a while.
// So unlike ApiClient/HttpApiClient, there's no trait with the request method.
pub async fn request<ResultType, QueryType, BodyType>(
#[async_trait]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the language will ever support this directly - I wasn't able to find anything in the rust-lang issue tracker.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The async book mentions it, and this article explains why it's hard. There's two features that probably have to land before async traits are properly supported: 1 and 2

@adamchalmers adamchalmers merged commit 61f6edc into master Jan 10, 2020
@delete-merged-branch delete-merged-branch bot deleted the achalmers/async-trait branch January 10, 2020 16:43
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

Successfully merging this pull request may close these issues.

None yet

3 participants