dfir_rs/scheduled/handoff/
mod.rs

1//! Module for all [`Handoff`]-related items.
2
3pub mod handoff_list;
4mod tee;
5mod vector;
6
7use std::any::Any;
8use std::cell::{RefCell, RefMut};
9use std::rc::Rc;
10
11pub use tee::TeeingHandoff;
12pub use vector::VecHandoff;
13
14/// Trait representing something which we can attempt to give an item to.
15pub trait TryCanReceive<T> {
16    // TODO(mingwei): Isn't used.
17    /// Try to give a value to the handoff, may return an error if full, representing backpressure.
18    fn try_give(&self, item: T) -> Result<T, T>;
19}
20
21/// Trait representing somethign which we can give an item to.
22pub trait CanReceive<T> {
23    // TODO: represent backpressure in this return value.
24    /// Give a value to the handoff.
25    fn give(&self, item: T) -> T;
26}
27
28/// A handle onto the metadata part of a [Handoff], with no element type.
29pub trait HandoffMeta: Any {
30    // TODO(justin): more fine-grained info here.
31    /// Return if the handoff is empty.
32    fn is_bottom(&self) -> bool;
33}
34
35impl<H> HandoffMeta for Rc<RefCell<H>>
36where
37    H: HandoffMeta,
38{
39    fn is_bottom(&self) -> bool {
40        self.borrow().is_bottom()
41    }
42}
43
44/// Trait for handoffs to implement.
45pub trait Handoff: Default + HandoffMeta {
46    /// Inner datastructure type.
47    type Inner;
48
49    /// Take the inner datastructure, similar to [`std::mem::take`].
50    fn take_inner(&self) -> Self::Inner;
51
52    /// Take the inner datastructure by swapping input and output buffers.
53    ///
54    /// For better performance over [`Self::take_inner`].
55    fn borrow_mut_swap(&self) -> RefMut<'_, Self::Inner>;
56
57    /// See [`CanReceive::give`].
58    fn give<T>(&self, item: T) -> T
59    where
60        Self: CanReceive<T>,
61    {
62        <Self as CanReceive<T>>::give(self, item)
63    }
64
65    /// See [`TryCanReceive::try_give`].
66    fn try_give<T>(&self, item: T) -> Result<T, T>
67    where
68        Self: TryCanReceive<T>,
69    {
70        <Self as TryCanReceive<T>>::try_give(self, item)
71    }
72}
73
74/// Wrapper around `IntoIterator` to avoid trait impl conflicts.
75#[repr(transparent)]
76#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
77pub struct Iter<I>(pub I)
78where
79    I: IntoIterator;
80impl<I> IntoIterator for Iter<I>
81where
82    I: IntoIterator,
83{
84    type Item = I::Item;
85    type IntoIter = I::IntoIter;
86
87    fn into_iter(self) -> Self::IntoIter {
88        self.0.into_iter()
89    }
90}