diff options
Diffstat (limited to 'embassy-net/src/packet_pool.rs')
| -rw-r--r-- | embassy-net/src/packet_pool.rs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/embassy-net/src/packet_pool.rs b/embassy-net/src/packet_pool.rs new file mode 100644 index 000000000..246356431 --- /dev/null +++ b/embassy-net/src/packet_pool.rs | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | use as_slice::{AsMutSlice, AsSlice}; | ||
| 2 | use core::ops::{Deref, DerefMut, Range}; | ||
| 3 | |||
| 4 | use super::pool::{BitPool, Box, StaticPool}; | ||
| 5 | |||
| 6 | pub const MTU: usize = 1514; | ||
| 7 | pub const PACKET_POOL_SIZE: usize = 4; | ||
| 8 | |||
| 9 | pool!(pub PacketPool: [Packet; PACKET_POOL_SIZE]); | ||
| 10 | pub type PacketBox = Box<PacketPool>; | ||
| 11 | |||
| 12 | pub struct Packet(pub [u8; MTU]); | ||
| 13 | |||
| 14 | impl Packet { | ||
| 15 | pub const fn new() -> Self { | ||
| 16 | Self([0; MTU]) | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | impl Box<PacketPool> { | ||
| 21 | pub fn slice(self, range: Range<usize>) -> PacketBuf { | ||
| 22 | PacketBuf { | ||
| 23 | packet: self, | ||
| 24 | range, | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | impl AsSlice for Packet { | ||
| 30 | type Element = u8; | ||
| 31 | |||
| 32 | fn as_slice(&self) -> &[Self::Element] { | ||
| 33 | &self.deref()[..] | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | impl AsMutSlice for Packet { | ||
| 38 | fn as_mut_slice(&mut self) -> &mut [Self::Element] { | ||
| 39 | &mut self.deref_mut()[..] | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | impl Deref for Packet { | ||
| 44 | type Target = [u8; MTU]; | ||
| 45 | |||
| 46 | fn deref(&self) -> &[u8; MTU] { | ||
| 47 | &self.0 | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | impl DerefMut for Packet { | ||
| 52 | fn deref_mut(&mut self) -> &mut [u8; MTU] { | ||
| 53 | &mut self.0 | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | pub struct PacketBuf { | ||
| 58 | packet: PacketBox, | ||
| 59 | range: Range<usize>, | ||
| 60 | } | ||
| 61 | |||
| 62 | impl AsSlice for PacketBuf { | ||
| 63 | type Element = u8; | ||
| 64 | |||
| 65 | fn as_slice(&self) -> &[Self::Element] { | ||
| 66 | &self.packet[self.range.clone()] | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | impl AsMutSlice for PacketBuf { | ||
| 71 | fn as_mut_slice(&mut self) -> &mut [Self::Element] { | ||
| 72 | &mut self.packet[self.range.clone()] | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | impl Deref for PacketBuf { | ||
| 77 | type Target = [u8]; | ||
| 78 | |||
| 79 | fn deref(&self) -> &[u8] { | ||
| 80 | &self.packet[self.range.clone()] | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | impl DerefMut for PacketBuf { | ||
| 85 | fn deref_mut(&mut self) -> &mut [u8] { | ||
| 86 | &mut self.packet[self.range.clone()] | ||
| 87 | } | ||
| 88 | } | ||
