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 Vec to ToServerAddrs impl and improve docs examples #802

Merged
merged 3 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .config/nats.dic
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ cloneable
PurgeRequest
StatsResponse
SemVer
ServerAddr
Vec
60 changes: 60 additions & 0 deletions async-nats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,8 @@ impl fmt::Display for Event {
/// To have customized NATS connection, check [ConnectOptions].
///
/// # Examples
///
/// ## Single URL
/// ```
/// # #[tokio::main]
/// # async fn main() -> Result<(), async_nats::Error> {
Expand All @@ -768,6 +770,56 @@ impl fmt::Display for Event {
/// # Ok(())
/// # }
/// ```
///
/// ## Connect with [Vec] of [ServerAddr].
/// ```no_run
///#[tokio::main]
///# async fn main() -> Result<(), async_nats::Error> {
///use async_nats::ServerAddr;
///let client = async_nats::connect(vec![
/// "demo.nats.io".parse::<ServerAddr>()?,
/// "other.nats.io".parse::<ServerAddr>()?,
///])
///.await
///.unwrap();
///# Ok(())
///# }
/// ```
///
/// ## with [Vec], but parse URLs inside [crate::connect()]
/// ```no_run
///#[tokio::main]
///# async fn main() -> Result<(), async_nats::Error> {
///use async_nats::ServerAddr;
///let servers = vec!["demo.nats.io", "other.nats.io"];
///let client = async_nats::connect(
/// servers
/// .iter()
/// .map(|url| url.parse())
/// .collect::<Result<Vec<ServerAddr>, _>>()?
///)
///.await?;
///# Ok(())
///# }
///```
///
///
/// ## with slice.
/// ```no_run
///#[tokio::main]
///# async fn main() -> Result<(), async_nats::Error> {
///use async_nats::ServerAddr;
///let client = async_nats::connect(
/// [
/// "demo.nats.io".parse::<ServerAddr>()?,
/// "other.nats.io".parse::<ServerAddr>()?,
/// ]
/// .as_slice(),
///)
///.await?;
///# Ok(())
///# }
///
pub async fn connect<A: ToServerAddrs>(addrs: A) -> Result<Client, io::Error> {
connect_with_options(addrs, ConnectOptions::default()).await
}
Expand Down Expand Up @@ -1165,6 +1217,14 @@ impl<'a> ToServerAddrs for &'a [ServerAddr] {
}
}

impl ToServerAddrs for Vec<ServerAddr> {
type Iter = std::vec::IntoIter<ServerAddr>;

fn to_server_addrs(&self) -> io::Result<Self::Iter> {
Ok(self.clone().into_iter())
}
}

impl<T: ToServerAddrs + ?Sized> ToServerAddrs for &T {
type Iter = T::Iter;
fn to_server_addrs(&self) -> io::Result<Self::Iter> {
Expand Down
15 changes: 15 additions & 0 deletions async-nats/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ impl ConnectOptions {
/// # Ok(())
/// # }
/// ```
///
/// ## Pass multiple URLs.
/// ```no_run
///#[tokio::main]
///# async fn main() -> Result<(), async_nats::Error> {
///use async_nats::ServerAddr;
///let client = async_nats::connect(vec![
/// "demo.nats.io".parse::<ServerAddr>()?,
/// "other.nats.io".parse::<ServerAddr>()?,
///])
///.await
///.unwrap();
///# Ok(())
///# }
/// ```
pub async fn connect<A: ToServerAddrs>(self, addrs: A) -> io::Result<Client> {
crate::connect_with_options(addrs, self).await
}
Expand Down