Skip to content

Commit

Permalink
sql: Use itertools to multiunzip instead of doing it manually
Browse files Browse the repository at this point in the history
Summary:
I always wanted this feature in Rust and just found out `itertools` has it :)

So I'm using it here, where we were doing this manually because vanilla Rust only knows how to unzip 2-tuples.

Doesn't change any behaviour, purely a refactor.

Reviewed By: RajivTS

Differential Revision: D40943893

fbshipit-source-id: bd4f63c89e07d69469152b8db78621f49219818f
  • Loading branch information
yancouto authored and facebook-github-bot committed Nov 9, 2022
1 parent 026a14a commit 22c5a6d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions shed/sql/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ anyhow = "1.0.65"
cloned = { version = "0.1.0", path = "../../cloned" }
futures = { version = "0.3.22", features = ["async-await", "compat"] }
futures_stats = { version = "0.1.0", path = "../../futures_stats" }
itertools = "0.10.3"
lazy_static = "1.4"
mysql_async = "0.27.1"
mysql_derive = { version = "0.1.0", path = "../derive" }
Expand Down
16 changes: 8 additions & 8 deletions shed/sql/common/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ pub struct SqlShardedConnections {
impl From<Vec1<SqlConnections>> for SqlShardedConnections {
fn from(shard_connections: Vec1<SqlConnections>) -> Self {
let (head, last) = shard_connections.split_off_last();
let mut write_connections = Vec::with_capacity(head.len());
let mut read_connections = Vec::with_capacity(head.len());
let mut read_master_connections = Vec::with_capacity(head.len());
for connections in head {
write_connections.push(connections.write_connection);
read_connections.push(connections.read_connection);
read_master_connections.push(connections.read_master_connection);
}
let (write_connections, read_connections, read_master_connections) =
itertools::multiunzip(head.into_iter().map(|conn| {
(
conn.write_connection,
conn.read_connection,
conn.read_master_connection,
)
}));

Self {
read_connections: Vec1::from_vec_push(read_connections, last.read_connection),
Expand Down

0 comments on commit 22c5a6d

Please sign in to comment.