@@ -37,6 +37,41 @@ impl From<TrySendError<Request>> for ClientError {
3737 }
3838}
3939
40+ pub struct AsyncClientBuilder {
41+ options : MqttOptions ,
42+ capacity : Option < usize > ,
43+ }
44+
45+ impl AsyncClientBuilder {
46+ pub fn new ( options : MqttOptions ) -> Self {
47+ Self {
48+ options,
49+ capacity : None ,
50+ }
51+ }
52+
53+ pub fn capacity ( mut self , cap : usize ) -> Self {
54+ self . capacity = Some ( cap) ;
55+ self
56+ }
57+
58+ pub fn build_async ( self ) -> ( AsyncClient , EventLoop ) {
59+ let eventloop = EventLoop :: new ( self . options , self . capacity ) ;
60+ let request_tx = eventloop. requests_tx . clone ( ) ;
61+ ( AsyncClient { request_tx } , eventloop)
62+ }
63+
64+ pub fn build ( self ) -> ( Client , Connection ) {
65+ let ( client, eventloop) = self . build_async ( ) ;
66+ let client = Client { client } ;
67+ let runtime = runtime:: Builder :: new_current_thread ( )
68+ . enable_all ( )
69+ . build ( )
70+ . unwrap ( ) ;
71+ ( client, Connection :: new ( eventloop, runtime) )
72+ }
73+ }
74+
4075/// An asynchronous client, communicates with MQTT `EventLoop`.
4176///
4277/// This is cloneable and can be used to asynchronously [`publish`](`AsyncClient::publish`),
@@ -52,14 +87,17 @@ pub struct AsyncClient {
5287impl AsyncClient {
5388 /// Create a new `AsyncClient`.
5489 ///
55- /// `cap` specifies the capacity of the bounded async channel.
90+ /// # Deprecation
91+ /// Use [`AsyncClientBuilder`] instead for bounded/unbounded control.
92+ /// `Client::builder(options).capacity(cap).build_async()` for bounded,
93+ /// `Client::builder(options).build_async()` for unbounded.
94+ #[ deprecated( since = "0.26.0" , note = "Use AsyncClientBuilder instead" ) ]
5695 pub fn new ( options : MqttOptions , cap : usize ) -> ( AsyncClient , EventLoop ) {
57- let eventloop = EventLoop :: new ( options, cap) ;
58- let request_tx = eventloop. requests_tx . clone ( ) ;
59-
60- let client = AsyncClient { request_tx } ;
96+ AsyncClientBuilder :: new ( options) . capacity ( cap) . build_async ( )
97+ }
6198
62- ( client, eventloop)
99+ pub fn builder ( options : MqttOptions ) -> AsyncClientBuilder {
100+ AsyncClientBuilder :: new ( options)
63101 }
64102
65103 /// Create a new `AsyncClient` from a channel `Sender`.
@@ -469,20 +507,17 @@ pub struct Client {
469507}
470508
471509impl Client {
472- /// Create a new `Client`
510+ /// Create a new `Client`.
473511 ///
474- /// `cap` specifies the capacity of the bounded async channel.
512+ /// # Deprecation
513+ /// Use [`AsyncClientBuilder`] instead.
514+ #[ deprecated( since = "0.26.0" , note = "Use AsyncClientBuilder instead" ) ]
475515 pub fn new ( options : MqttOptions , cap : usize ) -> ( Client , Connection ) {
476- let ( client, eventloop) = AsyncClient :: new ( options, cap) ;
477- let client = Client { client } ;
478-
479- let runtime = runtime:: Builder :: new_current_thread ( )
480- . enable_all ( )
481- . build ( )
482- . unwrap ( ) ;
516+ AsyncClientBuilder :: new ( options) . capacity ( cap) . build ( )
517+ }
483518
484- let connection = Connection :: new ( eventloop , runtime ) ;
485- ( client , connection )
519+ pub fn builder ( options : MqttOptions ) -> AsyncClientBuilder {
520+ AsyncClientBuilder :: new ( options )
486521 }
487522
488523 /// Create a new `Client` from a channel `Sender`.
@@ -882,7 +917,7 @@ mod test {
882917 . set_keep_alive ( Duration :: from_secs ( 5 ) )
883918 . set_last_will ( will) ;
884919
885- let ( _, mut connection) = Client :: new ( mqttoptions, 10 ) ;
920+ let ( _, mut connection) = Client :: builder ( mqttoptions) . capacity ( 10 ) . build ( ) ;
886921 let _ = connection. iter ( ) ;
887922 let _ = connection. iter ( ) ;
888923 }
0 commit comments