dfir_rs/util/
demux_enum.rs

1//! Trait for the `demux_enum` derive and operator.
2
3pub use dfir_macro::DemuxEnum;
4
5/// Trait for use with the `demux_enum` operator.
6///
7/// This trait is meant to be derived: `#[derive(DemuxEnum)]`.
8///
9/// The derive will implement this such that `Outputs` can be any tuple where each item is a
10/// `Pusherator` that corresponds to each of the variants of the tuple, in alphabetic order.
11#[diagnostic::on_unimplemented(
12    note = "ensure there is exactly one output for each enum variant.",
13    note = "ensure that the type for each output is a tuple of the field for the variant: `()`, `(a,)`, or `(a, b, ...)`."
14)]
15pub trait DemuxEnum<Outputs>: DemuxEnumBase {
16    /// Pushes self into the corresponding output pusherator in `outputs`.
17    fn demux_enum(self, outputs: &mut Outputs);
18}
19
20/// Special case of [`DemuxEnum`] for when there is only one variant.
21#[diagnostic::on_unimplemented(
22    note = "requires that the enum have only one variant.",
23    note = "ensure there are no missing outputs; there must be exactly one output for each enum variant."
24)]
25pub trait SingleVariant: DemuxEnumBase {
26    /// Output tuple type.
27    type Output;
28    /// Convert self into it's single variant tuple Output.
29    fn single_variant(self) -> Self::Output;
30}
31
32/// Base implementation to constrain that [`DemuxEnum<SOMETHING>`] is implemented.
33#[diagnostic::on_unimplemented(note = "use `#[derive(dfir_rs::DemuxEnum)]`")]
34pub trait DemuxEnumBase {}