Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ToServerAddrs impl for array/vector of strings #1231

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions async-nats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,24 @@ impl ToServerAddrs for String {
}
}

impl<T: AsRef<str>> ToServerAddrs for [T] {
type Iter = std::vec::IntoIter<ServerAddr>;
fn to_server_addrs(&self) -> io::Result<Self::Iter> {
self.iter()
.map(AsRef::as_ref)
.map(str::parse)
.collect::<io::Result<_>>()
.map(Vec::into_iter)
}
}

impl<T: AsRef<str>> ToServerAddrs for Vec<T> {
type Iter = std::vec::IntoIter<ServerAddr>;
fn to_server_addrs(&self) -> io::Result<Self::Iter> {
self.as_slice().to_server_addrs()
}
}

impl<'a> ToServerAddrs for &'a [ServerAddr] {
type Iter = iter::Cloned<slice::Iter<'a, ServerAddr>>;

Expand Down Expand Up @@ -1554,4 +1572,40 @@ mod tests {
let address = ServerAddr::from_str("nats://example.com").unwrap();
assert_eq!(address.host(), "example.com")
}

#[test]
fn to_server_addrs_vec_str() {
let vec = vec!["nats://127.0.0.1", "nats://[::]"];
let mut addrs_iter = vec.to_server_addrs().unwrap();
assert_eq!(addrs_iter.next().unwrap().host(), "127.0.0.1");
assert_eq!(addrs_iter.next().unwrap().host(), "::");
assert_eq!(addrs_iter.next(), None);
}

#[test]
fn to_server_addrs_arr_str() {
let arr = ["nats://127.0.0.1", "nats://[::]"];
let mut addrs_iter = arr.to_server_addrs().unwrap();
assert_eq!(addrs_iter.next().unwrap().host(), "127.0.0.1");
assert_eq!(addrs_iter.next().unwrap().host(), "::");
assert_eq!(addrs_iter.next(), None);
}

#[test]
fn to_server_addrs_vec_string() {
let vec = vec!["nats://127.0.0.1".to_string(), "nats://[::]".to_string()];
let mut addrs_iter = vec.to_server_addrs().unwrap();
assert_eq!(addrs_iter.next().unwrap().host(), "127.0.0.1");
assert_eq!(addrs_iter.next().unwrap().host(), "::");
assert_eq!(addrs_iter.next(), None);
}

#[test]
fn to_server_addrs_arr_string() {
let arr = ["nats://127.0.0.1".to_string(), "nats://[::]".to_string()];
let mut addrs_iter = arr.to_server_addrs().unwrap();
assert_eq!(addrs_iter.next().unwrap().host(), "127.0.0.1");
assert_eq!(addrs_iter.next().unwrap().host(), "::");
assert_eq!(addrs_iter.next(), None);
}
}
Loading