@@ -4,7 +4,7 @@ use std::time::Duration;
4
4
5
5
use crate :: mqttbytes:: { v4:: * , QoS } ;
6
6
use crate :: {
7
- valid_filter, valid_topic, AckPromise , ConnectionError , Event , EventLoop , MqttOptions ,
7
+ valid_filter, valid_topic, AckPromise , ConnectionError , Event , EventLoop , MqttOptions , Pending ,
8
8
PromiseTx , Request ,
9
9
} ;
10
10
@@ -23,15 +23,15 @@ pub enum ClientError {
23
23
TryRequest ( Request ) ,
24
24
}
25
25
26
- impl From < SendError < ( Request , Option < PromiseTx > ) > > for ClientError {
27
- fn from ( e : SendError < ( Request , Option < PromiseTx > ) > ) -> Self {
28
- Self :: Request ( e. into_inner ( ) . 0 )
26
+ impl From < SendError < Pending < Request > > > for ClientError {
27
+ fn from ( e : SendError < Pending < Request > > ) -> Self {
28
+ Self :: Request ( e. into_inner ( ) . request )
29
29
}
30
30
}
31
31
32
- impl From < TrySendError < ( Request , Option < PromiseTx > ) > > for ClientError {
33
- fn from ( e : TrySendError < ( Request , Option < PromiseTx > ) > ) -> Self {
34
- Self :: TryRequest ( e. into_inner ( ) . 0 )
32
+ impl From < TrySendError < Pending < Request > > > for ClientError {
33
+ fn from ( e : TrySendError < Pending < Request > > ) -> Self {
34
+ Self :: TryRequest ( e. into_inner ( ) . request )
35
35
}
36
36
}
37
37
@@ -44,7 +44,7 @@ impl From<TrySendError<(Request, Option<PromiseTx>)>> for ClientError {
44
44
/// from the broker, i.e. move ahead.
45
45
#[ derive( Clone , Debug ) ]
46
46
pub struct AsyncClient {
47
- request_tx : Sender < ( Request , Option < PromiseTx > ) > ,
47
+ request_tx : Sender < Pending < Request > > ,
48
48
}
49
49
50
50
impl AsyncClient {
@@ -64,7 +64,7 @@ impl AsyncClient {
64
64
///
65
65
/// This is mostly useful for creating a test instance where you can
66
66
/// listen on the corresponding receiver.
67
- pub fn from_senders ( request_tx : Sender < ( Request , Option < PromiseTx > ) > ) -> AsyncClient {
67
+ pub fn from_senders ( request_tx : Sender < Pending < Request > > ) -> AsyncClient {
68
68
AsyncClient { request_tx }
69
69
}
70
70
@@ -84,12 +84,11 @@ impl AsyncClient {
84
84
let topic = topic. into ( ) ;
85
85
let mut publish = Publish :: new ( & topic, qos, payload) ;
86
86
publish. retain = retain;
87
- let publish = Request :: Publish ( publish) ;
88
87
if !valid_topic ( & topic) {
89
- return Err ( ClientError :: Request ( publish) ) ;
88
+ return Err ( ClientError :: Request ( publish. into ( ) ) ) ;
90
89
}
91
90
self . request_tx
92
- . send_async ( ( publish, Some ( promise_tx) ) )
91
+ . send_async ( Pending :: new ( publish. into ( ) , promise_tx) )
93
92
. await ?;
94
93
95
94
Ok ( promise)
@@ -111,11 +110,11 @@ impl AsyncClient {
111
110
let topic = topic. into ( ) ;
112
111
let mut publish = Publish :: new ( & topic, qos, payload) ;
113
112
publish. retain = retain;
114
- let publish = Request :: Publish ( publish) ;
115
113
if !valid_topic ( & topic) {
116
- return Err ( ClientError :: TryRequest ( publish) ) ;
114
+ return Err ( ClientError :: TryRequest ( publish. into ( ) ) ) ;
117
115
}
118
- self . request_tx . try_send ( ( publish, Some ( promise_tx) ) ) ?;
116
+ self . request_tx
117
+ . try_send ( Pending :: new ( publish. into ( ) , promise_tx) ) ?;
119
118
120
119
Ok ( promise)
121
120
}
@@ -125,7 +124,9 @@ impl AsyncClient {
125
124
let ack = get_ack_req ( publish) ;
126
125
127
126
if let Some ( ack) = ack {
128
- self . request_tx . send_async ( ( ack, None ) ) . await ?;
127
+ self . request_tx
128
+ . send_async ( Pending :: no_promises ( ack) )
129
+ . await ?;
129
130
}
130
131
131
132
Ok ( ( ) )
@@ -135,7 +136,7 @@ impl AsyncClient {
135
136
pub fn try_ack ( & self , publish : & Publish ) -> Result < ( ) , ClientError > {
136
137
let ack = get_ack_req ( publish) ;
137
138
if let Some ( ack) = ack {
138
- self . request_tx . try_send ( ( ack, None ) ) ?;
139
+ self . request_tx . try_send ( Pending :: no_promises ( ack) ) ?;
139
140
}
140
141
141
142
Ok ( ( ) )
@@ -155,9 +156,8 @@ impl AsyncClient {
155
156
let ( promise_tx, promise) = PromiseTx :: new ( ) ;
156
157
let mut publish = Publish :: from_bytes ( topic, qos, payload) ;
157
158
publish. retain = retain;
158
- let publish = Request :: Publish ( publish) ;
159
159
self . request_tx
160
- . send_async ( ( publish, Some ( promise_tx) ) )
160
+ . send_async ( Pending :: new ( publish. into ( ) , promise_tx) )
161
161
. await ?;
162
162
163
163
Ok ( promise)
@@ -175,7 +175,7 @@ impl AsyncClient {
175
175
return Err ( ClientError :: Request ( subscribe. into ( ) ) ) ;
176
176
}
177
177
self . request_tx
178
- . send_async ( ( subscribe. into ( ) , Some ( promise_tx) ) )
178
+ . send_async ( Pending :: new ( subscribe. into ( ) , promise_tx) )
179
179
. await ?;
180
180
181
181
Ok ( promise)
@@ -193,7 +193,7 @@ impl AsyncClient {
193
193
return Err ( ClientError :: TryRequest ( subscribe. into ( ) ) ) ;
194
194
}
195
195
self . request_tx
196
- . try_send ( ( subscribe. into ( ) , Some ( promise_tx) ) ) ?;
196
+ . try_send ( Pending :: new ( subscribe. into ( ) , promise_tx) ) ?;
197
197
198
198
Ok ( promise)
199
199
}
@@ -209,7 +209,7 @@ impl AsyncClient {
209
209
return Err ( ClientError :: Request ( subscribe. into ( ) ) ) ;
210
210
}
211
211
self . request_tx
212
- . send_async ( ( subscribe. into ( ) , Some ( promise_tx) ) )
212
+ . send_async ( Pending :: new ( subscribe. into ( ) , promise_tx) )
213
213
. await ?;
214
214
215
215
Ok ( promise)
@@ -226,7 +226,7 @@ impl AsyncClient {
226
226
return Err ( ClientError :: TryRequest ( subscribe. into ( ) ) ) ;
227
227
}
228
228
self . request_tx
229
- . try_send ( ( subscribe. into ( ) , Some ( promise_tx) ) ) ?;
229
+ . try_send ( Pending :: new ( subscribe. into ( ) , promise_tx) ) ?;
230
230
231
231
Ok ( promise)
232
232
}
@@ -236,7 +236,7 @@ impl AsyncClient {
236
236
let ( promise_tx, promise) = PromiseTx :: new ( ) ;
237
237
let unsubscribe = Unsubscribe :: new ( topic. into ( ) ) ;
238
238
self . request_tx
239
- . send_async ( ( unsubscribe. into ( ) , Some ( promise_tx) ) )
239
+ . send_async ( Pending :: new ( unsubscribe. into ( ) , promise_tx) )
240
240
. await ?;
241
241
242
242
Ok ( promise)
@@ -247,23 +247,25 @@ impl AsyncClient {
247
247
let ( promise_tx, promise) = PromiseTx :: new ( ) ;
248
248
let unsubscribe = Unsubscribe :: new ( topic. into ( ) ) ;
249
249
self . request_tx
250
- . try_send ( ( unsubscribe. into ( ) , Some ( promise_tx) ) ) ?;
250
+ . try_send ( Pending :: new ( unsubscribe. into ( ) , promise_tx) ) ?;
251
251
252
252
Ok ( promise)
253
253
}
254
254
255
255
/// Sends a MQTT disconnect to the `EventLoop`
256
256
pub async fn disconnect ( & self ) -> Result < ( ) , ClientError > {
257
257
let request = Request :: Disconnect ( Disconnect ) ;
258
- self . request_tx . send_async ( ( request, None ) ) . await ?;
258
+ self . request_tx
259
+ . send_async ( Pending :: no_promises ( request) )
260
+ . await ?;
259
261
260
262
Ok ( ( ) )
261
263
}
262
264
263
265
/// Attempts to send a MQTT disconnect to the `EventLoop`
264
266
pub fn try_disconnect ( & self ) -> Result < ( ) , ClientError > {
265
267
let request = Request :: Disconnect ( Disconnect ) ;
266
- self . request_tx . try_send ( ( request, None ) ) ?;
268
+ self . request_tx . try_send ( Pending :: no_promises ( request) ) ?;
267
269
268
270
Ok ( ( ) )
269
271
}
@@ -313,7 +315,7 @@ impl Client {
313
315
///
314
316
/// This is mostly useful for creating a test instance where you can
315
317
/// listen on the corresponding receiver.
316
- pub fn from_sender ( request_tx : Sender < ( Request , Option < PromiseTx > ) > ) -> Client {
318
+ pub fn from_sender ( request_tx : Sender < Pending < Request > > ) -> Client {
317
319
Client {
318
320
client : AsyncClient :: from_senders ( request_tx) ,
319
321
}
@@ -335,11 +337,13 @@ impl Client {
335
337
let topic = topic. into ( ) ;
336
338
let mut publish = Publish :: new ( & topic, qos, payload) ;
337
339
publish. retain = retain;
338
- let publish = Request :: Publish ( publish) ;
340
+ let request = Request :: Publish ( publish) ;
339
341
if !valid_topic ( & topic) {
340
- return Err ( ClientError :: Request ( publish ) ) ;
342
+ return Err ( ClientError :: Request ( request ) ) ;
341
343
}
342
- self . client . request_tx . send ( ( publish, Some ( promise_tx) ) ) ?;
344
+ self . client
345
+ . request_tx
346
+ . send ( Pending :: new ( request, promise_tx) ) ?;
343
347
344
348
Ok ( promise)
345
349
}
@@ -363,7 +367,7 @@ impl Client {
363
367
let ack = get_ack_req ( publish) ;
364
368
365
369
if let Some ( ack) = ack {
366
- self . client . request_tx . send ( ( ack, None ) ) ?;
370
+ self . client . request_tx . send ( Pending :: no_promises ( ack) ) ?;
367
371
}
368
372
369
373
Ok ( ( ) )
@@ -387,7 +391,7 @@ impl Client {
387
391
}
388
392
self . client
389
393
. request_tx
390
- . send ( ( subscribe. into ( ) , Some ( promise_tx) ) ) ?;
394
+ . send ( Pending :: new ( subscribe. into ( ) , promise_tx) ) ?;
391
395
392
396
Ok ( promise)
393
397
}
@@ -413,7 +417,7 @@ impl Client {
413
417
}
414
418
self . client
415
419
. request_tx
416
- . send ( ( subscribe. into ( ) , Some ( promise_tx) ) ) ?;
420
+ . send ( Pending :: new ( subscribe. into ( ) , promise_tx) ) ?;
417
421
418
422
Ok ( promise)
419
423
}
@@ -429,8 +433,9 @@ impl Client {
429
433
pub fn unsubscribe < S : Into < String > > ( & self , topic : S ) -> Result < AckPromise , ClientError > {
430
434
let ( promise_tx, promise) = PromiseTx :: new ( ) ;
431
435
let unsubscribe = Unsubscribe :: new ( topic. into ( ) ) ;
432
- let request = Request :: Unsubscribe ( unsubscribe) ;
433
- self . client . request_tx . send ( ( request, Some ( promise_tx) ) ) ?;
436
+ self . client
437
+ . request_tx
438
+ . send ( Pending :: new ( unsubscribe. into ( ) , promise_tx) ) ?;
434
439
435
440
Ok ( promise)
436
441
}
@@ -444,7 +449,9 @@ impl Client {
444
449
pub fn disconnect ( & self ) -> Result < AckPromise , ClientError > {
445
450
let ( promise_tx, promise) = PromiseTx :: new ( ) ;
446
451
let request = Request :: Disconnect ( Disconnect ) ;
447
- self . client . request_tx . send ( ( request, Some ( promise_tx) ) ) ?;
452
+ self . client
453
+ . request_tx
454
+ . send ( Pending :: new ( request, promise_tx) ) ?;
448
455
449
456
Ok ( promise)
450
457
}
0 commit comments