2017-09-11 04:18:12 +08:00
|
|
|
use core::ops::{Deref, DerefMut};
|
|
|
|
|
2017-11-06 17:51:03 +08:00
|
|
|
#[cfg(feature = "socket-raw")]
|
2017-09-11 04:18:12 +08:00
|
|
|
use socket::RawSocket;
|
2018-05-09 21:27:51 +08:00
|
|
|
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
|
2017-10-30 08:19:34 +08:00
|
|
|
use socket::IcmpSocket;
|
2017-11-06 17:51:03 +08:00
|
|
|
#[cfg(feature = "socket-udp")]
|
2017-09-11 04:18:12 +08:00
|
|
|
use socket::UdpSocket;
|
2017-11-06 17:51:03 +08:00
|
|
|
#[cfg(feature = "socket-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-11-06 17:51:03 +08:00
|
|
|
#[cfg(feature = "socket-raw")]
|
2017-09-11 04:18:12 +08:00
|
|
|
impl<'a, 'b> Session for RawSocket<'a, 'b> {}
|
2018-05-09 21:27:51 +08:00
|
|
|
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
|
2017-10-30 08:19:34 +08:00
|
|
|
impl<'a, 'b> Session for IcmpSocket<'a, 'b> {}
|
2017-11-06 17:51:03 +08:00
|
|
|
#[cfg(feature = "socket-udp")]
|
2017-09-11 04:18:12 +08:00
|
|
|
impl<'a, 'b> Session for UdpSocket<'a, 'b> {}
|
2017-11-06 17:51:03 +08:00
|
|
|
#[cfg(feature = "socket-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> {
|
2019-06-22 18:52:39 +08:00
|
|
|
socket: &'a mut T
|
2017-09-11 04:18:12 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-06-22 18:52:39 +08:00
|
|
|
Ref { socket }
|
2017-09-11 04:18:12 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
///
|
2019-06-22 15:49:18 +08:00
|
|
|
/// [new]: #method.new
|
2019-06-22 18:52:39 +08:00
|
|
|
pub fn into_inner(ref_: Self) -> &'a mut T {
|
2017-10-25 09:28:57 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 18:52:39 +08:00
|
|
|
// FIXME: `Session::finish` currently does not do anything, but if it did, this would be a problem,
|
|
|
|
// because `SocketRef::into_inner` would have to use unsafe code, and there's currently no unsafe
|
|
|
|
// code in smoltcp at all (other than the `phy` module). The reason it would need unsafe code is
|
|
|
|
// that it is currently an error to destructure a value that implements Drop (or move out its
|
|
|
|
// fields in any other way), so it'd have to be transmuted away. This is a deficiency in Rust:
|
|
|
|
// it is always safe to ignore the Drop impl during destructuring.
|
|
|
|
//
|
|
|
|
// impl<'a, T: Session> Drop for Ref<'a, T> {
|
|
|
|
// fn drop(&mut self) {
|
|
|
|
// Session::finish(self.socket);
|
|
|
|
// }
|
|
|
|
// }
|