Skip to content

Commit 33d45c0

Browse files
n1ghtmarecaspervonb
andcommitted
Make Client field in Request a reference
Co-Authored-By: Casper Beyer <[email protected]>
1 parent e7ce05b commit 33d45c0

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

async-nats/src/client.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl Client {
302302
/// # }
303303
/// ```
304304
pub fn request(&self, subject: String, payload: Bytes) -> Request {
305-
Request::new(self.clone(), subject, payload)
305+
Request::new(self, subject, payload)
306306
}
307307

308308
/// Sends the request with headers.
@@ -325,7 +325,7 @@ impl Client {
325325
headers: HeaderMap,
326326
payload: Bytes,
327327
) -> Result<Message, Error> {
328-
let message = Request::new(self.clone(), subject, payload)
328+
let message = Request::new(self, subject, payload)
329329
.headers(headers)
330330
.await?;
331331

@@ -455,17 +455,17 @@ impl Client {
455455

456456
/// Used for building and sending requests.
457457
#[derive(Debug)]
458-
pub struct Request {
459-
client: Client,
458+
pub struct Request<'a> {
459+
client: &'a Client,
460460
subject: String,
461461
payload: Option<Bytes>,
462462
headers: Option<HeaderMap>,
463463
timeout: Option<Option<Duration>>,
464464
inbox: Option<String>,
465465
}
466466

467-
impl Request {
468-
pub fn new(client: Client, subject: String, payload: Bytes) -> Request {
467+
impl<'a> Request<'a> {
468+
pub fn new(client: &'a Client, subject: String, payload: Bytes) -> Request<'a> {
469469
Request {
470470
client,
471471
subject,
@@ -487,7 +487,7 @@ impl Request {
487487
/// # Ok(())
488488
/// # }
489489
/// ```
490-
pub fn payload(mut self, payload: Bytes) -> Request {
490+
pub fn payload(mut self, payload: Bytes) -> Request<'a> {
491491
self.payload = Some(payload);
492492
self
493493
}
@@ -510,7 +510,7 @@ impl Request {
510510
/// # Ok(())
511511
/// # }
512512
/// ```
513-
pub fn headers(mut self, headers: HeaderMap) -> Request {
513+
pub fn headers(mut self, headers: HeaderMap) -> Request<'a> {
514514
self.headers = Some(headers);
515515
self
516516
}
@@ -531,7 +531,7 @@ impl Request {
531531
/// # Ok(())
532532
/// # }
533533
/// ```
534-
pub fn timeout(mut self, timeout: Option<Duration>) -> Request {
534+
pub fn timeout(mut self, timeout: Option<Duration>) -> Request<'a> {
535535
self.timeout = Some(timeout);
536536
self
537537
}
@@ -550,7 +550,7 @@ impl Request {
550550
/// # Ok(())
551551
/// # }
552552
/// ```
553-
pub fn inbox(mut self, inbox: String) -> Request {
553+
pub fn inbox(mut self, inbox: String) -> Request<'a> {
554554
self.inbox = Some(inbox);
555555
self
556556
}
@@ -561,6 +561,7 @@ impl Request {
561561
let mut publish = self
562562
.client
563563
.publish(self.subject, self.payload.unwrap_or_else(Bytes::new));
564+
564565
if let Some(headers) = self.headers {
565566
publish = publish.headers(headers);
566567
}
@@ -598,11 +599,11 @@ impl Request {
598599
}
599600
}
600601

601-
impl IntoFuture for Request {
602+
impl<'a> IntoFuture for Request<'a> {
602603
type Output = Result<Message, Error>;
603-
type IntoFuture = Pin<Box<dyn Future<Output = Result<Message, Error>> + Send>>;
604+
type IntoFuture = Pin<Box<dyn Future<Output = Result<Message, Error>> + Send + 'a>>;
604605

605606
fn into_future(self) -> Self::IntoFuture {
606-
Box::pin(self.send())
607+
self.send().boxed()
607608
}
608609
}

async-nats/src/jetstream/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ impl<'a, T: Sized + Serialize, V: DeserializeOwned> Request<'a, T, V> {
11461146
impl<'a, T: Sized + Serialize, V: DeserializeOwned + Send> IntoFuture for Request<'a, T, V> {
11471147
type Output = Result<Response<V>, Error>;
11481148

1149-
type IntoFuture = Pin<Box<dyn Future<Output = Result<Response<V>, Error>> + Send>>;
1149+
type IntoFuture = Pin<Box<dyn Future<Output = Result<Response<V>, Error>> + Send + 'a>>;
11501150

11511151
fn into_future(self) -> Self::IntoFuture {
11521152
serde_json::to_vec(&self.payload)

0 commit comments

Comments
 (0)