pub struct SocketAddr { /* private fields */ }Available on crate feature
net only.Expand description
A socket address combining a host and a port.
This type provides a type-safe socket address that can represent:
- IP addresses with ports (e.g., “192.168.1.1:8080”)
- Domain names with ports (e.g., “example.com:443”)
- Hostnames with ports (e.g., “localhost:3000”)
§Invariants
- The host is always valid (IP address, domain name, or hostname)
- The port is always in the valid range (1-65535)
§Examples
use bare_types::net::{SocketAddr, Host, Port};
// Create from components
let host = Host::parse_str("192.168.1.1")?;
let port = Port::new(8080)?;
let addr = SocketAddr::new(host, port);
// Parse from string
let addr: SocketAddr = "example.com:443".parse()?;
// Access components
assert_eq!(addr.as_port().as_u16(), 443);Implementations§
Source§impl SocketAddr
impl SocketAddr
Sourcepub const fn new(host: Host, port: Port) -> Self
pub const fn new(host: Host, port: Port) -> Self
Creates a new socket address from a host and port.
§Examples
use bare_types::net::{SocketAddr, Host, Port};
let host = Host::parse_str("192.168.1.1")?;
let port = Port::new(8080)?;
let addr = SocketAddr::new(host, port);Sourcepub const fn as_host(&self) -> &Host
pub const fn as_host(&self) -> &Host
Returns a reference to the host part of this socket address.
§Examples
use bare_types::net::{SocketAddr, Host};
let addr: SocketAddr = "192.168.1.1:8080".parse()?;
assert!(addr.as_host().is_ipaddr());Sourcepub const fn as_port(&self) -> &Port
pub const fn as_port(&self) -> &Port
Returns a reference to the port part of this socket address.
§Examples
use bare_types::net::SocketAddr;
let addr: SocketAddr = "192.168.1.1:8080".parse()?;
assert_eq!(addr.as_port().as_u16(), 8080);Sourcepub fn set_host(&mut self, host: Host)
pub fn set_host(&mut self, host: Host)
Sets the host part of this socket address.
§Examples
use bare_types::net::{SocketAddr, Host};
let mut addr: SocketAddr = "192.168.1.1:8080".parse()?;
let new_host = Host::parse_str("10.0.0.1")?;
addr.set_host(new_host);Sourcepub const fn set_port(&mut self, port: Port)
pub const fn set_port(&mut self, port: Port)
Sets the port part of this socket address.
§Examples
use bare_types::net::{SocketAddr, Port};
let mut addr: SocketAddr = "192.168.1.1:8080".parse()?;
let new_port = Port::new(9090)?;
addr.set_port(new_port);Sourcepub fn into_parts(self) -> (Host, Port)
pub fn into_parts(self) -> (Host, Port)
Consumes this socket address and returns the host and port components as a tuple.
§Examples
use bare_types::net::{SocketAddr, Host, Port};
let host = Host::parse_str("192.168.1.1")?;
let port = Port::new(8080)?;
let addr = SocketAddr::new(host, port);
let (host, port) = addr.into_parts();
assert!(host.is_ipaddr());
assert_eq!(port.as_u16(), 8080);Sourcepub fn parse_str(s: &str) -> Result<Self, SocketAddrError>
pub fn parse_str(s: &str) -> Result<Self, SocketAddrError>
Parses a string into a socket address.
The string must be in the format <host>:<port>, where:
<host>is a valid IP address, domain name, or hostname<port>is a valid port number (1-65535)
§Errors
Returns SocketAddrError if:
- The input is empty
- The port separator ‘:’ is missing
- The host part is invalid
- The port part is invalid
§Examples
use bare_types::net::SocketAddr;
// Parse IP address with port
let addr = SocketAddr::parse_str("192.168.1.1:8080")?;
assert!(addr.as_host().is_ipaddr());
// Parse domain name with port
let addr = SocketAddr::parse_str("example.com:443")?;
assert!(addr.as_host().is_domainname());Sourcepub const fn is_ip(&self) -> bool
pub const fn is_ip(&self) -> bool
Returns true if the host is an IP address.
§Examples
use bare_types::net::SocketAddr;
let addr: SocketAddr = "192.168.1.1:8080".parse()?;
assert!(addr.is_ip());Sourcepub const fn is_domain_name(&self) -> bool
pub const fn is_domain_name(&self) -> bool
Returns true if the host is a domain name.
§Examples
use bare_types::net::SocketAddr;
let addr: SocketAddr = "example.com:443".parse()?;
assert!(addr.is_domain_name());Sourcepub const fn is_hostname(&self) -> bool
pub const fn is_hostname(&self) -> bool
Returns true if the host is a hostname.
§Examples
use bare_types::net::{SocketAddr, Host, Hostname, Port};
let hostname = Hostname::new("localhost")?;
let host = Host::from_hostname(hostname);
let port = Port::new(3000)?;
let addr = SocketAddr::new(host, port);
assert!(addr.is_hostname());Trait Implementations§
Source§impl<'a> Arbitrary<'a> for SocketAddr
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for SocketAddr
Available on crate feature
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Generate an arbitrary value of
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Generate an arbitrary value of
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§impl Clone for SocketAddr
impl Clone for SocketAddr
Source§fn clone(&self) -> SocketAddr
fn clone(&self) -> SocketAddr
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for SocketAddr
impl Debug for SocketAddr
Source§impl<'de> Deserialize<'de> for SocketAddr
impl<'de> Deserialize<'de> for SocketAddr
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Display for SocketAddr
impl Display for SocketAddr
Source§impl FromStr for SocketAddr
impl FromStr for SocketAddr
Source§impl Hash for SocketAddr
impl Hash for SocketAddr
Source§impl PartialEq for SocketAddr
impl PartialEq for SocketAddr
Source§impl Serialize for SocketAddr
impl Serialize for SocketAddr
Source§impl TryFrom<SocketAddr> for SocketAddr
Available on crate feature std only.
impl TryFrom<SocketAddr> for SocketAddr
Available on crate feature
std only.Source§type Error = StdConversionError
type Error = StdConversionError
The type returned in the event of a conversion error.
Source§impl TryFrom<SocketAddr> for SocketAddr
Available on crate feature std only.
impl TryFrom<SocketAddr> for SocketAddr
Available on crate feature
std only.Source§type Error = StdConversionError
type Error = StdConversionError
The type returned in the event of a conversion error.
impl Eq for SocketAddr
impl StructuralPartialEq for SocketAddr
Auto Trait Implementations§
impl Freeze for SocketAddr
impl RefUnwindSafe for SocketAddr
impl Send for SocketAddr
impl Sync for SocketAddr
impl Unpin for SocketAddr
impl UnwindSafe for SocketAddr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more