Skip to main content

SocketAddr

Struct SocketAddr 

Source
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

Source

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);
Source

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());
Source

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);
Source

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);
Source

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);
Source

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);
Source

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());
Source

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());
Source

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());
Source

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.
Source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

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 more
Source§

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 more
Source§

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 more
Source§

impl Clone for SocketAddr

Source§

fn clone(&self) -> SocketAddr

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SocketAddr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SocketAddr

Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for SocketAddr

Source§

type Err = SocketAddrError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for SocketAddr

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for SocketAddr

Source§

fn eq(&self, other: &SocketAddr) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for SocketAddr

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<SocketAddr> for SocketAddr

Available on crate feature std only.
Source§

type Error = StdConversionError

The type returned in the event of a conversion error.
Source§

fn try_from(addr: SocketAddr) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<SocketAddr> for SocketAddr

Available on crate feature std only.
Source§

type Error = StdConversionError

The type returned in the event of a conversion error.
Source§

fn try_from(addr: SocketAddr) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for SocketAddr

Source§

impl StructuralPartialEq for SocketAddr

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,