From 3de10ca22cca06c4d9069984d0e66e370a331dde Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 1 Apr 2026 15:48:41 +0800 Subject: 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 --- mingling_core/src/asset/chain.rs | 9 ++- mingling_core/src/asset/chain/error.rs | 5 -- mingling_core/src/asset/dispatcher.rs | 140 ++++++++++++++++++--------------- 3 files changed, 83 insertions(+), 71 deletions(-) (limited to 'mingling_core/src/asset') 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 +where + G: Display, +{ type Previous; - fn proc(p: Self::Previous) -> impl Future + Send; + fn proc(p: Self::Previous) -> impl Future> + 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 +where + G: Display, +{ fn node(&self) -> Node; - fn begin(&self, args: Vec) -> ChainProcess; - fn clone_dispatcher(&self) -> Box; + fn begin(&self, args: Vec) -> ChainProcess; + fn clone_dispatcher(&self) -> Box>; } -impl Clone for Box { +impl Clone for Box> +where + G: Display, +{ fn clone(&self) -> Self { self.clone_dispatcher() } } -impl Program { +impl Program { /// Adds a dispatcher to the program. - pub fn with_dispatcher(&mut self, dispatcher: D) + pub fn with_dispatcher(&mut self, dispatcher: Disp) where - D: Into, + Disp: Dispatcher + 'static, { - let dispatchers = dispatcher.into().dispatcher; - self.dispatcher.extend(dispatchers); + self.dispatcher.push(Box::new(dispatcher)); } -} -pub struct Dispatchers { - dispatcher: Vec>, + /// Add some dispatchers to the program. + pub fn with_dispatchers(&mut self, dispatchers: D) + where + D: Into>, + { + let dispatchers = dispatchers.into(); + self.dispatcher.extend(dispatchers.dispatcher); + } } -impl From for Dispatchers -where - D: Dispatcher + 'static, -{ - fn from(dispatcher: D) -> Self { - Self { - dispatcher: vec![Box::new(dispatcher)], - } - } +pub struct Dispatchers { + dispatcher: Vec + 'static>>, } -impl From>> for Dispatchers { - fn from(dispatcher: Vec>) -> Self { +impl From>>> for Dispatchers { + fn from(dispatcher: Vec>>) -> Self { Self { dispatcher } } } -impl From> for Dispatchers { - fn from(dispatcher: Box) -> Self { +impl From>> for Dispatchers { + fn from(dispatcher: Box>) -> Self { Self { dispatcher: vec![dispatcher], } } } -impl From<(D,)> for Dispatchers +impl From<(D,)> for Dispatchers where - D: Dispatcher + 'static, + D: Dispatcher + 'static, + G: Display, { fn from(dispatcher: (D,)) -> Self { Self { @@ -63,10 +69,11 @@ where } } -impl From<(D1, D2)> for Dispatchers +impl From<(D1, D2)> for Dispatchers where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, + D1: Dispatcher + 'static, + D2: Dispatcher + 'static, + G: Display, { fn from(dispatchers: (D1, D2)) -> Self { Self { @@ -75,11 +82,12 @@ where } } -impl From<(D1, D2, D3)> for Dispatchers +impl From<(D1, D2, D3)> for Dispatchers where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, + D1: Dispatcher + 'static, + D2: Dispatcher + 'static, + D3: Dispatcher + 'static, + G: Display, { fn from(dispatchers: (D1, D2, D3)) -> Self { Self { @@ -92,12 +100,13 @@ where } } -impl From<(D1, D2, D3, D4)> for Dispatchers +impl From<(D1, D2, D3, D4)> for Dispatchers where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, - D4: Dispatcher + 'static, + D1: Dispatcher + 'static, + D2: Dispatcher + 'static, + D3: Dispatcher + 'static, + D4: Dispatcher + 'static, + G: Display, { fn from(dispatchers: (D1, D2, D3, D4)) -> Self { Self { @@ -111,13 +120,14 @@ where } } -impl From<(D1, D2, D3, D4, D5)> for Dispatchers +impl From<(D1, D2, D3, D4, D5)> for Dispatchers where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, - D4: Dispatcher + 'static, - D5: Dispatcher + 'static, + D1: Dispatcher + 'static, + D2: Dispatcher + 'static, + D3: Dispatcher + 'static, + D4: Dispatcher + 'static, + D5: Dispatcher + 'static, + G: Display, { fn from(dispatchers: (D1, D2, D3, D4, D5)) -> Self { Self { @@ -132,14 +142,15 @@ where } } -impl From<(D1, D2, D3, D4, D5, D6)> for Dispatchers +impl From<(D1, D2, D3, D4, D5, D6)> for Dispatchers where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, - D4: Dispatcher + 'static, - D5: Dispatcher + 'static, - D6: Dispatcher + 'static, + D1: Dispatcher + 'static, + D2: Dispatcher + 'static, + D3: Dispatcher + 'static, + D4: Dispatcher + 'static, + D5: Dispatcher + 'static, + D6: Dispatcher + 'static, + G: Display, { fn from(dispatchers: (D1, D2, D3, D4, D5, D6)) -> Self { Self { @@ -155,15 +166,16 @@ where } } -impl From<(D1, D2, D3, D4, D5, D6, D7)> for Dispatchers +impl From<(D1, D2, D3, D4, D5, D6, D7)> for Dispatchers where - D1: Dispatcher + 'static, - D2: Dispatcher + 'static, - D3: Dispatcher + 'static, - D4: Dispatcher + 'static, - D5: Dispatcher + 'static, - D6: Dispatcher + 'static, - D7: Dispatcher + 'static, + D1: Dispatcher + 'static, + D2: Dispatcher + 'static, + D3: Dispatcher + 'static, + D4: Dispatcher + 'static, + D5: Dispatcher + 'static, + D6: Dispatcher + 'static, + D7: Dispatcher + '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>; +impl std::ops::Deref for Dispatchers { + type Target = Vec + 'static>>; fn deref(&self) -> &Self::Target { &self.dispatcher } } -impl From for Vec> { - fn from(val: Dispatchers) -> Self { +impl From> for Vec + 'static>> { + fn from(val: Dispatchers) -> Self { val.dispatcher } } -- cgit