diff options
Diffstat (limited to 'src/net.rs')
| -rw-r--r-- | src/net.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/net.rs b/src/net.rs new file mode 100644 index 0000000000..0906426f7b --- /dev/null +++ b/src/net.rs @@ -0,0 +1,40 @@ +/// Representation of a TCP or UDP endpoint. The host is represented as a String since it can be +/// both a hostname/domain as well as an IP. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct RemoteAddr { + address: String, + port: u16, +} + +impl RemoteAddr { + /// Constructs a new `RemoteAddr` from the given address and port. + pub fn new(address: &str, port: u16) -> Self { + RemoteAddr { + address: address.to_owned(), + port: port, + } + } + + /// Returns the address associated with this `RemoteAddr`. + pub fn address(&self) -> &str { + &self.address + } + + /// Returns the port associated with this `RemoteAddr`. + pub fn port(&self) -> u16 { + self.port + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn remote_addr_new_and_getters() { + let remote_addr = RemoteAddr::new("a_domain", 543); + assert_eq!("a_domain", remote_addr.address()); + assert_eq!(543, remote_addr.port()); + } +} |
