Don't use channels to send dependencies
This commit is contained in:
1 parent
8e507d76f3
commit
df39dd80b9
14 files changed
+223
-311
No files matched your search
+16
-35
@@ -335,7 +335,7 @@ pub fn task(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
quote! {
|
||||
fn extract_and_run(
|
||||
self,
|
||||
_receivers: Vec<Box<dyn std::any::Any + Send>>,
|
||||
_dependencies: Vec<std::sync::Arc<dyn std::any::Any + Send + Sync>>,
|
||||
) -> impl std::future::Future<Output = Result<Self::Output, String>> + Send {
|
||||
async move {
|
||||
let input = ();
|
||||
@@ -350,24 +350,20 @@ pub fn task(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
quote! {
|
||||
fn extract_and_run(
|
||||
self,
|
||||
mut receivers: Vec<Box<dyn std::any::Any + Send>>,
|
||||
mut dependencies: Vec<std::sync::Arc<dyn std::any::Any + Send + Sync>>,
|
||||
) -> impl std::future::Future<Output = Result<Self::Output, String>> + Send {
|
||||
async move {
|
||||
use futures::channel::oneshot;
|
||||
use std::sync::Arc;
|
||||
|
||||
if receivers.len() != 1 {
|
||||
return Err(format!("Expected 1 dependency, got {}", receivers.len()));
|
||||
if dependencies.len() != 1 {
|
||||
return Err(format!("Expected 1 dependency, got {}", dependencies.len()));
|
||||
}
|
||||
|
||||
let rx = *receivers.pop()
|
||||
let arc_value = dependencies.pop()
|
||||
.unwrap()
|
||||
.downcast::<oneshot::Receiver<Arc<#param_type>>>()
|
||||
.downcast::<#param_type>()
|
||||
.map_err(|_| format!("Type mismatch: expected Arc<{}>", std::any::type_name::<#param_type>()))?;
|
||||
|
||||
let arc_value = rx.await
|
||||
.map_err(|_| "Channel closed before receiving value".to_string())?;
|
||||
|
||||
let input = (*arc_value).clone();
|
||||
Ok(self.run(input).await)
|
||||
}
|
||||
@@ -380,51 +376,36 @@ pub fn task(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let param_types: Vec<_> = params.iter().map(|(_, ty)| ty).collect();
|
||||
let indices: Vec<_> = (0..param_count).collect();
|
||||
|
||||
// Generate unique receiver variable names
|
||||
let rx_vars: Vec<_> = (0..param_count)
|
||||
.map(|i| syn::Ident::new(&format!("rx_{}", i), proc_macro2::Span::call_site()))
|
||||
.collect();
|
||||
|
||||
// Create syn::Index for tuple field access (avoids the suffix warning)
|
||||
let syn_indices: Vec<_> = (0..param_count).map(syn::Index::from).collect();
|
||||
|
||||
quote! {
|
||||
fn extract_and_run(
|
||||
self,
|
||||
receivers: Vec<Box<dyn std::any::Any + Send>>,
|
||||
dependencies: Vec<std::sync::Arc<dyn std::any::Any + Send + Sync>>,
|
||||
) -> impl std::future::Future<Output = Result<Self::Output, String>> + Send {
|
||||
async move {
|
||||
use futures::channel::oneshot;
|
||||
use std::sync::Arc;
|
||||
|
||||
let expected_count = #param_count;
|
||||
if receivers.len() != expected_count {
|
||||
return Err(format!("Expected {} dependencies, got {}", expected_count, receivers.len()));
|
||||
if dependencies.len() != expected_count {
|
||||
return Err(format!("Expected {} dependencies, got {}", expected_count, dependencies.len()));
|
||||
}
|
||||
|
||||
let mut iter = receivers.into_iter();
|
||||
let mut iter = dependencies.into_iter();
|
||||
|
||||
// Extract each receiver
|
||||
#(
|
||||
let #rx_vars = *iter.next()
|
||||
let arc_results = (#(
|
||||
iter.next()
|
||||
.ok_or_else(|| format!("Missing receiver at index {}", #indices))?
|
||||
.downcast::<oneshot::Receiver<Arc<#param_types>>>()
|
||||
.downcast::<#param_types>()
|
||||
.map_err(|_| format!("Type mismatch at index {}: expected Arc<{}>",
|
||||
#indices, std::any::type_name::<#param_types>()))?;
|
||||
)*
|
||||
|
||||
// Await all channels concurrently
|
||||
let arc_results = futures::join!(
|
||||
#(
|
||||
async move {
|
||||
#rx_vars.await.map_err(|_| format!("Channel {} closed", #indices))
|
||||
}
|
||||
),*
|
||||
);
|
||||
#indices, std::any::type_name::<#param_types>()))?,
|
||||
)*);
|
||||
|
||||
// Clone inner values and build tuple
|
||||
let input = (#(
|
||||
(*arc_results.#syn_indices?).clone()
|
||||
(*arc_results.#syn_indices).clone()
|
||||
),*);
|
||||
|
||||
Ok(self.run(input).await)
|
||||
|
||||
Reference in new issue
Block a user