Borrow client and token in RequestInfo instead of moving. (this also adds lifetimes to Request, RequestInfo, RequestFuture)

This commit is contained in:
Waffle 2019-09-04 10:34:52 +03:00
parent 4fc11dc3dc
commit fe48ed2730
3 changed files with 16 additions and 16 deletions

View file

@ -2,17 +2,17 @@ use crate::core::network;
use crate::core::requests::{Request, RequestFuture, RequestInfo, ResponseResult}; use crate::core::requests::{Request, RequestFuture, RequestInfo, ResponseResult};
use crate::core::types::User; use crate::core::types::User;
#[derive(Debug, Constructor)] #[derive(Debug, Constructor, TypedBuilder)]
pub struct GetMe { pub struct GetMe<'a> {
info: RequestInfo, info: RequestInfo<'a>,
} }
impl Request for GetMe { impl<'a> Request<'a> for GetMe<'a> {
type ReturnValue = User; type ReturnValue = User;
fn send(self) -> RequestFuture<ResponseResult<Self::ReturnValue>> { fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>> {
Box::pin(async move { Box::pin(async move {
network::request(&self.info.client, &self.info.token, "getMe", None).await network::request(self.info.client, self.info.token, "getMe", None).await
}) })
} }
} }

View file

@ -36,22 +36,22 @@ pub type ResponseResult<T> = Result<T, RequestError>;
/// Request that can be sent to telegram. /// Request that can be sent to telegram.
/// `ReturnValue` - a type that will be returned from Telegram. /// `ReturnValue` - a type that will be returned from Telegram.
pub trait Request { pub trait Request<'a> {
type ReturnValue: DeserializeOwned; type ReturnValue: DeserializeOwned;
/// Send request to telegram /// Send request to telegram
fn send(self) -> RequestFuture<ResponseResult<Self::ReturnValue>>; fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>>;
} }
pub type RequestFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>; pub type RequestFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
// todo: better name? // todo: better name?
#[derive(Debug)] #[derive(Debug)]
pub struct RequestInfo { pub struct RequestInfo<'a> {
pub client: Client, pub client: &'a Client,
pub token: String, pub token: &'a str,
} }
/// Unique identifier for the target chat or username of the target channel (in /// Unique identifier for the target chat or username of the target channel (in

View file

@ -3,8 +3,8 @@ use crate::core::requests::{ChatId, Request, RequestFuture, RequestInfo, Respons
use crate::core::{network, types::Message}; use crate::core::{network, types::Message};
#[derive(Debug, TypedBuilder)] #[derive(Debug, TypedBuilder)]
pub struct SendMessage { pub struct SendMessage<'a> {
info: RequestInfo, info: RequestInfo<'a>,
chat_id: ChatId, chat_id: ChatId,
text: String, text: String,
@ -21,10 +21,10 @@ pub struct SendMessage {
reply_markup: Option<()>, // TODO: ReplyMarkup enum reply_markup: Option<()>, // TODO: ReplyMarkup enum
} }
impl Request for SendMessage { impl<'a> Request<'a> for SendMessage<'a> {
type ReturnValue = Message; type ReturnValue = Message;
fn send(self) -> RequestFuture<ResponseResult<Self::ReturnValue>> { fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>> {
Box::pin(async move { Box::pin(async move {
let params = FormBuilder::new() let params = FormBuilder::new()
.add("chat_id", &self.chat_id) .add("chat_id", &self.chat_id)