Merge pull request #127 from teloxide/must_use_requests

Add `#[must_use]` attrs to payload setters, request wrappers and send* methods
This commit is contained in:
Waffle Maybe 2021-10-24 23:10:25 +03:00 committed by GitHub
commit 839181bffd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 15 additions and 6 deletions

View file

@ -116,6 +116,7 @@ download_forward! {
{ this => this.inner() } { this => this.inner() }
} }
#[must_use = "Futures are lazy and do nothing unless polled or awaited"]
#[pin_project::pin_project] #[pin_project::pin_project]
pub struct AutoRequest<R: Request>(#[pin] Inner<R>); pub struct AutoRequest<R: Request>(#[pin] Inner<R>);

View file

@ -123,6 +123,7 @@ download_forward! {
{ this => this.inner() } { this => this.inner() }
} }
#[must_use = "Requests are lazy and do nothing unless sent"]
pub struct CachedMeRequest<R: Request<Payload = GetMe>>(Inner<R>, GetMe); pub struct CachedMeRequest<R: Request<Payload = GetMe>>(Inner<R>, GetMe);
enum Inner<R: Request<Payload = GetMe>> { enum Inner<R: Request<Payload = GetMe>> {

View file

@ -35,6 +35,7 @@ impl<'a, E> ErasedRequester<'a, E> {
} }
/// [`Request`] with erased type. /// [`Request`] with erased type.
#[must_use = "Requests are lazy and do nothing unless sent"]
pub struct ErasedRequest<'a, T, E> { pub struct ErasedRequest<'a, T, E> {
inner: Box<dyn ErasableRequest<'a, Payload = T, Err = E> + 'a>, inner: Box<dyn ErasableRequest<'a, Payload = T, Err = E> + 'a>,
} }

View file

@ -663,6 +663,7 @@ impl From<&ChatId> for ChatIdHash {
} }
} }
#[must_use = "Requests are lazy and do nothing unless sent"]
pub struct ThrottlingRequest<R: HasPayload> { pub struct ThrottlingRequest<R: HasPayload> {
request: R, request: R,
chat_id: fn(&R::Payload) -> ChatIdHash, chat_id: fn(&R::Payload) -> ChatIdHash,

View file

@ -141,6 +141,7 @@ where
} }
} }
#[must_use = "Requests are lazy and do nothing unless sent"]
pub struct TraceRequest<R> { pub struct TraceRequest<R> {
inner: R, inner: R,
settings: Settings, settings: Settings,

View file

@ -255,6 +255,7 @@ macro_rules! impl_payload {
") field." ") field."
)] )]
#[allow(clippy::wrong_self_convention)] #[allow(clippy::wrong_self_convention)]
#[must_use = "Payloads and requests do nothing unless sent"]
fn $field<T>(mut self, value: T) -> Self fn $field<T>(mut self, value: T) -> Self
where where
T: Into<$FTy>, T: Into<$FTy>,
@ -276,6 +277,7 @@ macro_rules! impl_payload {
") field." ") field."
)] )]
#[allow(clippy::wrong_self_convention)] #[allow(clippy::wrong_self_convention)]
#[must_use = "Payloads and requests do nothing unless sent"]
fn $field<T>(mut self, value: T) -> Self fn $field<T>(mut self, value: T) -> Self
where where
T: ::core::iter::IntoIterator<Item = <$FTy as ::core::iter::IntoIterator>::Item>, T: ::core::iter::IntoIterator<Item = <$FTy as ::core::iter::IntoIterator>::Item>,
@ -297,6 +299,7 @@ macro_rules! impl_payload {
") field." ") field."
)] )]
#[allow(clippy::wrong_self_convention)] #[allow(clippy::wrong_self_convention)]
#[must_use = "Payloads and requests do nothing unless sent"]
fn $field(mut self, value: $FTy) -> Self { fn $field(mut self, value: $FTy) -> Self {
self.payload_mut().$field = Some(value); self.payload_mut().$field = Some(value);
self self
@ -315,6 +318,7 @@ macro_rules! impl_payload {
") field." ") field."
)] )]
#[allow(clippy::wrong_self_convention)] #[allow(clippy::wrong_self_convention)]
#[must_use = "Payloads and requests do nothing unless sent"]
fn $field<T>(mut self, value: T) -> Self fn $field<T>(mut self, value: T) -> Self
where where
T: Into<$FTy>, T: Into<$FTy>,
@ -336,6 +340,7 @@ macro_rules! impl_payload {
") field." ") field."
)] )]
#[allow(clippy::wrong_self_convention)] #[allow(clippy::wrong_self_convention)]
#[must_use = "Payloads and requests do nothing unless sent"]
fn $field<T>(mut self, value: T) -> Self fn $field<T>(mut self, value: T) -> Self
where where
T: ::core::iter::IntoIterator<Item = <$FTy as ::core::iter::IntoIterator>::Item>, T: ::core::iter::IntoIterator<Item = <$FTy as ::core::iter::IntoIterator>::Item>,
@ -357,6 +362,7 @@ macro_rules! impl_payload {
") field." ") field."
)] )]
#[allow(clippy::wrong_self_convention)] #[allow(clippy::wrong_self_convention)]
#[must_use = "Payloads and requests do nothing unless sent"]
fn $field(mut self, value: $FTy) -> Self { fn $field(mut self, value: $FTy) -> Self {
self.payload_mut().$field = value; self.payload_mut().$field = value;
self self

View file

@ -9,7 +9,7 @@ use crate::{
/// A ready-to-send Telegram request whose payload is sent using [JSON]. /// A ready-to-send Telegram request whose payload is sent using [JSON].
/// ///
/// [JSON]: https://core.telegram.org/bots/api#making-requests /// [JSON]: https://core.telegram.org/bots/api#making-requests
#[must_use = "requests do nothing until sent"] #[must_use = "Requests are lazy and do nothing unless sent"]
pub struct JsonRequest<P> { pub struct JsonRequest<P> {
bot: Bot, bot: Bot,
payload: P, payload: P,

View file

@ -10,7 +10,7 @@ use crate::{
/// [multipart/form-data]. /// [multipart/form-data].
/// ///
/// [multipart/form-data]: https://core.telegram.org/bots/api#making-requests /// [multipart/form-data]: https://core.telegram.org/bots/api#making-requests
#[must_use = "requests do nothing until sent"] #[must_use = "Requests are lazy and do nothing unless sent"]
pub struct MultipartRequest<P> { pub struct MultipartRequest<P> {
bot: Bot, bot: Bot,
payload: P, payload: P,

View file

@ -59,6 +59,7 @@ pub trait Request: HasPayload {
/// let _: Me = request.send().await.unwrap(); /// let _: Me = request.send().await.unwrap();
/// # }; /// # };
/// ``` /// ```
#[must_use = "Futures are lazy and do nothing unless polled or awaited"]
fn send(self) -> Self::Send; fn send(self) -> Self::Send;
/// Send this request by reference. /// Send this request by reference.
@ -86,13 +87,10 @@ pub trait Request: HasPayload {
/// } /// }
/// # }; /// # };
/// ``` /// ```
#[must_use = "Futures are lazy and do nothing unless polled or awaited"]
fn send_ref(&self) -> Self::SendRef; fn send_ref(&self) -> Self::SendRef;
#[cfg(feature = "erased")] #[cfg(feature = "erased")]
#[cfg_attr(
all(any(docsrs, dep_docsrs), feature = "nightly"),
doc(cfg(feature = "erased"))
)]
fn erase<'a>(self) -> crate::adaptors::erased::ErasedRequest<'a, Self::Payload, Self::Err> fn erase<'a>(self) -> crate::adaptors::erased::ErasedRequest<'a, Self::Payload, Self::Err>
where where
Self: Sized + 'a, Self: Sized + 'a,