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.rs88
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 @@
1use as_slice::{AsMutSlice, AsSlice};
2use core::ops::{Deref, DerefMut, Range};
3
4use super::pool::{BitPool, Box, StaticPool};
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
20impl Box<PacketPool> {
21 pub fn slice(self, range: Range<usize>) -> PacketBuf {
22 PacketBuf {
23 packet: self,
24 range,
25 }
26 }
27}
28
29impl AsSlice for Packet {
30 type Element = u8;
31
32 fn as_slice(&self) -> &[Self::Element] {
33 &self.deref()[..]
34 }
35}
36
37impl AsMutSlice for Packet {
38 fn as_mut_slice(&mut self) -> &mut [Self::Element] {
39 &mut self.deref_mut()[..]
40 }
41}
42
43impl Deref for Packet {
44 type Target = [u8; MTU];
45
46 fn deref(&self) -> &[u8; MTU] {
47 &self.0
48 }
49}
50
51impl DerefMut for Packet {
52 fn deref_mut(&mut self) -> &mut [u8; MTU] {
53 &mut self.0
54 }
55}
56
57pub struct PacketBuf {
58 packet: PacketBox,
59 range: Range<usize>,
60}
61
62impl AsSlice for PacketBuf {
63 type Element = u8;
64
65 fn as_slice(&self) -> &[Self::Element] {
66 &self.packet[self.range.clone()]
67 }
68}
69
70impl AsMutSlice for PacketBuf {
71 fn as_mut_slice(&mut self) -> &mut [Self::Element] {
72 &mut self.packet[self.range.clone()]
73 }
74}
75
76impl Deref for PacketBuf {
77 type Target = [u8];
78
79 fn deref(&self) -> &[u8] {
80 &self.packet[self.range.clone()]
81 }
82}
83
84impl DerefMut for PacketBuf {
85 fn deref_mut(&mut self) -> &mut [u8] {
86 &mut self.packet[self.range.clone()]
87 }
88}