@@ -3,6 +3,7 @@ use std::iter;
33#[ cfg( all( feature = "runtime" , unix) ) ]
44use std:: path:: { Path , PathBuf } ;
55use std:: str:: { self , FromStr } ;
6+ use std:: sync:: Arc ;
67#[ cfg( feature = "runtime" ) ]
78use std:: time:: Duration ;
89use tokio_io:: { AsyncRead , AsyncWrite } ;
@@ -23,7 +24,7 @@ pub(crate) enum Host {
2324}
2425
2526#[ derive( Debug , Clone , PartialEq ) ]
26- pub struct Builder {
27+ pub ( crate ) struct Inner {
2728 pub ( crate ) params : HashMap < String , String > ,
2829 pub ( crate ) password : Option < Vec < u8 > > ,
2930 #[ cfg( feature = "runtime" ) ]
@@ -34,6 +35,9 @@ pub struct Builder {
3435 pub ( crate ) connect_timeout : Option < Duration > ,
3536}
3637
38+ #[ derive( Debug , Clone , PartialEq ) ]
39+ pub struct Builder ( pub ( crate ) Arc < Inner > ) ;
40+
3741impl Default for Builder {
3842 fn default ( ) -> Builder {
3943 Builder :: new ( )
@@ -46,7 +50,7 @@ impl Builder {
4650 params. insert ( "client_encoding" . to_string ( ) , "UTF8" . to_string ( ) ) ;
4751 params. insert ( "timezone" . to_string ( ) , "GMT" . to_string ( ) ) ;
4852
49- Builder {
53+ Builder ( Arc :: new ( Inner {
5054 params,
5155 password : None ,
5256 #[ cfg( feature = "runtime" ) ]
@@ -55,20 +59,24 @@ impl Builder {
5559 port : vec ! [ ] ,
5660 #[ cfg( feature = "runtime" ) ]
5761 connect_timeout : None ,
58- }
62+ } ) )
5963 }
6064
6165 #[ cfg( feature = "runtime" ) ]
6266 pub fn host ( & mut self , host : & str ) -> & mut Builder {
6367 #[ cfg( unix) ]
6468 {
6569 if host. starts_with ( '/' ) {
66- self . host . push ( Host :: Unix ( PathBuf :: from ( host) ) ) ;
70+ Arc :: make_mut ( & mut self . 0 )
71+ . host
72+ . push ( Host :: Unix ( PathBuf :: from ( host) ) ) ;
6773 return self ;
6874 }
6975 }
7076
71- self . host . push ( Host :: Tcp ( host. to_string ( ) ) ) ;
77+ Arc :: make_mut ( & mut self . 0 )
78+ . host
79+ . push ( Host :: Tcp ( host. to_string ( ) ) ) ;
7280 self
7381 }
7482
@@ -77,32 +85,36 @@ impl Builder {
7785 where
7886 T : AsRef < Path > ,
7987 {
80- self . host . push ( Host :: Unix ( host. as_ref ( ) . to_path_buf ( ) ) ) ;
88+ Arc :: make_mut ( & mut self . 0 )
89+ . host
90+ . push ( Host :: Unix ( host. as_ref ( ) . to_path_buf ( ) ) ) ;
8191 self
8292 }
8393
8494 #[ cfg( feature = "runtime" ) ]
8595 pub fn port ( & mut self , port : u16 ) -> & mut Builder {
86- self . port . push ( port) ;
96+ Arc :: make_mut ( & mut self . 0 ) . port . push ( port) ;
8797 self
8898 }
8999
90100 #[ cfg( feature = "runtime" ) ]
91101 pub fn connect_timeout ( & mut self , connect_timeout : Duration ) -> & mut Builder {
92- self . connect_timeout = Some ( connect_timeout) ;
102+ Arc :: make_mut ( & mut self . 0 ) . connect_timeout = Some ( connect_timeout) ;
93103 self
94104 }
95105
96106 pub fn password < T > ( & mut self , password : T ) -> & mut Builder
97107 where
98108 T : AsRef < [ u8 ] > ,
99109 {
100- self . password = Some ( password. as_ref ( ) . to_vec ( ) ) ;
110+ Arc :: make_mut ( & mut self . 0 ) . password = Some ( password. as_ref ( ) . to_vec ( ) ) ;
101111 self
102112 }
103113
104114 pub fn param ( & mut self , key : & str , value : & str ) -> & mut Builder {
105- self . params . insert ( key. to_string ( ) , value. to_string ( ) ) ;
115+ Arc :: make_mut ( & mut self . 0 )
116+ . params
117+ . insert ( key. to_string ( ) , value. to_string ( ) ) ;
106118 self
107119 }
108120
0 commit comments