pub struct ResponseFuture<T> { /* private fields */ }
Expand description
Future that will resolve to a Response
.
§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::Json
error type if serializing the response body
of the request failed.
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.
Returns an ErrorType::ServiceUnavailable
error type if the Discord API
is unavailable.
Implementations§
source§impl<T> ResponseFuture<T>
impl<T> ResponseFuture<T>
sourcepub fn set_pre_flight(
&mut self,
pre_flight: Box<dyn FnOnce() -> bool + Send + 'static>,
) -> bool
pub fn set_pre_flight( &mut self, pre_flight: Box<dyn FnOnce() -> bool + Send + 'static>, ) -> 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::{error::ErrorType, Client};
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(Box::new(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> 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<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