Skip to content

Commit dc6b9a2

Browse files
committed
session_builder: known_nodes accept IntoIterator
There is no reason why not accept arbitrary impl IntoIterator in SessionBuilder::[known_nodes(), known_nodes_addr()]. It will also save an allocation in bindings. Note that the changes in usage (removing references from arrays) are for clippy only ("the borrowed expression implements the requested traits"). The new API is backwards-compatible. To assert that, a compile-time check was added. It verifies that any value satisfing the old constraints satisfies the new constraints as well.
1 parent 8a521b9 commit dc6b9a2

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

scylla/src/transport/session.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl SessionConfig {
305305
/// let mut config = SessionConfig::new();
306306
/// config.add_known_nodes(&["127.0.0.1:9042", "db1.example.com"]);
307307
/// ```
308-
pub fn add_known_nodes(&mut self, hostnames: &[impl AsRef<str>]) {
308+
pub fn add_known_nodes(&mut self, hostnames: impl IntoIterator<Item = impl AsRef<str>>) {
309309
for hostname in hostnames {
310310
self.add_known_node(hostname);
311311
}
@@ -322,9 +322,12 @@ impl SessionConfig {
322322
/// let mut config = SessionConfig::new();
323323
/// config.add_known_nodes_addr(&[addr1, addr2]);
324324
/// ```
325-
pub fn add_known_nodes_addr(&mut self, node_addrs: &[SocketAddr]) {
325+
pub fn add_known_nodes_addr(
326+
&mut self,
327+
node_addrs: impl IntoIterator<Item = impl Borrow<SocketAddr>>,
328+
) {
326329
for address in node_addrs {
327-
self.add_known_node_addr(*address);
330+
self.add_known_node_addr(*address.borrow());
328331
}
329332
}
330333
}

scylla/src/transport/session_builder.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::ExecutionProfile;
1212

1313
use crate::transport::connection_pool::PoolSize;
1414
use crate::transport::host_filter::HostFilter;
15+
use std::borrow::Borrow;
1516
use std::marker::PhantomData;
1617
use std::net::SocketAddr;
1718
#[cfg(feature = "cloud")]
@@ -126,13 +127,13 @@ impl SessionBuilder {
126127
/// # use scylla::{Session, SessionBuilder};
127128
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
128129
/// let session: Session = SessionBuilder::new()
129-
/// .known_nodes(&["127.0.0.1:9042", "db1.example.com"])
130+
/// .known_nodes(["127.0.0.1:9042", "db1.example.com"])
130131
/// .build()
131132
/// .await?;
132133
/// # Ok(())
133134
/// # }
134135
/// ```
135-
pub fn known_nodes(mut self, hostnames: &[impl AsRef<str>]) -> Self {
136+
pub fn known_nodes(mut self, hostnames: impl IntoIterator<Item = impl AsRef<str>>) -> Self {
136137
self.config.add_known_nodes(hostnames);
137138
self
138139
}
@@ -147,13 +148,16 @@ impl SessionBuilder {
147148
/// let addr2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(172, 17, 0, 4)), 9042);
148149
///
149150
/// let session: Session = SessionBuilder::new()
150-
/// .known_nodes_addr(&[addr1, addr2])
151+
/// .known_nodes_addr([addr1, addr2])
151152
/// .build()
152153
/// .await?;
153154
/// # Ok(())
154155
/// # }
155156
/// ```
156-
pub fn known_nodes_addr(mut self, node_addrs: &[SocketAddr]) -> Self {
157+
pub fn known_nodes_addr(
158+
mut self,
159+
node_addrs: impl IntoIterator<Item = impl Borrow<SocketAddr>>,
160+
) -> Self {
157161
self.config.add_known_nodes_addr(node_addrs);
158162
self
159163
}
@@ -788,7 +792,7 @@ mod tests {
788792
fn add_known_nodes() {
789793
let mut builder = SessionBuilder::new();
790794

791-
builder = builder.known_nodes(&["test_hostname1", "test_hostname2"]);
795+
builder = builder.known_nodes(["test_hostname1", "test_hostname2"]);
792796

793797
assert_eq!(
794798
builder.config.known_nodes,
@@ -807,7 +811,7 @@ mod tests {
807811
let addr1 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(172, 17, 0, 3)), 1357);
808812
let addr2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(172, 17, 0, 4)), 9090);
809813

810-
builder = builder.known_nodes_addr(&[addr1, addr2]);
814+
builder = builder.known_nodes_addr([addr1, addr2]);
811815

812816
assert_eq!(
813817
builder.config.known_nodes,
@@ -955,8 +959,8 @@ mod tests {
955959

956960
builder = builder.known_node("hostname_test");
957961
builder = builder.known_node_addr(addr);
958-
builder = builder.known_nodes(&["hostname_test1", "hostname_test2"]);
959-
builder = builder.known_nodes_addr(&[addr1, addr2]);
962+
builder = builder.known_nodes(["hostname_test1", "hostname_test2"]);
963+
builder = builder.known_nodes_addr([addr1, addr2]);
960964
builder = builder.compression(Some(Compression::Snappy));
961965
builder = builder.tcp_nodelay(true);
962966
builder = builder.use_keyspace("ks_name", true);
@@ -982,4 +986,18 @@ mod tests {
982986
assert!(builder.config.keyspace_case_sensitive);
983987
assert!(!builder.config.fetch_schema_metadata);
984988
}
989+
990+
// This is to assert that #705 does not break the API (i.e. it merely extends it).
991+
fn _check_known_nodes_compatibility(
992+
hostnames: &[impl AsRef<str>],
993+
host_addresses: &[SocketAddr],
994+
) {
995+
let mut sb: SessionBuilder = SessionBuilder::new();
996+
sb = sb.known_nodes(hostnames);
997+
sb = sb.known_nodes_addr(host_addresses);
998+
999+
let mut config = sb.config;
1000+
config.add_known_nodes(hostnames);
1001+
config.add_known_nodes_addr(host_addresses);
1002+
}
9851003
}

0 commit comments

Comments
 (0)