dfir_rs/util/
demux_enum.rs

1//! Trait for the `demux_enum` derive and operator.
2
3use std::task::{Context, Poll};
4
5pub use dfir_macro::DemuxEnum;
6
7/// Trait for use with the `demux_enum` operator.
8///
9/// This trait is meant to be derived: `#[derive(DemuxEnum)]`.
10///
11/// The derive will implement this such that `Outputs` can be any tuple where each item is a
12/// `Sink` that corresponds to each of the variants of the tuple, in alphabetic order.
13#[diagnostic::on_unimplemented(
14    note = "ensure there is exactly one output for each enum variant.",
15    note = "ensure that the type for each output is a tuple of the field for the variant: `()`, `(a,)`, or `(a, b, ...)`."
16)]
17pub trait DemuxEnumSink<Outputs>: DemuxEnumBase {
18    /// The error type for pushing self into the `Outputs` `Sink`s.
19    type Error;
20
21    /// Call `poll_ready` on all `Outputs`.
22    fn poll_ready(outputs: &mut Outputs, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
23
24    /// Pushes `self` into the corresponding output sink in `outputs`.
25    fn start_send(self, outputs: &mut Outputs) -> Result<(), Self::Error>;
26
27    /// Call `poll_flush` on all `Outputs`.
28    fn poll_flush(outputs: &mut Outputs, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
29
30    /// Call `poll_close` on all `Outputs`.
31    fn poll_close(outputs: &mut Outputs, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
32}
33
34/// Special case of [`DemuxEnum`] for when there is only one variant.
35#[diagnostic::on_unimplemented(
36    note = "requires that the enum have only one variant.",
37    note = "ensure there are no missing outputs; there must be exactly one output for each enum variant."
38)]
39pub trait SingleVariant: DemuxEnumBase {
40    /// Output tuple type.
41    type Output;
42    /// Convert self into it's single variant tuple Output.
43    fn single_variant(self) -> Self::Output;
44}
45
46/// Base implementation to constrain that [`DemuxEnum<SOMETHING>`] is implemented.
47#[diagnostic::on_unimplemented(note = "use `#[derive(dfir_rs::DemuxEnum)]`")]
48pub trait DemuxEnumBase {}