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