From b0176d613d522f1863d9a46646106f38850b88a1 Mon Sep 17 00:00:00 2001 From: aon <21188659+aon@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:01:05 -0300 Subject: [PATCH] Remove clone restriction from injectable --- crates/teloxide/src/dispatching/tracing.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/teloxide/src/dispatching/tracing.rs b/crates/teloxide/src/dispatching/tracing.rs index c55fe9bd..7e542a10 100644 --- a/crates/teloxide/src/dispatching/tracing.rs +++ b/crates/teloxide/src/dispatching/tracing.rs @@ -5,34 +5,41 @@ use dptree::{ prelude::DependencyMap, HandlerDescription, }; +use std::sync::Arc; use tracing::{Instrument, Span}; pub trait UpdateHandlerTracingExt { /// Returns an `UpdateHandler` wrapped in an async span. fn instrument_with_async(self, f: F) -> Self where - F: Injectable + Send + Sync + Clone + 'static; + F: Injectable + Send + Sync + 'static; /// Returns an `UpdateHandler` wrapped in a span. fn instrument_with(self, f: F) -> Self where - Asyncify: Injectable + Send + Sync + Clone + 'static; + Asyncify: Injectable + Send + Sync + 'static; } impl UpdateHandlerTracingExt for UpdateHandler { fn instrument_with_async(self, f: F) -> UpdateHandler where - F: Injectable + Send + Sync + Clone + 'static, + F: Injectable + Send + Sync + 'static, { // FIXME: This is a hacky replacement for `handler.description().clone()`. // Ideally cloning `DpHandlerDescription` would be supported by `teloxide`. let description = DpHandlerDescription::entry().merge_chain(self.description()); + let f = Arc::new(f); + dptree::from_fn_with_description(description, move |deps: DependencyMap, cont| { let self_c = self.clone(); - let f_c = f.clone(); + let f = f.clone(); + async move { - let span = f_c.inject(&deps)().await; + let f = f.inject(&deps); + let span = f().await; + drop(f); + self_c.execute(deps, cont).instrument(span).await } }) @@ -40,7 +47,7 @@ impl UpdateHandlerTracingExt for UpdateHandler { fn instrument_with(self, f: F) -> Self where - Asyncify: Injectable + Send + Sync + Clone + 'static, + Asyncify: Injectable + Send + Sync + 'static, { self.instrument_with_async(Asyncify(f)) }