Struct MaybeConnectedSocket

Source
pub struct MaybeConnectedSocket<T> { /* private fields */ }
Expand description

A cheap wrapper around a datagram socket which describes if it is connected to an explicit peer.

This struct essentially forwards its underlying socket’s send_to() method to send() if the socket is explicitly connected to a peer. This is helpful for preventing issues on platforms that do not support send_to on already-connected sockets.

§Warning

A socket’s “connectedness” is determined once, when it is created. If the socket is created as connected, then later disconnected from its peer, its send_to() call will fail.

For example, MacOS errors if send_to is used on a socket that’s already connected. Only send can be used. By using MaybeConnectedSocket, you can use the same send and send_to APIs in both client- and server-side code. Clients, usually with connected sockets, will then forward send_to to send, whereas servers, usually with unconnected sockets, will use send_to.

Implementations§

Source§

impl<T: DatagramSocketSend> MaybeConnectedSocket<T>

Source

pub fn new(inner: T) -> Self

Source

pub fn inner(&self) -> &T

Provides access to the wrapped socket, allowing the user to override send_to() behavior if required.

Source

pub fn into_inner(self) -> T

Consumes self, returning the wrapped socket.

Trait Implementations§

Source§

impl<T: Clone> Clone for MaybeConnectedSocket<T>

Source§

fn clone(&self) -> MaybeConnectedSocket<T>

Returns a copy 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<T: DatagramSocketSend> DatagramSocketSend for MaybeConnectedSocket<T>

Source§

fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>

Attempts to send data on the socket to the remote address to which it was previously connected. Read more
Source§

fn poll_send_to( &self, cx: &mut Context<'_>, buf: &[u8], addr: SocketAddr, ) -> Poll<Result<usize>>

Attempts to send data on the socket to a given address. Read more
Source§

fn poll_send_many( &self, cx: &mut Context<'_>, bufs: &[ReadBuf<'_>], ) -> Poll<Result<usize>>

Attempts to send multiple packets of data on the socket to the remote address to which it was previously connected. Read more
Source§

fn as_udp_socket(&self) -> Option<&UdpSocket>

If the underlying socket is a UdpSocket, return the reference to it.
Source§

fn peer_addr(&self) -> Option<SocketAddr>

Returns the socket address of the remote peer this socket was connected to.

Auto Trait Implementations§

§

impl<T> Freeze for MaybeConnectedSocket<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for MaybeConnectedSocket<T>
where T: RefUnwindSafe,

§

impl<T> Send for MaybeConnectedSocket<T>
where T: Send,

§

impl<T> Sync for MaybeConnectedSocket<T>
where T: Sync,

§

impl<T> Unpin for MaybeConnectedSocket<T>
where T: Unpin,

§

impl<T> UnwindSafe for MaybeConnectedSocket<T>
where T: UnwindSafe,

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> DatagramSocketSendExt for T

Source§

fn send(&self, buf: &[u8]) -> impl Future<Output = Result<usize>>

Sends data on the socket to the remote address that the socket is connected to.
Source§

fn send_to( &self, buf: &[u8], addr: SocketAddr, ) -> impl Future<Output = Result<usize>>

Sends data on the socket to the given address. On success, returns the number of bytes written.
Source§

fn send_many(&self, bufs: &[ReadBuf<'_>]) -> impl Future<Output = Result<usize>>

Sends multiple data packets on the socket to the to the remote address that the socket is connected to. On success, returns the number of packets sent.
Source§

fn try_send(&self, buf: &[u8]) -> Result<usize>

Source§

fn try_send_many(&self, bufs: &[ReadBuf<'_>]) -> Result<usize>

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.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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, 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.