pub struct ResponseFuture<T>(/* private fields */);Expand description
Future that completes when a Response is received.
§Rate limits
Requests that exceed a rate limit are automatically and immediately retried
until they succeed or fail with another error. If configured without a
RateLimiter, care must be taken that an external service intercepts and
delays these retry requests.
§Canceling a response future pre-flight
Response futures can be canceled pre-flight via
ResponseFuture::set_pre_flight. This allows you to cancel requests that
are no longer necessary once they have been cleared by the ratelimit queue,
which may be necessary in scenarios where requests are being spammed. Refer
to its documentation for more information.
§Errors
Returns an ErrorType::Parsing error type if the request failed and the
error in the response body could not be deserialized.
Returns an ErrorType::RequestCanceled error type if the request was
canceled by the user.
Returns an ErrorType::RequestError error type if creating the request
failed.
Returns an ErrorType::RequestTimedOut error type if the request timed
out. The timeout value is configured via ClientBuilder::timeout.
Returns an ErrorType::Response error type if the request failed.
Implementations§
Source§impl<T> ResponseFuture<T>
impl<T> ResponseFuture<T>
Sourcepub fn set_pre_flight<P>(&mut self, predicate: P) -> bool
pub fn set_pre_flight<P>(&mut self, predicate: P) -> bool
Set a function to call after clearing the ratelimiter but prior to sending the request to determine if the request is still valid.
This function will be a no-op if the request has failed, has already passed the ratelimiter, or if there is no ratelimiter configured.
Returns whether the pre flight function was set.
§Examples
Delete a message, but immediately before sending the request check if the request should still be sent:
use std::{
collections::HashSet,
env,
future::IntoFuture,
sync::{Arc, Mutex},
};
use twilight_http::{Client, error::ErrorType};
use twilight_model::id::Id;
let channel_id = Id::new(1);
let message_id = Id::new(2);
let channels_ignored = {
let mut map = HashSet::new();
map.insert(channel_id);
Arc::new(Mutex::new(map))
};
let client = Client::new(env::var("DISCORD_TOKEN")?);
let mut req = client.delete_message(channel_id, message_id).into_future();
let channels_ignored_clone = channels_ignored.clone();
req.set_pre_flight(move || {
// imagine you have some logic here to external state that checks
// whether the request should still be performed
let channels_ignored = channels_ignored_clone.lock().expect("channels poisoned");
!channels_ignored.contains(&channel_id)
});
// the pre-flight check will cancel the request
assert!(matches!(
req.await.unwrap_err().kind(),
ErrorType::RequestCanceled,
));Trait Implementations§
Source§impl<T: Unpin> Future for ResponseFuture<T>
impl<T: Unpin> Future for ResponseFuture<T>
Auto Trait Implementations§
impl<T> !Freeze for ResponseFuture<T>
impl<T> !RefUnwindSafe for ResponseFuture<T>
impl<T> Send for ResponseFuture<T>where
T: Send,
impl<T> !Sync for ResponseFuture<T>
impl<T> Unpin for ResponseFuture<T>where
T: Unpin,
impl<T> !UnwindSafe for ResponseFuture<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f. Read more§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll will never again be called once it has
completed. This method can be used to turn any Future into a
FusedFuture. Read more§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = ()>.§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = Never>.§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn timeout(self, timeout: Duration) -> Timeout<Self>where
Self: Sized,
fn timeout(self, timeout: Duration) -> Timeout<Self>where
Self: Sized,
tokio::time::timeout], with the advantage that it is easier to write
fluent call chains. Read more§fn timeout_at(self, deadline: Instant) -> Timeout<Self>where
Self: Sized,
fn timeout_at(self, deadline: Instant) -> Timeout<Self>where
Self: Sized,
tokio::time::timeout_at], with the advantage that it is easier to write
fluent call chains. Read more§fn with_cancellation_token(
self,
cancellation_token: &CancellationToken,
) -> WithCancellationTokenFuture<'_, Self>where
Self: Sized,
fn with_cancellation_token(
self,
cancellation_token: &CancellationToken,
) -> WithCancellationTokenFuture<'_, Self>where
Self: Sized,
CancellationToken::run_until_cancelled],
but with the advantage that it is easier to write fluent call chains. Read more§fn with_cancellation_token_owned(
self,
cancellation_token: CancellationToken,
) -> WithCancellationTokenFutureOwned<Self>where
Self: Sized,
fn with_cancellation_token_owned(
self,
cancellation_token: CancellationToken,
) -> WithCancellationTokenFutureOwned<Self>where
Self: Sized,
CancellationToken::run_until_cancelled_owned],
but with the advantage that it is easier to write fluent call chains. Read more§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<F, T, E> TryFuture for F
impl<F, T, E> TryFuture for F
§impl<Fut> TryFutureExt for Futwhere
Fut: TryFuture + ?Sized,
impl<Fut> TryFutureExt for Futwhere
Fut: TryFuture + ?Sized,
§fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>where
Self::Ok: Sink<Item, Error = Self::Error>,
Self: Sized,
fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>where
Self::Ok: Sink<Item, Error = Self::Error>,
Self: Sized,
Sink]. Read more