@@ -26,6 +26,7 @@ use anyhow::bail;
26
26
use anyhow:: format_err;
27
27
use anyhow:: Context ;
28
28
use anyhow:: Error ;
29
+ use vec1:: Vec1 ;
29
30
30
31
// Used in docs
31
32
#[ cfg( test) ]
@@ -114,35 +115,32 @@ impl From<SqlConnectionsWithSchema> for SqlConnections {
114
115
#[ derive( Clone ) ]
115
116
pub struct SqlShardedConnections {
116
117
/// Write connections to the master for each shard
117
- pub write_connections : Vec < Connection > ,
118
+ pub write_connections : Vec1 < Connection > ,
118
119
/// Read connections for each shard
119
- pub read_connections : Vec < Connection > ,
120
+ pub read_connections : Vec1 < Connection > ,
120
121
/// Read master connections for each shard
121
- pub read_master_connections : Vec < Connection > ,
122
+ pub read_master_connections : Vec1 < Connection > ,
122
123
}
123
124
124
- impl SqlShardedConnections {
125
- /// Check if the struct is empty.
126
- pub fn is_empty ( & self ) -> bool {
127
- self . write_connections . is_empty ( )
128
- }
129
- }
130
-
131
- impl From < Vec < SqlConnections > > for SqlShardedConnections {
132
- fn from ( shard_connections : Vec < SqlConnections > ) -> Self {
133
- let mut write_connections = Vec :: with_capacity ( shard_connections. len ( ) ) ;
134
- let mut read_connections = Vec :: with_capacity ( shard_connections. len ( ) ) ;
135
- let mut read_master_connections = Vec :: with_capacity ( shard_connections. len ( ) ) ;
136
- for connections in shard_connections. into_iter ( ) {
125
+ impl From < Vec1 < SqlConnections > > for SqlShardedConnections {
126
+ fn from ( shard_connections : Vec1 < SqlConnections > ) -> Self {
127
+ let ( head, last) = shard_connections. split_off_last ( ) ;
128
+ let mut write_connections = Vec :: with_capacity ( head. len ( ) ) ;
129
+ let mut read_connections = Vec :: with_capacity ( head. len ( ) ) ;
130
+ let mut read_master_connections = Vec :: with_capacity ( head. len ( ) ) ;
131
+ for connections in head {
137
132
write_connections. push ( connections. write_connection ) ;
138
133
read_connections. push ( connections. read_connection ) ;
139
134
read_master_connections. push ( connections. read_master_connection ) ;
140
135
}
141
136
142
137
Self {
143
- read_connections,
144
- read_master_connections,
145
- write_connections,
138
+ read_connections : Vec1 :: from_vec_push ( read_connections, last. read_connection ) ,
139
+ read_master_connections : Vec1 :: from_vec_push (
140
+ read_master_connections,
141
+ last. read_master_connection ,
142
+ ) ,
143
+ write_connections : Vec1 :: from_vec_push ( write_connections, last. write_connection ) ,
146
144
}
147
145
}
148
146
}
0 commit comments