2021-02-03 05:09:37 +01:00
|
|
|
use core::ops::{Deref, DerefMut, Range};
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
use as_slice::{AsMutSlice, AsSlice};
|
2021-04-07 19:06:45 +02:00
|
|
|
use atomic_pool::{pool, Box};
|
2021-02-03 05:09:37 +01:00
|
|
|
|
2021-06-07 07:30:38 +02:00
|
|
|
pub const MTU: usize = 1516;
|
2021-06-14 23:06:12 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "pool-4")]
|
2021-02-03 05:09:37 +01:00
|
|
|
pub const PACKET_POOL_SIZE: usize = 4;
|
|
|
|
|
2021-06-14 23:06:12 +02:00
|
|
|
#[cfg(feature = "pool-8")]
|
|
|
|
pub const PACKET_POOL_SIZE: usize = 8;
|
|
|
|
|
|
|
|
#[cfg(feature = "pool-16")]
|
|
|
|
pub const PACKET_POOL_SIZE: usize = 16;
|
|
|
|
|
|
|
|
#[cfg(feature = "pool-32")]
|
|
|
|
pub const PACKET_POOL_SIZE: usize = 32;
|
|
|
|
|
2022-04-27 21:27:28 +02:00
|
|
|
#[cfg(feature = "pool-64")]
|
|
|
|
pub const PACKET_POOL_SIZE: usize = 64;
|
|
|
|
|
|
|
|
#[cfg(feature = "pool-128")]
|
|
|
|
pub const PACKET_POOL_SIZE: usize = 128;
|
|
|
|
|
2021-02-03 05:09:37 +01:00
|
|
|
pool!(pub PacketPool: [Packet; PACKET_POOL_SIZE]);
|
|
|
|
pub type PacketBox = Box<PacketPool>;
|
|
|
|
|
2021-06-07 07:30:38 +02:00
|
|
|
#[repr(align(4))]
|
2021-02-03 05:09:37 +01:00
|
|
|
pub struct Packet(pub [u8; MTU]);
|
|
|
|
|
|
|
|
impl Packet {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self([0; MTU])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 19:06:45 +02:00
|
|
|
pub trait PacketBoxExt {
|
|
|
|
fn slice(self, range: Range<usize>) -> PacketBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PacketBoxExt for PacketBox {
|
|
|
|
fn slice(self, range: Range<usize>) -> PacketBuf {
|
2022-06-12 22:15:44 +02:00
|
|
|
PacketBuf { packet: self, range }
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsSlice for Packet {
|
|
|
|
type Element = u8;
|
|
|
|
|
|
|
|
fn as_slice(&self) -> &[Self::Element] {
|
|
|
|
&self.deref()[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsMutSlice for Packet {
|
|
|
|
fn as_mut_slice(&mut self) -> &mut [Self::Element] {
|
|
|
|
&mut self.deref_mut()[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Packet {
|
|
|
|
type Target = [u8; MTU];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[u8; MTU] {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Packet {
|
|
|
|
fn deref_mut(&mut self) -> &mut [u8; MTU] {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PacketBuf {
|
|
|
|
packet: PacketBox,
|
|
|
|
range: Range<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsSlice for PacketBuf {
|
|
|
|
type Element = u8;
|
|
|
|
|
|
|
|
fn as_slice(&self) -> &[Self::Element] {
|
|
|
|
&self.packet[self.range.clone()]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsMutSlice for PacketBuf {
|
|
|
|
fn as_mut_slice(&mut self) -> &mut [Self::Element] {
|
|
|
|
&mut self.packet[self.range.clone()]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for PacketBuf {
|
|
|
|
type Target = [u8];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[u8] {
|
|
|
|
&self.packet[self.range.clone()]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for PacketBuf {
|
|
|
|
fn deref_mut(&mut self) -> &mut [u8] {
|
|
|
|
&mut self.packet[self.range.clone()]
|
|
|
|
}
|
|
|
|
}
|