1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::ops::{Deref, DerefMut};

/// The maximum usable packet size by `turbulence`.
///
/// It is not useful for an implementation of `PacketPool` to return packets with larger capacity
/// than this, `turbulence` may not be able to use the entire packet capacity otherwise.
pub const MAX_PACKET_LEN: u16 = 32768;

/// A trait for packet buffers used by `turbulence`.
pub trait Packet: Deref<Target = [u8]> + DerefMut {
    /// Static capacity of this packet
    fn capacity(&self) -> usize;

    /// Resizes the packet to the given length, which must be at most the static capacity.
    fn resize(&mut self, len: usize, val: u8);

    fn extend(&mut self, other: &[u8]) {
        let cur_len = self.len();
        let new_len = cur_len + other.len();
        self.resize(new_len, 0);
        self[cur_len..new_len].copy_from_slice(other);
    }

    fn truncate(&mut self, len: usize) {
        let len = len.min(self.len());
        self.resize(len, 0);
    }

    fn clear(&mut self) {
        self.resize(0, 0);
    }

    fn as_slice(&self) -> &[u8] {
        self.deref()
    }

    fn as_mut_slice(&mut self) -> &mut [u8] {
        self.deref_mut()
    }
}

/// Trait for packet allocation and pooling.
///
/// All packets that are allocated from `turbulence` are allocated through this interface.
///
/// Packets must implement the `Packet` trait and should all have the same capacity: the MTU for
/// whatever the underlying transport is, up to `MAX_PACKET_LEN` in size.
pub trait PacketPool {
    type Packet: Packet;

    fn acquire(&self) -> Self::Packet;
}