2017-09-11 04:18:12 +08:00
|
|
|
use core::ops::{Deref, DerefMut};
|
|
|
|
|
2017-10-25 08:20:40 +08:00
|
|
|
#[cfg(feature = "proto-raw")]
|
2017-09-11 04:18:12 +08:00
|
|
|
use socket::RawSocket;
|
2017-10-25 08:20:40 +08:00
|
|
|
#[cfg(feature = "proto-udp")]
|
2017-09-11 04:18:12 +08:00
|
|
|
use socket::UdpSocket;
|
2017-10-25 08:20:40 +08:00
|
|
|
#[cfg(feature = "proto-tcp")]
|
2017-09-11 04:18:12 +08:00
|
|
|
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) {}
|
|
|
|
}
|
|
|
|
|
2017-10-25 08:20:40 +08:00
|
|
|
#[cfg(feature = "proto-raw")]
|
2017-09-11 04:18:12 +08:00
|
|
|
impl<'a, 'b> Session for RawSocket<'a, 'b> {}
|
2017-10-25 08:20:40 +08:00
|
|
|
#[cfg(feature = "proto-udp")]
|
2017-09-11 04:18:12 +08:00
|
|
|
impl<'a, 'b> Session for UdpSocket<'a, 'b> {}
|
2017-10-25 08:20:40 +08:00
|
|
|
#[cfg(feature = "proto-tcp")]
|
2017-09-11 04:18:12 +08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:28:57 +08:00
|
|
|
impl<'a, T: Session + 'a> Ref<'a, T> {
|
|
|
|
/// Wrap a pointer to a socket to make a smart pointer.
|
|
|
|
///
|
2017-10-25 10:16:23 +08:00
|
|
|
/// Calling this function is only necessary if your code is using [into_inner].
|
2017-10-25 09:28:57 +08:00
|
|
|
///
|
2017-10-25 10:16:23 +08:00
|
|
|
/// [into_inner]: #method.into_inner
|
|
|
|
pub fn new(socket: &'a mut T) -> Self {
|
2017-09-11 04:18:12 +08:00
|
|
|
Ref { socket, consumed: false }
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:28:57 +08:00
|
|
|
/// 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);
|
2017-10-25 10:16:23 +08:00
|
|
|
/// be sure to call [new] afterwards.
|
2017-10-25 09:28:57 +08:00
|
|
|
///
|
2017-10-25 10:16:23 +08:00
|
|
|
/// [new]: #method.new
|
|
|
|
pub fn into_inner(mut ref_: Self) -> &'a mut T {
|
2017-10-25 09:28:57 +08:00
|
|
|
ref_.consumed = true;
|
|
|
|
ref_.socket
|
2017-09-11 04:18:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|