Skip to content

Commit 1a913a1

Browse files
authored
Merge pull request #705 from wprzytula/known_nodes_more_generic
session_builder: known_nodes accept IntoIterator
2 parents da0aed6 + dc6b9a2 commit 1a913a1

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
@@ -306,7 +306,7 @@ impl SessionConfig {
306306
/// let mut config = SessionConfig::new();
307307
/// config.add_known_nodes(&["127.0.0.1:9042", "db1.example.com"]);
308308
/// ```
309-
pub fn add_known_nodes(&mut self, hostnames: &[impl AsRef<str>]) {
309+
pub fn add_known_nodes(&mut self, hostnames: impl IntoIterator<Item = impl AsRef<str>>) {
310310
for hostname in hostnames {
311311
self.add_known_node(hostname);
312312
}
@@ -323,9 +323,12 @@ impl SessionConfig {
323323
/// let mut config = SessionConfig::new();
324324
/// config.add_known_nodes_addr(&[addr1, addr2]);
325325
/// ```
326-
pub fn add_known_nodes_addr(&mut self, node_addrs: &[SocketAddr]) {
326+
pub fn add_known_nodes_addr(
327+
&mut self,
328+
node_addrs: impl IntoIterator<Item = impl Borrow<SocketAddr>>,
329+
) {
327330
for address in node_addrs {
328-
self.add_known_node_addr(*address);
331+
self.add_known_node_addr(*address.borrow());
329332
}
330333
}
331334
}

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)