renet/src/socket/ref_.rs

82 lines
2.2 KiB
Rust
Raw Normal View History

use core::ops::{Deref, DerefMut};
#[cfg(feature = "proto-raw")]
use socket::RawSocket;
#[cfg(feature = "proto-udp")]
use socket::UdpSocket;
#[cfg(feature = "proto-tcp")]
use socket::TcpSocket;
/// A trait for tracking a socket usage session.
///
/// Allows implementation of custom drop logic that runs only if the socket was changed
/// in specific ways. For example, drop logic for UDP would check if the local endpoint
/// has changed, and if yes, notify the socket set.
#[doc(hidden)]
pub trait Session {
fn finish(&mut self) {}
}
#[cfg(feature = "proto-raw")]
impl<'a, 'b> Session for RawSocket<'a, 'b> {}
#[cfg(feature = "proto-udp")]
impl<'a, 'b> Session for UdpSocket<'a, 'b> {}
#[cfg(feature = "proto-tcp")]
impl<'a> Session for TcpSocket<'a> {}
/// A smart pointer to a socket.
///
/// Allows the network stack to efficiently determine if the socket state was changed in any way.
pub struct Ref<'a, T: Session + 'a> {
socket: &'a mut T,
consumed: bool,
}
impl<'a, T: Session + 'a> Ref<'a, T> {
/// Wrap a pointer to a socket to make a smart pointer.
///
/// Calling this function is only necessary if your code is using [unwrap].
///
/// [unwrap]: #method.unwrap
pub fn wrap(socket: &'a mut T) -> Self {
Ref { socket, consumed: false }
}
/// Unwrap a smart pointer to a socket.
///
/// The finalization code is not run. Prompt operation of the network stack depends
/// on wrapping the returned pointer back and dropping it.
///
/// Calling this function is only necessary to achieve composability if you *must*
/// map a `&mut SocketRef<'a, XSocket>` to a `&'a mut XSocket` (note the lifetimes);
/// be sure to call [wrap] afterwards.
///
/// [wrap]: #method.wrap
pub fn unwrap(mut ref_: Self) -> &'a mut T {
ref_.consumed = true;
ref_.socket
}
}
impl<'a, T: Session> Deref for Ref<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.socket
}
}
impl<'a, T: Session> DerefMut for Ref<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.socket
}
}
impl<'a, T: Session> Drop for Ref<'a, T> {
fn drop(&mut self) {
if !self.consumed {
Session::finish(self.socket);
}
}
}