Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Make the trait bounds in Request{,er} stricter #12

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bot/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,8 @@ impl Bot {
}

impl Requester for Bot {
type Err = crate::errors::RequestError;

type GetMe = JsonRequest<payloads::GetMe>;

fn get_me(&self) -> JsonRequest<payloads::GetMe> {
Expand Down
2 changes: 2 additions & 0 deletions src/bot/auto_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ impl<B> AutoSend<B> {
}

impl<B: Requester> Requester for AutoSend<B> {
type Err = B::Err;

type GetMe = AutoRequest<B::GetMe>;

fn get_me(&self) -> Self::GetMe {
Expand Down
12 changes: 10 additions & 2 deletions src/bot/cache_me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ impl<B> CacheMe<B> {
}
}

impl<B: Requester> Requester for CacheMe<B> {
impl<B> Requester for CacheMe<B>
where
B: Requester,
{
type Err = B::Err;

type GetMe = CachedMeRequest<B::GetMe>;

fn get_me(&self) -> Self::GetMe {
Expand Down Expand Up @@ -85,7 +90,10 @@ enum Inner<R: Request<Payload = GetMe>> {
Pending(R, Arc<OnceCell<User>>),
}

impl<R: Request<Payload = GetMe>> Request for CachedMeRequest<R> {
impl<R> Request for CachedMeRequest<R>
where
R: Request<Payload = GetMe>,
{
type Err = R::Err;
type Send = Send<R>;
type SendRef = SendRef<R>;
Expand Down
12 changes: 9 additions & 3 deletions src/bot/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,12 @@ impl<B> Throttle<B> {
}
}

impl<B: Requester> Requester for Throttle<B> {
impl<B: Requester> Requester for Throttle<B>
where
B::SendMessage: Send,
{
type Err = B::Err;

type GetMe = B::GetMe;

fn get_me(&self) -> Self::GetMe {
Expand Down Expand Up @@ -412,8 +417,9 @@ impl<R: HasPayload> HasPayload for ThrottlingRequest<R> {
}
}

impl<R: Request> Request for ThrottlingRequest<R>
impl<R> Request for ThrottlingRequest<R>
where
R: Request + Send,
<R as HasPayload>::Payload: GetChatId,
{
type Err = R::Err;
Expand Down Expand Up @@ -617,7 +623,7 @@ mod chan_send {
pub(super) struct ChanSend(#[pin] Inner);

#[cfg(not(feature = "nightly"))]
type Inner = Pin<Box<dyn Future<Output = Result<(), SendError<(Id, Sender<Never>)>>>>>;
type Inner = Pin<Box<dyn Future<Output = Result<(), SendError<(Id, Sender<Never>)>>> + Send>>;
#[cfg(feature = "nightly")]
type Inner = impl Future<Output = Result<(), SendError<(Id, Sender<Never>)>>>;

Expand Down
2 changes: 1 addition & 1 deletion src/local_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ macro_rules! req_future {

#[cfg(not(feature = "nightly"))]
pub(crate) type $i<$T>
$(where $($wh)*)? = ::core::pin::Pin<Box<dyn ::core::future::Future<Output = $Out>>>;
$(where $($wh)*)? = ::core::pin::Pin<Box<dyn ::core::future::Future<Output = $Out> + ::core::marker::Send + 'static>>;

#[cfg(not(feature = "nightly"))]
pub(crate) fn def<$T>($( $arg: $ArgTy ),*) -> $i<$T>
Expand Down
6 changes: 3 additions & 3 deletions src/requests/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ pub trait Request: HasPayload {
*/

/// Type of error that may happen during sending the request to telegram.
type Err: std::error::Error;
type Err: std::error::Error + Send;

/// Type of future returned by [`send`](Request::send) method.
type Send: Future<Output = Result<Output<Self>, Self::Err>>;
type Send: Future<Output = Result<Output<Self>, Self::Err>> + Send;

/// Type of future returned by [`send_ref`](Request::send_ref) method.
///
/// NOTE: it intentionally forbids borrowing from self
// though anyway we couldn't allow borrowing without GATs :sob:
type SendRef: Future<Output = Result<Output<Self>, Self::Err>>;
type SendRef: Future<Output = Result<Output<Self>, Self::Err>> + Send;

/// Send the request.
///
Expand Down
6 changes: 4 additions & 2 deletions src/requests/requester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ use crate::{
/// _This trait is included in the crate's [`prelude`](crate::prelude)_.
#[cfg_attr(all(docsrs, feature = "nightly"), doc(spotlight))]
pub trait Requester {
type GetMe: Request<Payload = GetMe>;
type Err: std::error::Error + Send;

type GetMe: Request<Payload = GetMe, Err = Self::Err>;

/// For telegram documentation of the method see [`GetMe`].
fn get_me(&self) -> Self::GetMe;

type SendMessage: Request<Payload = SendMessage>;
type SendMessage: Request<Payload = SendMessage, Err = Self::Err>;

/// For telegram documentation of the method see [`SendMessage`].
fn send_message<C, T>(&self, chat_id: C, text: T) -> Self::SendMessage
Expand Down