diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-01 15:48:41 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-01 15:48:41 +0800 |
| commit | 3de10ca22cca06c4d9069984d0e66e370a331dde (patch) | |
| tree | 7e8a9b035c360c016cde848b3442d3e1d5dcac5e /mingling_core/src/asset | |
| parent | f3d6f76dfd07c35dabc11aa86d86c3671cd283c5 (diff) | |
Replace typeid-based dispatch with enum-based dispatch
- Add `Groupped` trait and `member_id` to `AnyOutput`
- Add generic parameter `G` to `Dispatcher`, `Chain`, `Program` etc
- Remove `hint` module and its marker types
- Update macros to support explicit group specification
- Add `gen_program` macro for generating enum-based programs
- Add `GroupProcess` marker type for type-level grouping
Diffstat (limited to 'mingling_core/src/asset')
| -rw-r--r-- | mingling_core/src/asset/chain.rs | 9 | ||||
| -rw-r--r-- | mingling_core/src/asset/chain/error.rs | 5 | ||||
| -rw-r--r-- | mingling_core/src/asset/dispatcher.rs | 140 |
3 files changed, 83 insertions, 71 deletions
diff --git a/mingling_core/src/asset/chain.rs b/mingling_core/src/asset/chain.rs index 1ea1125..9f62228 100644 --- a/mingling_core/src/asset/chain.rs +++ b/mingling_core/src/asset/chain.rs @@ -1,8 +1,13 @@ +use std::fmt::Display; + use crate::ChainProcess; pub mod error; -pub trait Chain { +pub trait Chain<G> +where + G: Display, +{ type Previous; - fn proc(p: Self::Previous) -> impl Future<Output = ChainProcess> + Send; + fn proc(p: Self::Previous) -> impl Future<Output = ChainProcess<G>> + Send; } diff --git a/mingling_core/src/asset/chain/error.rs b/mingling_core/src/asset/chain/error.rs index d4da4ac..cc22bdc 100644 --- a/mingling_core/src/asset/chain/error.rs +++ b/mingling_core/src/asset/chain/error.rs @@ -1,5 +1,3 @@ -use crate::AnyOutput; - #[derive(thiserror::Error, Debug)] pub enum ChainProcessError { #[error("Other error: {0}")] @@ -7,7 +5,4 @@ pub enum ChainProcessError { #[error("IO error: {0}")] IO(#[from] std::io::Error), - - #[error("Broken chain")] - Broken(AnyOutput), } diff --git a/mingling_core/src/asset/dispatcher.rs b/mingling_core/src/asset/dispatcher.rs index 13e35f7..0863f16 100644 --- a/mingling_core/src/asset/dispatcher.rs +++ b/mingling_core/src/asset/dispatcher.rs @@ -1,60 +1,66 @@ +use std::fmt::Display; + use crate::{ChainProcess, Program, asset::node::Node}; -pub trait Dispatcher { +pub trait Dispatcher<G> +where + G: Display, +{ fn node(&self) -> Node; - fn begin(&self, args: Vec<String>) -> ChainProcess; - fn clone_dispatcher(&self) -> Box<dyn Dispatcher>; + fn begin(&self, args: Vec<String>) -> ChainProcess<G>; + fn clone_dispatcher(&self) -> Box<dyn Dispatcher<G>>; } -impl Clone for Box<dyn Dispatcher> { +impl<G> Clone for Box<dyn Dispatcher<G>> +where + G: Display, +{ fn clone(&self) -> Self { self.clone_dispatcher() } } -impl<C: crate::program::ProgramCollect> Program<C> { +impl<C: crate::program::ProgramCollect, G: Display> Program<C, G> { /// Adds a dispatcher to the program. - pub fn with_dispatcher<D>(&mut self, dispatcher: D) + pub fn with_dispatcher<Disp>(&mut self, dispatcher: Disp) where - D: Into<Dispatchers>, + Disp: Dispatcher<G> + 'static, { - let dispatchers = dispatcher.into().dispatcher; - self.dispatcher.extend(dispatchers); + self.dispatcher.push(Box::new(dispatcher)); } -} -pub struct Dispatchers { - dispatcher: Vec<Box<dyn Dispatcher + 'static>>, + /// Add some dispatchers to the program. + pub fn with_dispatchers<D>(&mut self, dispatchers: D) + where + D: Into<Dispatchers<G>>, + { + let dispatchers = dispatchers.into(); + self.dispatcher.extend(dispatchers.dispatcher); + } } -impl<D> From<D> for Dispatchers -where - D: Dispatcher + 'static, -{ - fn from(dispatcher: D) -> Self { - Self { - dispatcher: vec![Box::new(dispatcher)], - } - } +pub struct Dispatchers<G> { + dispatcher: Vec<Box<dyn Dispatcher<G> + 'static>>, } -impl From<Vec<Box<dyn Dispatcher>>> for Dispatchers { - fn from(dispatcher: Vec<Box<dyn Dispatcher>>) -> Self { +impl<G> From<Vec<Box<dyn Dispatcher<G>>>> for Dispatchers<G> { + fn from(dispatcher: Vec<Box<dyn Dispatcher<G>>>) -> Self { Self { dispatcher } } } -impl From<Box<dyn Dispatcher>> for Dispatchers { - fn from(dispatcher: Box<dyn Dispatcher>) -> Self { +impl<G> From<Box<dyn Dispatcher<G>>> for Dispatchers<G> { + fn from(dispatcher: Box<dyn Dispatcher<G>>) -> Self { Self { dispatcher: vec![dispatcher], } } } -impl<D> From<(D,)> for Dispatchers +impl<D, G> From<(D,)> for Dispatchers<G> where - D: Dispatcher + 'static, + D: Dispatcher<G> + 'static, + G: Display, { fn from(dispatcher: (D,)) -> Self { Self { @@ -63,10 +69,11 @@ where } } -impl<D1, D2> From<(D1, D2)> for Dispatchers +impl<D1, D2, G> From<(D1, D2)> for Dispatchers<G> where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, + D1: Dispatcher<G> + 'static, + D2: Dispatcher<G> + 'static, + G: Display, { fn from(dispatchers: (D1, D2)) -> Self { Self { @@ -75,11 +82,12 @@ where } } -impl<D1, D2, D3> From<(D1, D2, D3)> for Dispatchers +impl<D1, D2, D3, G> From<(D1, D2, D3)> for Dispatchers<G> where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, + D1: Dispatcher<G> + 'static, + D2: Dispatcher<G> + 'static, + D3: Dispatcher<G> + 'static, + G: Display, { fn from(dispatchers: (D1, D2, D3)) -> Self { Self { @@ -92,12 +100,13 @@ where } } -impl<D1, D2, D3, D4> From<(D1, D2, D3, D4)> for Dispatchers +impl<D1, D2, D3, D4, G> From<(D1, D2, D3, D4)> for Dispatchers<G> where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, - D4: Dispatcher + 'static, + D1: Dispatcher<G> + 'static, + D2: Dispatcher<G> + 'static, + D3: Dispatcher<G> + 'static, + D4: Dispatcher<G> + 'static, + G: Display, { fn from(dispatchers: (D1, D2, D3, D4)) -> Self { Self { @@ -111,13 +120,14 @@ where } } -impl<D1, D2, D3, D4, D5> From<(D1, D2, D3, D4, D5)> for Dispatchers +impl<D1, D2, D3, D4, D5, G> From<(D1, D2, D3, D4, D5)> for Dispatchers<G> where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, - D4: Dispatcher + 'static, - D5: Dispatcher + 'static, + D1: Dispatcher<G> + 'static, + D2: Dispatcher<G> + 'static, + D3: Dispatcher<G> + 'static, + D4: Dispatcher<G> + 'static, + D5: Dispatcher<G> + 'static, + G: Display, { fn from(dispatchers: (D1, D2, D3, D4, D5)) -> Self { Self { @@ -132,14 +142,15 @@ where } } -impl<D1, D2, D3, D4, D5, D6> From<(D1, D2, D3, D4, D5, D6)> for Dispatchers +impl<D1, D2, D3, D4, D5, D6, G> From<(D1, D2, D3, D4, D5, D6)> for Dispatchers<G> where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, - D4: Dispatcher + 'static, - D5: Dispatcher + 'static, - D6: Dispatcher + 'static, + D1: Dispatcher<G> + 'static, + D2: Dispatcher<G> + 'static, + D3: Dispatcher<G> + 'static, + D4: Dispatcher<G> + 'static, + D5: Dispatcher<G> + 'static, + D6: Dispatcher<G> + 'static, + G: Display, { fn from(dispatchers: (D1, D2, D3, D4, D5, D6)) -> Self { Self { @@ -155,15 +166,16 @@ where } } -impl<D1, D2, D3, D4, D5, D6, D7> From<(D1, D2, D3, D4, D5, D6, D7)> for Dispatchers +impl<D1, D2, D3, D4, D5, D6, D7, G> From<(D1, D2, D3, D4, D5, D6, D7)> for Dispatchers<G> where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, - D4: Dispatcher + 'static, - D5: Dispatcher + 'static, - D6: Dispatcher + 'static, - D7: Dispatcher + 'static, + D1: Dispatcher<G> + 'static, + D2: Dispatcher<G> + 'static, + D3: Dispatcher<G> + 'static, + D4: Dispatcher<G> + 'static, + D5: Dispatcher<G> + 'static, + D6: Dispatcher<G> + 'static, + D7: Dispatcher<G> + 'static, + G: Display, { fn from(dispatchers: (D1, D2, D3, D4, D5, D6, D7)) -> Self { Self { @@ -180,16 +192,16 @@ where } } -impl std::ops::Deref for Dispatchers { - type Target = Vec<Box<dyn Dispatcher + 'static>>; +impl<G> std::ops::Deref for Dispatchers<G> { + type Target = Vec<Box<dyn Dispatcher<G> + 'static>>; fn deref(&self) -> &Self::Target { &self.dispatcher } } -impl From<Dispatchers> for Vec<Box<dyn Dispatcher + 'static>> { - fn from(val: Dispatchers) -> Self { +impl<G> From<Dispatchers<G>> for Vec<Box<dyn Dispatcher<G> + 'static>> { + fn from(val: Dispatchers<G>) -> Self { val.dispatcher } } |
