dfir_rs/scheduled/
input.rs
1#![allow(clippy::allow_attributes, missing_docs, reason = "deprecated code")]
2
3use std::cell::RefCell;
4use std::marker::PhantomData;
5use std::rc::Rc;
6use std::sync::mpsc::SyncSender;
7
8use super::SubgraphId;
9use super::reactor::Reactor;
10
11pub trait Give<T> {
12 fn give(&self, t: T) -> bool;
13}
14
15pub struct Buffer<T>(pub(crate) Rc<RefCell<Vec<T>>>);
16impl<T> Give<T> for Buffer<T> {
17 fn give(&self, t: T) -> bool {
18 (*self.0).borrow_mut().push(t);
19 true
20 }
21}
22
23impl<T> Default for Buffer<T> {
24 fn default() -> Self {
25 Buffer(Rc::new(RefCell::new(Vec::new())))
26 }
27}
28
29impl<T> Clone for Buffer<T> {
30 fn clone(&self) -> Self {
31 Buffer(self.0.clone())
32 }
33}
34
35impl<T> Give<T> for SyncSender<T> {
36 fn give(&self, t: T) -> bool {
37 self.send(t).is_ok()
38 }
39}
40
41pub struct Input<T, G>
45where
46 G: Give<T>,
47{
48 reactor: Reactor,
49 sg_id: SubgraphId,
50 givable: G,
51 _marker: PhantomData<T>,
52}
53impl<T, G> Input<T, G>
54where
55 G: Give<T>,
56{
57 pub fn new(reactor: Reactor, sg_id: SubgraphId, givable: G) -> Self {
58 Input {
59 reactor,
60 sg_id,
61 givable,
62 _marker: PhantomData,
63 }
64 }
65
66 pub fn give(&self, t: T) {
67 self.givable.give(t);
68 }
69
70 pub fn flush(&self) {
71 self.reactor.trigger(self.sg_id).unwrap();
72 }
73}