# socket2::SockAddr - Rust
API documentation for the Rust `SockAddr` struct in crate `socket2`.
Keywords: rust, rustlang, rust-lang, SockAddr

# Struct socket2::SockAddr [−] [src]

    pub struct SockAddr { /* fields omitted */ }

The address of a socket.

`SockAddr`s may be constructed directly to and from the standard library `SocketAddr`, `SocketAddrV4`, and `SocketAddrV6` types.

## Implementations

### `impl SockAddr` [src]

#### `pub unsafe fn init<F, T>(init: F) -> Result<(T, SockAddr)> where
F: FnOnce(*mut sockaddr_storage, *mut socklen_t) -> Result<T>,` [src]

Initialise a `SockAddr` by calling the function `init`.

The type of the address storage and length passed to the function `init` is OS/architecture specific.

# Safety

Caller must initialise the provided address storage and set the length properly. The address is zeroed before `init` is called and is thus valid to dereference and read from. The length initialised to the maximum length of the storage.

# Examples

    use std::io;
    use std::os::unix::io::AsRawFd;

    use socket2::{SockAddr, Socket, Domain, Type};

    let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;

    // Initialise a `SocketAddr` byte calling `getsockname(2)`.
    let (_, address) = unsafe {
        SockAddr::init(|addr_storage, len| {
            // The `getsockname(2)` system call will intiliase `storage` for
            // us, setting `len` to the correct length.
            if libc::getsockname(socket.as_raw_fd(), addr_storage.cast(), len) == -1 {
                Err(io::Error::last_os_error())
            } else {
                Ok(())
            }
        })
    }?;

#### `pub const fn family(&self) -> sa_family_t` [src]

Returns this address's family.

#### `pub const fn len(&self) -> socklen_t` [src]

Returns the size of this address in bytes.

#### `pub const fn as_ptr(&self) -> *const sockaddr` [src]

Returns a raw pointer to the address.

#### `pub fn as_socket(&self) -> Option<SocketAddr>` [src]

Returns this address as a `SocketAddr` if it is in the `AF_INET` (IPv4) or `AF_INET6` (IPv6) family, otherwise returns `None`.

#### `pub fn as_socket_ipv4(&self) -> Option<SocketAddrV4>` [src]

Returns this address as a `SocketAddrV4` if it is in the `AF_INET` family.

#### `pub fn as_socket_ipv6(&self) -> Option<SocketAddrV6>` [src]

Returns this address as a `SocketAddrV6` if it is in the `AF_INET6` family.

## Trait Implementations

### `impl Debug for SockAddr` [src]

#### `fn fmt(&self, fmt: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter. Read more

### `impl From<SocketAddr> for SockAddr` [src]

#### `fn from(addr: SocketAddr) -> SockAddr` [src]

Performs the conversion.

### `impl From<SocketAddrV4> for SockAddr` [src]

#### `fn from(addr: SocketAddrV4) -> SockAddr` [src]

Performs the conversion.

### `impl From<SocketAddrV6> for SockAddr` [src]

#### `fn from(addr: SocketAddrV6) -> SockAddr` [src]

Performs the conversion.

## Auto Trait Implementations

### `impl RefUnwindSafe for SockAddr` [src]

### `impl Send for SockAddr` [src]

### `impl Sync for SockAddr` [src]

### `impl Unpin for SockAddr` [src]

### `impl UnwindSafe for SockAddr` [src]

## Blanket Implementations

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `pub fn type_id(&self) -> TypeId` [src]

Gets the `TypeId` of `self`. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow(&self) -> &T` [src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow_mut(&mut self) -> &mut T` [src]

Mutably borrows from an owned value. Read more

### `impl<T> From<T> for T` [src]

#### `pub fn from(t: T) -> T` [src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `pub fn into(self) -> U` [src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>` [src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

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

The type returned in the event of a conversion error.

#### `pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>` [src]

Performs the conversion.