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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! Stream-based Ports
use futures::channel::mpsc::Sender;
use std::any::Any;
use std::any::TypeId;
use std::fmt;
use std::mem;
use std::slice;

use crate::runtime::buffer::BufferReader;
use crate::runtime::buffer::BufferWriter;
use crate::runtime::tag::default_tag_propagation;
use crate::runtime::BlockMessage;
use crate::runtime::ItemTag;
use crate::runtime::Tag;

#[derive(Debug)]
struct CurrentInput {
    ptr: *const u8,
    len: usize,
    index: usize,
    tags: Vec<ItemTag>,
}

// Needed for raw pointer `ptr`
unsafe impl Send for CurrentInput {}

/// Stream input port
#[derive(Debug)]
pub struct StreamInput {
    name: String,
    item_size: usize,
    type_id: TypeId,
    reader: Option<BufferReader>,
    current: Option<CurrentInput>,
    tags: Vec<ItemTag>,
}

impl StreamInput {
    /// Create stream input with given name
    pub fn new<T: Any>(name: &str) -> StreamInput {
        StreamInput {
            name: name.to_string(),
            item_size: std::mem::size_of::<T>(),
            type_id: TypeId::of::<T>(),
            reader: None,
            current: None,
            tags: Vec::new(),
        }
    }

    /// Get size of items, handled by the port
    pub fn item_size(&self) -> usize {
        self.item_size
    }

    /// Get [`TypeId`] of items, handled by the port
    pub fn type_id(&self) -> TypeId {
        self.type_id
    }

    /// Get name of port
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Try to cast buffer reader to specific type
    pub fn try_as<T: 'static>(&mut self) -> Option<&mut T> {
        self.reader.as_mut().unwrap().try_as::<T>()
    }

    /// Consume `amount` samples from buffer
    pub fn consume(&mut self, amount: usize) {
        debug_assert!(self.current.is_some());
        debug_assert!(
            amount
                <= self.current.as_mut().unwrap().len
                    - self.current.as_mut().unwrap().index * self.item_size
        );

        self.current.as_mut().unwrap().index += amount * self.item_size;
        self.tags.retain(|x| x.index >= amount);
        self.tags.iter_mut().for_each(|x| x.index -= amount);
    }

    /// Get buffer content as slice
    pub fn slice<T>(&mut self) -> &'static [T] {
        assert_eq!(self.type_id, TypeId::of::<T>());
        self.slice_unchecked()
    }

    /// Get buffer content as slice without checking the type
    pub fn slice_unchecked<T>(&mut self) -> &'static [T] {
        if self.current.is_none() {
            let (ptr, len, tags) = self.reader.as_mut().unwrap().bytes();
            self.tags = tags;
            self.tags.sort_by_key(|x| x.index);
            self.current = Some(CurrentInput {
                ptr,
                len,
                index: 0,
                tags: self.tags.clone(),
            });
        }

        let c = self.current.as_ref().unwrap();
        unsafe { slice::from_raw_parts(c.ptr as *const T, c.len / mem::size_of::<T>()) }
    }

    /// Returns a mutable slice to the input buffer.
    ///
    /// # Safety
    /// The block has to be the sole reader for the input buffer.
    pub unsafe fn slice_mut<T>(&mut self) -> &'static mut [T] {
        assert_eq!(self.type_id, TypeId::of::<T>());
        self.slice_mut_unchecked()
    }

    /// Returns a mutable slice to the input buffer.
    ///
    /// # Safety
    /// The block has to be the sole reader for the input buffer.
    pub unsafe fn slice_mut_unchecked<T>(&mut self) -> &'static mut [T] {
        let s = self.slice::<T>();
        slice::from_raw_parts_mut(s.as_ptr() as *mut T, s.len())
    }

    /// Get [`ItemTags`](ItemTag) in buffer
    pub fn tags(&mut self) -> &mut Vec<ItemTag> {
        &mut self.tags
    }

    fn commit(&mut self) {
        if let Some(ref c) = self.current {
            let amount = c.index / self.item_size;
            if amount != 0 {
                self.reader.as_mut().unwrap().consume(amount);
            }
            self.current = None;
        }
    }

    /// Items already consumed in this call to work
    pub fn consumed(&self) -> (usize, &Vec<ItemTag>) {
        if let Some(ref c) = self.current {
            (c.index / self.item_size, &c.tags)
        } else {
            (0, &self.tags)
        }
    }

    /// Set the buffer reader
    pub fn set_reader(&mut self, reader: BufferReader) {
        debug_assert!(self.reader.is_none());
        self.reader = Some(reader);
    }

    /// Notify connected, upstream writer that we are finished
    pub async fn notify_finished(&mut self) {
        self.reader.as_mut().unwrap().notify_finished().await;
    }

    /// Mark port as finished
    ///
    /// No further data will become available in this port.
    pub fn finish(&mut self) {
        self.reader.as_mut().unwrap().finish();
    }

    /// Check, if port is marked as finished
    pub fn finished(&self) -> bool {
        self.reader.as_ref().unwrap().finished()
    }
}

/// Stream output port
#[derive(Debug)]
pub struct StreamOutput {
    name: String,
    item_size: usize,
    type_id: TypeId,
    writer: Option<BufferWriter>,
    tags: Vec<ItemTag>,
    offset: usize,
}

impl StreamOutput {
    /// Create stream output port
    pub fn new<T: Any>(name: &str) -> StreamOutput {
        StreamOutput {
            name: name.to_string(),
            item_size: std::mem::size_of::<T>(),
            type_id: TypeId::of::<T>(),
            writer: None,
            tags: Vec::new(),
            offset: 0,
        }
    }

    /// Get size of items, handled by the port
    pub fn item_size(&self) -> usize {
        self.item_size
    }

    /// Get [`TypeId`] of items, handled by the port
    pub fn type_id(&self) -> TypeId {
        self.type_id
    }

    /// Get name of port
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Initialize port, setting the writer
    pub fn init(&mut self, writer: BufferWriter) {
        debug_assert!(self.writer.is_none());
        self.writer = Some(writer);
    }

    /// Add [`ItemTag`] to sample in port
    pub fn add_tag(&mut self, index: usize, tag: Tag) {
        self.tags.push(ItemTag {
            index: index + self.offset,
            tag,
        });
    }

    /// Add [`ItemTag`] using the absolute index
    ///
    /// The difference between `add_tag` and `add_tag_abs` is only relevant if the work function
    /// calls produce multiple times.
    pub fn add_tag_abs(&mut self, index: usize, tag: Tag) {
        self.tags.push(ItemTag { index, tag });
    }

    /// Connect a downstream reader to the port
    pub fn add_reader(
        &mut self,
        reader_inbox: Sender<BlockMessage>,
        reader_port: usize,
    ) -> BufferReader {
        debug_assert!(self.writer.is_some());
        self.writer
            .as_mut()
            .unwrap()
            .add_reader(reader_inbox, reader_port)
    }

    /// Try to cast buffer writer to specific type
    pub fn try_as<T: 'static>(&mut self) -> Option<&mut T> {
        self.writer.as_mut().unwrap().try_as::<T>()
    }

    /// Produce `amount` samples
    pub fn produce(&mut self, amount: usize) {
        self.offset += amount;
    }

    /// Get buffer content as slice
    pub fn slice<T>(&mut self) -> &'static mut [T] {
        assert_eq!(self.type_id, TypeId::of::<T>());
        self.slice_unchecked()
    }

    /// Get buffer content as slice without checking the type
    pub fn slice_unchecked<T>(&mut self) -> &'static mut [T] {
        let (ptr, len) = self.writer.as_mut().unwrap().bytes();

        unsafe {
            slice::from_raw_parts_mut(
                ptr.cast::<T>().add(self.offset),
                (len / mem::size_of::<T>()) - self.offset,
            )
        }
    }

    fn commit(&mut self) {
        if self.offset == 0 {
            return;
        }

        let mut tmp = self.tags.clone();
        tmp.retain(|x| x.index < self.offset);
        self.tags.retain(|x| x.index >= self.offset);

        self.writer.as_mut().unwrap().produce(self.offset, tmp);
        self.offset = 0;
    }

    /// Items already produced in this call to work
    pub fn produced(&self) -> usize {
        self.offset
    }

    /// Notify downstream readers that we are finished
    pub async fn notify_finished(&mut self) {
        self.writer.as_mut().unwrap().notify_finished().await;
    }

    /// Mark port as finshed
    pub fn finish(&mut self) {
        self.writer.as_mut().unwrap().finish();
    }

    /// Check, if  port is marked as finished
    pub fn finished(&self) -> bool {
        self.writer.as_ref().unwrap().finished()
    }

    /// Get a mutable reference to the buffer writer
    pub(super) fn writer_mut(&mut self) -> &mut BufferWriter {
        let w = self.writer.as_mut().unwrap();
        w
    }
}

/// Stream IO
pub struct StreamIo {
    inputs: Vec<StreamInput>,
    outputs: Vec<StreamOutput>,
    #[allow(clippy::type_complexity)]
    tag_propagation: Box<dyn FnMut(&mut [StreamInput], &mut [StreamOutput]) + Send + 'static>,
}

impl fmt::Debug for StreamIo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("StreamIo")
            .field("inputs", &self.inputs)
            .field("outputs", &self.outputs)
            .finish()
    }
}

impl StreamIo {
    #[allow(clippy::type_complexity)]
    fn new(
        inputs: Vec<StreamInput>,
        outputs: Vec<StreamOutput>,
        tag_propagation: Box<dyn FnMut(&mut [StreamInput], &mut [StreamOutput]) + Send + 'static>,
    ) -> StreamIo {
        StreamIo {
            inputs,
            outputs,
            tag_propagation,
        }
    }

    /// All inputs
    pub fn inputs(&self) -> &Vec<StreamInput> {
        &self.inputs
    }

    /// All inputs mutable
    pub fn inputs_mut(&mut self) -> &mut Vec<StreamInput> {
        &mut self.inputs
    }

    /// Get input, given its name
    pub fn input_by_name(&self, name: &str) -> Option<&StreamInput> {
        self.inputs.iter().find(|x| x.name() == name)
    }

    /// Get input mutably, given its name
    pub fn input_by_name_mut(&mut self, name: &str) -> Option<&mut StreamInput> {
        self.inputs.iter_mut().find(|x| x.name() == name)
    }

    /// Get reference to input
    pub fn input_ref(&self, id: usize) -> &StreamInput {
        &self.inputs[id]
    }

    /// Get mutably reference to input
    pub fn input(&mut self, id: usize) -> &mut StreamInput {
        &mut self.inputs[id]
    }

    /// Get input name, given its Id
    pub fn input_name_to_id(&self, name: &str) -> Option<usize> {
        self.inputs
            .iter()
            .enumerate()
            .find(|item| item.1.name() == name)
            .map(|(i, _)| i)
    }

    /// All outputs
    pub fn outputs(&self) -> &Vec<StreamOutput> {
        &self.outputs
    }

    /// All outputs mutable
    pub fn outputs_mut(&mut self) -> &mut Vec<StreamOutput> {
        &mut self.outputs
    }

    /// Get output, given its name
    pub fn output_by_name(&self, name: &str) -> Option<&StreamOutput> {
        self.outputs.iter().find(|x| x.name() == name)
    }

    /// Get output mutable, given its name
    pub fn output_by_name_mut(&mut self, name: &str) -> Option<&mut StreamOutput> {
        self.outputs.iter_mut().find(|x| x.name() == name)
    }

    /// Get reference to output
    pub fn output_ref(&self, id: usize) -> &StreamOutput {
        &self.outputs[id]
    }

    /// Get mutable reference to output
    pub fn output(&mut self, id: usize) -> &mut StreamOutput {
        &mut self.outputs[id]
    }

    /// Get output Id, given its name
    pub fn output_name_to_id(&self, name: &str) -> Option<usize> {
        self.outputs
            .iter()
            .enumerate()
            .find(|item| item.1.name() == name)
            .map(|(i, _)| i)
    }

    /// Commit all consume/produce calls after `work()` call
    pub fn commit(&mut self) {
        (self.tag_propagation)(&mut self.inputs, &mut self.outputs);
        for i in self.inputs_mut() {
            i.commit();
        }
        for o in self.outputs_mut() {
            o.commit();
        }
    }

    /// Set tag propagation
    #[allow(clippy::type_complexity)]
    pub fn set_tag_propagation(
        &mut self,
        f: Box<dyn FnMut(&mut [StreamInput], &mut [StreamOutput]) + Send + 'static>,
    ) {
        self.tag_propagation = f;
    }
}

/// Stream IO builder
#[allow(clippy::type_complexity)]
pub struct StreamIoBuilder {
    inputs: Vec<StreamInput>,
    outputs: Vec<StreamOutput>,
    tag_propagation: Box<dyn FnMut(&mut [StreamInput], &mut [StreamOutput]) + Send + 'static>,
}

impl StreamIoBuilder {
    /// Create builder
    pub fn new() -> StreamIoBuilder {
        StreamIoBuilder {
            inputs: Vec::new(),
            outputs: Vec::new(),
            tag_propagation: Box::new(default_tag_propagation),
        }
    }

    /// Add input port
    #[must_use]
    pub fn add_input<T: Any>(mut self, name: &str) -> StreamIoBuilder {
        self.inputs.push(StreamInput::new::<T>(name));
        self
    }

    /// Add output port
    #[must_use]
    pub fn add_output<T: Any>(mut self, name: &str) -> StreamIoBuilder {
        self.outputs.push(StreamOutput::new::<T>(name));
        self
    }

    /// Configure tag propagation
    #[must_use]
    pub fn tag_propagation<F: FnMut(&mut [StreamInput], &mut [StreamOutput]) + Send + 'static>(
        mut self,
        f: F,
    ) -> StreamIoBuilder {
        self.tag_propagation = Box::new(f);
        self
    }

    /// Build Stream IO
    pub fn build(self) -> StreamIo {
        StreamIo::new(self.inputs, self.outputs, self.tag_propagation)
    }
}

impl Default for StreamIoBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn stream_connect() {
        let i = StreamInput::new::<f32>("foo");
        assert_eq!(i.name(), "foo");
        assert_eq!(i.item_size(), 4);

        let o = StreamOutput::new::<f32>("foo");
        assert_eq!(o.name(), "foo");
        assert_eq!(o.item_size(), 4);
    }
}