-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathmap_batch.rs
More file actions
120 lines (105 loc) · 2.69 KB
/
Copy pathmap_batch.rs
File metadata and controls
120 lines (105 loc) · 2.69 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use super::Batch;
use super::act::Act;
use super::iterator::*;
use super::packet_batch::PacketBatch;
use common::*;
use headers::EndOffset;
use interface::Packet;
use interface::PacketTx;
use std::marker::PhantomData;
pub type MapFn<T, M> = Box<FnMut(&Packet<T, M>) + Send>;
pub struct MapBatch<T, V>
where
T: EndOffset,
V: Batch + BatchIterator<Header = T> + Act,
{
parent: V,
transformer: MapFn<T, V::Metadata>,
applied: bool,
phantom_t: PhantomData<T>,
}
impl<T, V> MapBatch<T, V>
where
T: EndOffset,
V: Batch + BatchIterator<Header = T> + Act,
{
pub fn new(parent: V, transformer: MapFn<T, V::Metadata>) -> MapBatch<T, V> {
MapBatch {
parent: parent,
transformer: transformer,
applied: false,
phantom_t: PhantomData,
}
}
}
impl<T, V> Batch for MapBatch<T, V>
where
T: EndOffset,
V: Batch + BatchIterator<Header = T> + Act,
{
}
impl<T, V> Act for MapBatch<T, V>
where
T: EndOffset,
V: Batch + BatchIterator<Header = T> + Act,
{
#[inline]
fn act(&mut self) {
if !self.applied {
self.parent.act();
{
let iter = PayloadEnumerator::<T, V::Metadata>::new(&mut self.parent);
while let Some(ParsedDescriptor { packet, .. }) = iter.next(&mut self.parent) {
(self.transformer)(&packet);
}
}
self.applied = true;
}
}
#[inline]
fn done(&mut self) {
self.applied = false;
self.parent.done();
}
#[inline]
fn send_q(&mut self, port: &PacketTx) -> Result<u32> {
self.parent.send_q(port)
}
#[inline]
fn capacity(&self) -> i32 {
self.parent.capacity()
}
#[inline]
fn drop_packets(&mut self, idxes: &[usize]) -> Option<usize> {
self.parent.drop_packets(idxes)
}
#[inline]
fn clear_packets(&mut self) {
self.parent.clear_packets()
}
#[inline]
fn get_packet_batch(&mut self) -> &mut PacketBatch {
self.parent.get_packet_batch()
}
#[inline]
fn get_task_dependencies(&self) -> Vec<usize> {
self.parent.get_task_dependencies()
}
}
impl<T, V> BatchIterator for MapBatch<T, V>
where
T: EndOffset,
V: Batch + BatchIterator<Header = T> + Act,
{
type Header = T;
type Metadata = <V as BatchIterator>::Metadata;
#[inline]
fn start(&mut self) -> usize {
self.parent.start()
}
#[inline]
unsafe fn next_payload(&mut self, idx: usize) -> Option<PacketDescriptor<T, Self::Metadata>> {
// self.parent.next_payload(idx).map(|p| {(self.transformer)(&p.packet); p})
self.parent.next_payload(idx)
}
}