Trait SocketAddrArg

Source
pub unsafe trait SocketAddrArg {
    // Required method
    unsafe fn with_sockaddr<R>(
        &self,
        f: impl FnOnce(*const SocketAddrOpaque, SocketAddrLen) -> R,
    ) -> R;

    // Provided methods
    fn as_any(&self) -> SocketAddrAny { ... }
    unsafe fn write_sockaddr(
        &self,
        storage: *mut SocketAddrStorage,
    ) -> SocketAddrLen { ... }
}
Expand description

A trait abstracting over the types that can be passed as a sockaddr.

§Safety

Implementers of this trait must ensure that with_sockaddr calls f with a pointer that is readable for the passed length, and points to data that is a valid socket address for the system calls that accept sockaddr as a const pointer.

Required Methods§

Source

unsafe fn with_sockaddr<R>( &self, f: impl FnOnce(*const SocketAddrOpaque, SocketAddrLen) -> R, ) -> R

Call a closure with the pointer and length to the corresponding C type.

The memory pointed to by the pointer of size length is guaranteed to be valid only for the duration of the call.

The API uses a closure so that:

  • The libc types are not exposed in the rustix API.
  • Types like SocketAddrUnix that contain their corresponding C type can pass it directly without a copy.
  • Other socket types can construct their C-compatible struct on the stack and call the closure with a pointer to it.
§Safety

For f to use its pointer argument, it’ll contain an unsafe block. The caller of with_sockaddr here is responsible for ensuring that the safety condition for that unsafe block is satisfied by the guarantee that with_sockaddr here provides.

Provided Methods§

Source

fn as_any(&self) -> SocketAddrAny

Convert to SocketAddrAny.

Source

unsafe fn write_sockaddr( &self, storage: *mut SocketAddrStorage, ) -> SocketAddrLen

Encode an address into a SocketAddrStorage.

Returns the number of bytes that were written.

For a safe interface to this functionality, use as_any.

§Safety

storage must be valid to write up to size_of<SocketAddrStorage>() bytes to.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§