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
//! Module for all [`Handoff`]-related items.
pub mod handoff_list;
mod tee;
mod vector;
use std::any::Any;
use std::cell::RefMut;
pub use tee::TeeingHandoff;
pub use vector::VecHandoff;
/// Trait representing something which we can attempt to give an item to.
pub trait TryCanReceive<T> {
// TODO(mingwei): Isn't used.
/// Try to give a value to the handoff, may return an error if full, representing backpressure.
fn try_give(&self, item: T) -> Result<T, T>;
}
/// Trait representing somethign which we can give an item to.
pub trait CanReceive<T> {
// TODO: represent backpressure in this return value.
/// Give a value to the handoff.
fn give(&self, item: T) -> T;
}
/// A handle onto the metadata part of a [Handoff], with no element type.
pub trait HandoffMeta: Any {
/// Helper to cast an instance of `HandoffMeta` to [`Any`]. In general you cannot cast between
/// traits, including [`Any`], but this helper method works around that limitation.
///
/// For implementors: the body of this method will generally just be `{ self }`.
fn any_ref(&self) -> &dyn Any;
// TODO(justin): more fine-grained info here.
/// Return if the handoff is empty.
fn is_bottom(&self) -> bool;
}
/// Trait for handoffs to implement.
pub trait Handoff: Default + HandoffMeta {
/// Inner datastructure type.
type Inner;
/// Take the inner datastructure, similar to [`std::mem::take`].
fn take_inner(&self) -> Self::Inner;
/// Take the inner datastructure by swapping input and output buffers.
///
/// For better performance over [`Self::take_inner`].
fn borrow_mut_swap(&self) -> RefMut<Self::Inner>;
/// See [`CanReceive::give`].
fn give<T>(&self, item: T) -> T
where
Self: CanReceive<T>,
{
<Self as CanReceive<T>>::give(self, item)
}
/// See [`TryCanReceive::try_give`].
fn try_give<T>(&self, item: T) -> Result<T, T>
where
Self: TryCanReceive<T>,
{
<Self as TryCanReceive<T>>::try_give(self, item)
}
}
/// Wrapper around `IntoIterator` to avoid trait impl conflicts.
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Iter<I>(pub I)
where
I: IntoIterator;
impl<I> IntoIterator for Iter<I>
where
I: IntoIterator,
{
type Item = I::Item;
type IntoIter = I::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}