forked from hydro-project/hydroflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1c7f90
commit a34546a
Showing
12 changed files
with
129 additions
and
370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,22 @@ | ||
//! Traits for the `demux_enum` derive and operator. | ||
//! Trait for the `demux_enum` derive and operator. | ||
|
||
pub use hydroflow_macro::DemuxEnum; | ||
use pusherator::demux::PusheratorList; | ||
use pusherator::Pusherator; | ||
use variadics::Variadic; | ||
|
||
/// Trait for use with the `demux_enum` operator. | ||
/// | ||
/// This trait is meant to be derived: `#[derive(DemuEnum)]`. | ||
pub trait DemuxEnum<Nexts>: DemuxEnumItems | ||
where | ||
Nexts: PusheratorListForItems<Self::Items>, | ||
{ | ||
/// Pushes self into the corresponding output pusherator. | ||
fn demux_enum(self, outputs: &mut Nexts); | ||
} | ||
|
||
/// Fixed output item list for [`DemuxEnum`]. | ||
/// This trait is meant to be derived: `#[derive(DemuxEnum)]`. | ||
/// | ||
/// This trait is meant to be derived: `#[derive(DemuEnum)]`. | ||
pub trait DemuxEnumItems { | ||
/// A `var_type!(...)` list of items corresponding to each variant's output type. | ||
type Items: Variadic; | ||
/// The derive will implement this such that `Outputs` can be any tuple where each item is a | ||
/// `Pusherator` that corresponds to each of the variants of the tuple, in alphabetic order. | ||
#[diagnostic::on_unimplemented( | ||
note = "Ensure there is exactly one output for each enum variant.", | ||
note = "Ensure that the type for each output is a tuple of the field for the variant: `()`, `(a,)`, or `(a, b, ...)`." | ||
)] | ||
pub trait DemuxEnum<Outputs>: DemuxEnumBase { | ||
/// Pushes self into the corresponding output pusherator in `outputs`. | ||
fn demux_enum(self, outputs: &mut Outputs); | ||
} | ||
|
||
/// Helper trait to bound a [`PusheratorList`] variadic to some coresponding item list variadic. | ||
/// | ||
/// A pusherator list `var_type!(PushA, PushB, PushC)` implements `PusheratorListForItems<var_type!(ItemA, ItemB, ItemC)>`, | ||
/// where `PushA: Pusherator<Item = ItemA>`, etc. | ||
pub trait PusheratorListForItems<Items>: PusheratorList | ||
where | ||
Items: Variadic, | ||
{ | ||
} | ||
impl<HeadPush, RestPush, Head, Rest> PusheratorListForItems<(Head, Rest)> for (HeadPush, RestPush) | ||
where | ||
HeadPush: Pusherator<Item = Head>, | ||
RestPush: PusheratorListForItems<Rest>, | ||
Rest: Variadic, | ||
{ | ||
} | ||
impl PusheratorListForItems<()> for () {} | ||
/// Base implementation to constrain that `DemuxEnum<SOMETHING>` is implemented. | ||
#[diagnostic::on_unimplemented(note = "Use `#[derive(hydroflow::DemuxEnum)]`")] | ||
pub trait DemuxEnumBase {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 16 additions & 7 deletions
23
hydroflow/tests/compile-fail/surface_demuxenum_port_missing.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
error[E0308]: mismatched types | ||
--> tests/compile-fail/surface_demuxenum_port_missing.rs:12:18 | ||
--> tests/compile-fail/surface_demuxenum_port_missing.rs:17:15 | ||
| | ||
12 | let mut df = hydroflow_syntax! { | ||
| __________________^ | ||
| __________________- | ||
13 | | my_demux = source_iter([ | ||
14 | | Shape::Rectangle { w: 10.0, h: 8.0 }, | ||
15 | | Shape::Square(9.0), | ||
... | | ||
16 | | Shape::Circle { r: 5.0 }, | ||
17 | | ]) -> demux_enum::<Shape>(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^ expected a tuple with 3 elements, found one with 2 elements | ||
18 | | my_demux[Rectangle] -> for_each(std::mem::drop); | ||
| | ------------------------ one of the found opaque types | ||
19 | | my_demux[Circle] -> for_each(std::mem::drop); | ||
| | ------------------------ one of the found opaque types | ||
20 | | }; | ||
| |_____^ expected `(_, ())`, found `()` | ||
| |_____- arguments to this function are incorrect | ||
| | ||
= note: expected tuple `(_, ())` | ||
found unit type `()` | ||
= note: this error originates in the macro `$crate::var_expr` which comes from the expansion of the macro `hydroflow_syntax` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
= note: expected mutable reference `&mut (_, _, _)` | ||
found mutable reference `&mut (impl Pusherator<Item = _>, impl Pusherator<Item = _>)` | ||
note: method defined here | ||
--> src/util/demux_enum.rs | ||
| | ||
| fn demux_enum(self, outputs: &mut Outputs); | ||
| ^^^^^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.