From db9afa0b06355028eafe3bc29fe0b2429ba8fd0a Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 29 Mar 2026 00:52:16 +0800 Subject: Completed the first preliminary usable version of the Mingling framework. --- mingling/src/asset/dispatcher.rs | 192 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 184 insertions(+), 8 deletions(-) (limited to 'mingling/src/asset') diff --git a/mingling/src/asset/dispatcher.rs b/mingling/src/asset/dispatcher.rs index 31623d3..09bd386 100644 --- a/mingling/src/asset/dispatcher.rs +++ b/mingling/src/asset/dispatcher.rs @@ -1,19 +1,195 @@ use crate::{ChainProcess, Program, asset::node::Node}; -pub use mingling_macros::Dispatcher; - pub trait Dispatcher { fn node(&self) -> Node; + fn begin(&self, args: Vec) -> ChainProcess; + fn clone_dispatcher(&self) -> Box; } -pub trait DispatcherChain { - fn begin(&self) -> ChainProcess; +impl Clone for Box { + 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) { - let dispatcher = Box::new(dispatcher); - self.dispatcher.push(dispatcher); + pub fn with_dispatcher(&mut self, dispatcher: D) + where + D: Into, + { + let dispatchers = dispatcher.into().dispatcher; + self.dispatcher.extend(dispatchers); + } +} + +pub struct Dispatchers { + dispatcher: Vec>, +} + +impl From for Dispatchers +where + D: Dispatcher + 'static, +{ + fn from(dispatcher: D) -> Self { + Self { + dispatcher: vec![Box::new(dispatcher)], + } + } +} + +impl From>> for Dispatchers { + fn from(dispatcher: Vec>) -> Self { + Self { dispatcher } + } +} + +impl From> for Dispatchers { + fn from(dispatcher: Box) -> Self { + Self { + dispatcher: vec![dispatcher], + } + } +} + +impl From<(D,)> for Dispatchers +where + D: Dispatcher + 'static, +{ + fn from(dispatcher: (D,)) -> Self { + Self { + dispatcher: vec![Box::new(dispatcher.0)], + } + } +} + +impl From<(D1, D2)> for Dispatchers +where + D1: Dispatcher + 'static, + D2: Dispatcher + 'static, +{ + fn from(dispatchers: (D1, D2)) -> Self { + Self { + dispatcher: vec![Box::new(dispatchers.0), Box::new(dispatchers.1)], + } + } +} + +impl From<(D1, D2, D3)> for Dispatchers +where + D1: Dispatcher + 'static, + D2: Dispatcher + 'static, + D3: Dispatcher + 'static, +{ + fn from(dispatchers: (D1, D2, D3)) -> Self { + Self { + dispatcher: vec![ + Box::new(dispatchers.0), + Box::new(dispatchers.1), + Box::new(dispatchers.2), + ], + } + } +} + +impl From<(D1, D2, D3, D4)> for Dispatchers +where + D1: Dispatcher + 'static, + D2: Dispatcher + 'static, + D3: Dispatcher + 'static, + D4: Dispatcher + 'static, +{ + fn from(dispatchers: (D1, D2, D3, D4)) -> Self { + Self { + dispatcher: vec![ + Box::new(dispatchers.0), + Box::new(dispatchers.1), + Box::new(dispatchers.2), + Box::new(dispatchers.3), + ], + } + } +} + +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, +{ + fn from(dispatchers: (D1, D2, D3, D4, D5)) -> Self { + Self { + dispatcher: vec![ + Box::new(dispatchers.0), + Box::new(dispatchers.1), + Box::new(dispatchers.2), + Box::new(dispatchers.3), + Box::new(dispatchers.4), + ], + } + } +} + +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, +{ + fn from(dispatchers: (D1, D2, D3, D4, D5, D6)) -> Self { + Self { + dispatcher: vec![ + Box::new(dispatchers.0), + Box::new(dispatchers.1), + Box::new(dispatchers.2), + Box::new(dispatchers.3), + Box::new(dispatchers.4), + Box::new(dispatchers.5), + ], + } + } +} + +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, +{ + fn from(dispatchers: (D1, D2, D3, D4, D5, D6, D7)) -> Self { + Self { + dispatcher: vec![ + Box::new(dispatchers.0), + Box::new(dispatchers.1), + Box::new(dispatchers.2), + Box::new(dispatchers.3), + Box::new(dispatchers.4), + Box::new(dispatchers.5), + Box::new(dispatchers.6), + ], + } + } +} + +impl std::ops::Deref for Dispatchers { + type Target = Vec>; + + fn deref(&self) -> &Self::Target { + &self.dispatcher + } +} + +impl Into>> for Dispatchers { + fn into(self) -> Vec> { + self.dispatcher } } -- cgit