Pārlūkot izejas kodu

Make SocketSet private, move to `iface`.

Dario Nieuwenhuis 3 gadi atpakaļ
vecāks
revīzija
4e365ce9ba
5 mainītis faili ar 45 papildinājumiem un 33 dzēšanām
  1. 3 1
      src/iface/interface.rs
  2. 3 0
      src/iface/mod.rs
  3. 2 1
      src/iface/socket_meta.rs
  4. 37 26
      src/iface/socket_set.rs
  5. 0 5
      src/socket/mod.rs

+ 3 - 1
src/iface/interface.rs

@@ -5,6 +5,8 @@
 use core::cmp;
 use managed::{ManagedMap, ManagedSlice};
 
+use super::socket_set::SocketSet;
+use super::{SocketHandle, SocketStorage};
 use crate::iface::Routes;
 #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
 use crate::iface::{NeighborAnswer, NeighborCache};
@@ -108,7 +110,7 @@ let iface = InterfaceBuilder::new(device, vec![])
     )]
     pub fn new<SocketsT>(device: DeviceT, sockets: SocketsT) -> Self
     where
-        SocketsT: Into<ManagedSlice<'a, Option<SocketSetItem<'a>>>>,
+        SocketsT: Into<ManagedSlice<'a, SocketStorage<'a>>>,
     {
         InterfaceBuilder {
             device: device,

+ 3 - 0
src/iface/mod.rs

@@ -8,6 +8,8 @@ mod interface;
 #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
 mod neighbor;
 mod route;
+mod socket_meta;
+mod socket_set;
 
 #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
 pub(crate) use self::neighbor::Answer as NeighborAnswer;
@@ -16,5 +18,6 @@ pub use self::neighbor::Cache as NeighborCache;
 #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
 pub use self::neighbor::Neighbor;
 pub use self::route::{Route, Routes};
+pub use socket_set::{SocketHandle, SocketStorage};
 
 pub use self::interface::{Interface, InterfaceBuilder};

+ 2 - 1
src/socket/meta.rs → src/iface/socket_meta.rs

@@ -1,4 +1,5 @@
-use crate::socket::{PollAt, SocketHandle};
+use super::SocketHandle;
+use crate::socket::PollAt;
 use crate::time::{Duration, Instant};
 use crate::wire::IpAddress;
 

+ 37 - 26
src/socket/set.rs → src/iface/socket_set.rs

@@ -1,26 +1,35 @@
 use core::fmt;
 use managed::ManagedSlice;
 
+use super::socket_meta::Meta;
 use crate::socket::{AnySocket, Socket};
 
-use super::meta::Meta;
+/// Opaque struct with space for storing one socket.
+///
+/// This is public so you can use it to allocate space for storing
+/// sockets when creating an Interface.
+#[derive(Debug, Default)]
+pub struct SocketStorage<'a> {
+    inner: Option<Item<'a>>,
+}
+
+impl<'a> SocketStorage<'a> {
+    pub const EMPTY: Self = Self { inner: None };
+}
 
 /// An item of a socket set.
-///
-/// The only reason this struct is public is to allow the socket set storage
-/// to be allocated externally.
 #[derive(Debug)]
-pub struct Item<'a> {
+pub(crate) struct Item<'a> {
     pub(crate) meta: Meta,
     pub(crate) socket: Socket<'a>,
 }
 
-/// A handle, identifying a socket in a set.
+/// A handle, identifying a socket in an Interface.
 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
 #[cfg_attr(feature = "defmt", derive(defmt::Format))]
-pub struct Handle(usize);
+pub struct SocketHandle(usize);
 
-impl fmt::Display for Handle {
+impl fmt::Display for SocketHandle {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         write!(f, "#{}", self.0)
     }
@@ -30,38 +39,40 @@ impl fmt::Display for Handle {
 ///
 /// The lifetime `'a` is used when storing a `Socket<'a>`.
 #[derive(Debug)]
-pub struct Set<'a> {
-    sockets: ManagedSlice<'a, Option<Item<'a>>>,
+pub(crate) struct SocketSet<'a> {
+    sockets: ManagedSlice<'a, SocketStorage<'a>>,
 }
 
-impl<'a> Set<'a> {
+impl<'a> SocketSet<'a> {
     /// Create a socket set using the provided storage.
-    pub fn new<SocketsT>(sockets: SocketsT) -> Set<'a>
+    pub fn new<SocketsT>(sockets: SocketsT) -> SocketSet<'a>
     where
-        SocketsT: Into<ManagedSlice<'a, Option<Item<'a>>>>,
+        SocketsT: Into<ManagedSlice<'a, SocketStorage<'a>>>,
     {
         let sockets = sockets.into();
-        Set { sockets }
+        SocketSet { sockets }
     }
 
     /// Add a socket to the set, and return its handle.
     ///
     /// # Panics
     /// This function panics if the storage is fixed-size (not a `Vec`) and is full.
-    pub fn add<T: AnySocket<'a>>(&mut self, socket: T) -> Handle {
-        fn put<'a>(index: usize, slot: &mut Option<Item<'a>>, socket: Socket<'a>) -> Handle {
+    pub fn add<T: AnySocket<'a>>(&mut self, socket: T) -> SocketHandle {
+        fn put<'a>(index: usize, slot: &mut SocketStorage<'a>, socket: Socket<'a>) -> SocketHandle {
             net_trace!("[{}]: adding", index);
-            let handle = Handle(index);
+            let handle = SocketHandle(index);
             let mut meta = Meta::default();
             meta.handle = handle;
-            *slot = Some(Item { socket, meta });
+            *slot = SocketStorage {
+                inner: Some(Item { meta, socket }),
+            };
             handle
         }
 
         let socket = socket.upcast();
 
         for (index, slot) in self.sockets.iter_mut().enumerate() {
-            if slot.is_none() {
+            if slot.inner.is_none() {
                 return put(index, slot, socket);
             }
         }
@@ -70,7 +81,7 @@ impl<'a> Set<'a> {
             ManagedSlice::Borrowed(_) => panic!("adding a socket to a full SocketSet"),
             #[cfg(any(feature = "std", feature = "alloc"))]
             ManagedSlice::Owned(ref mut sockets) => {
-                sockets.push(None);
+                sockets.push(SocketStorage { inner: None });
                 let index = sockets.len() - 1;
                 put(index, &mut sockets[index], socket)
             }
@@ -82,8 +93,8 @@ impl<'a> Set<'a> {
     /// # Panics
     /// This function may panic if the handle does not belong to this socket set
     /// or the socket has the wrong type.
-    pub fn get<T: AnySocket<'a>>(&mut self, handle: Handle) -> &mut T {
-        match self.sockets[handle.0].as_mut() {
+    pub fn get<T: AnySocket<'a>>(&mut self, handle: SocketHandle) -> &mut T {
+        match self.sockets[handle.0].inner.as_mut() {
             Some(item) => {
                 T::downcast(&mut item.socket).expect("handle refers to a socket of a wrong type")
             }
@@ -95,9 +106,9 @@ impl<'a> Set<'a> {
     ///
     /// # Panics
     /// This function may panic if the handle does not belong to this socket set.
-    pub fn remove(&mut self, handle: Handle) -> Socket<'a> {
+    pub fn remove(&mut self, handle: SocketHandle) -> Socket<'a> {
         net_trace!("[{}]: removing", handle.0);
-        match self.sockets[handle.0].take() {
+        match self.sockets[handle.0].inner.take() {
             Some(item) => item.socket,
             None => panic!("handle does not refer to a valid socket"),
         }
@@ -105,11 +116,11 @@ impl<'a> Set<'a> {
 
     /// Iterate every socket in this set.
     pub fn iter(&self) -> impl Iterator<Item = &Item<'a>> + '_ {
-        self.sockets.iter().filter_map(|x| x.as_ref())
+        self.sockets.iter().filter_map(|x| x.inner.as_ref())
     }
 
     /// Iterate every socket in this set.
     pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Item<'a>> + '_ {
-        self.sockets.iter_mut().filter_map(|x| x.as_mut())
+        self.sockets.iter_mut().filter_map(|x| x.inner.as_mut())
     }
 }

+ 0 - 5
src/socket/mod.rs

@@ -14,9 +14,6 @@ size for a buffer, allocate it, and let the networking stack use it.
 use crate::phy::DeviceCapabilities;
 use crate::time::Instant;
 
-mod meta;
-mod set;
-
 #[cfg(feature = "socket-dhcpv4")]
 mod dhcpv4;
 #[cfg(feature = "socket-icmp")]
@@ -31,8 +28,6 @@ mod udp;
 #[cfg(feature = "async")]
 mod waker;
 
-pub use self::set::{Handle as SocketHandle, Item as SocketSetItem, Set as SocketSet};
-
 #[cfg(feature = "socket-dhcpv4")]
 pub use self::dhcpv4::{Config as Dhcpv4Config, Dhcpv4Socket, Event as Dhcpv4Event};
 #[cfg(feature = "socket-icmp")]