forked from twilight-rs/twilight
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!(http): add a
ResponseFuture
strcut (twilight-rs#1008)
We currently take owned heap allocated types, such as Vecs and Strings. This is due to the lifetimes around requests, their inner polled futures, and the caller are difficult to work around. The way requests currently work is that they implement `std::future::Future` and "start" a future when initially polled for the first time. They then store this future internally in a field like so: ```rust pub struct SomeRequest { fut: Option<Pending<'a, Message>>, // more fields... } ``` This requires a lot of boilerplate on our part: we have a macro implementing `Future` on every request with more or less unique implementations and need fields for the inner future. For users, this design comes at a cost of performance and clarity. Users need to give us owned arguments. It is also impossible for users to determine whether a request is in flight using the type system due to that information being hidden. We need to clone arguments if we want to avoid panics from users accidentally double polling a request after completion, when it may not be clear that a request has been used due to the request not being consumed. Instead, we can turn the inner request functions that "start" a future into ones that consume and execute the request and then return a future resolving to a response directly to the user. The new `response::ResponseFuture` implements `Poll` and will begin the execution of a request - if a failure did not occur during construction - while polling it to completion, providing the resulting `response::Response`. Internally the implementation of the `CreateMessage::exec` function looks like this: ```rust pub fn exec(self) -> ResponseFuture<Message> { let request = Request::from_route(Route::GetMessage { channel_id: self.channel_id.0, message_id: self.message_id.0, }); self.http.request(request) } ``` The usage of executing the request and printing the message contents looks like this: ```rust let msg = client.message(channel_id, message_id).exec().await?; let message = response.model().await?; println!("content: {}", message.content); ``` This work does not include the optimization work that turns all of our owned parameters to borrowed ones and will be done by a separate PR. Signed-off-by: Zeyla Hellyer <zeyla@hellyer.dev>
- Loading branch information
1 parent
7650a9d
commit 6d17ec5
Showing
149 changed files
with
2,160 additions
and
2,192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.