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::types::User;
#[derive(Debug, Constructor)]
pub struct GetMe {
info: RequestInfo,
#[derive(Debug, Constructor, TypedBuilder)]
pub struct GetMe<'a> {
info: RequestInfo<'a>,
}
impl Request for GetMe {
impl<'a> Request<'a> for GetMe<'a> {
type ReturnValue = User;
fn send(self) -> RequestFuture<ResponseResult<Self::ReturnValue>> {
fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>> {
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.
/// `ReturnValue` - a type that will be returned from Telegram.
pub trait Request {
pub trait Request<'a> {
type ReturnValue: DeserializeOwned;
/// 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?
#[derive(Debug)]
pub struct RequestInfo {
pub client: Client,
pub token: String,
pub struct RequestInfo<'a> {
pub client: &'a Client,
pub token: &'a str,
}
/// 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};
#[derive(Debug, TypedBuilder)]
pub struct SendMessage {
info: RequestInfo,
pub struct SendMessage<'a> {
info: RequestInfo<'a>,
chat_id: ChatId,
text: String,
@ -21,10 +21,10 @@ pub struct SendMessage {
reply_markup: Option<()>, // TODO: ReplyMarkup enum
}
impl Request for SendMessage {
impl<'a> Request<'a> for SendMessage<'a> {
type ReturnValue = Message;
fn send(self) -> RequestFuture<ResponseResult<Self::ReturnValue>> {
fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>> {
Box::pin(async move {
let params = FormBuilder::new()
.add("chat_id", &self.chat_id)