aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-protocol/src/lib.rs')
-rw-r--r--[-rwxr-xr-x]azalea-protocol/src/lib.rs17
1 files changed, 14 insertions, 3 deletions
diff --git a/azalea-protocol/src/lib.rs b/azalea-protocol/src/lib.rs
index 0fae75b1..052e740f 100755..100644
--- a/azalea-protocol/src/lib.rs
+++ b/azalea-protocol/src/lib.rs
@@ -13,7 +13,7 @@
#![feature(error_generic_member_access)]
#![feature(provide_any)]
-use std::str::FromStr;
+use std::{net::SocketAddr, str::FromStr};
#[cfg(feature = "connecting")]
pub mod connect;
@@ -35,13 +35,12 @@ pub mod write;
/// assert_eq!(addr.host, "localhost");
/// assert_eq!(addr.port, 25565);
/// ```
-#[derive(Debug)]
+#[derive(Debug, Clone)]
pub struct ServerAddress {
pub host: String,
pub port: u16,
}
-// impl try_from for ServerAddress
impl<'a> TryFrom<&'a str> for ServerAddress {
type Error = String;
@@ -59,6 +58,18 @@ impl<'a> TryFrom<&'a str> for ServerAddress {
}
}
+impl From<SocketAddr> for ServerAddress {
+ /// Convert an existing SocketAddr into a ServerAddress. This just converts
+ /// the ip to a string and passes along the port. The resolver will realize
+ /// it's already an IP address and not do any DNS requests.
+ fn from(addr: SocketAddr) -> Self {
+ ServerAddress {
+ host: addr.ip().to_string(),
+ port: addr.port(),
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use std::io::Cursor;