mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-08 19:33:53 +01:00
Remove clone restriction from injectable
This commit is contained in:
parent
43be068efd
commit
b0176d613d
1 changed files with 13 additions and 6 deletions
|
@ -5,34 +5,41 @@ use dptree::{
|
||||||
prelude::DependencyMap,
|
prelude::DependencyMap,
|
||||||
HandlerDescription,
|
HandlerDescription,
|
||||||
};
|
};
|
||||||
|
use std::sync::Arc;
|
||||||
use tracing::{Instrument, Span};
|
use tracing::{Instrument, Span};
|
||||||
|
|
||||||
pub trait UpdateHandlerTracingExt<E> {
|
pub trait UpdateHandlerTracingExt<E> {
|
||||||
/// Returns an `UpdateHandler` wrapped in an async span.
|
/// Returns an `UpdateHandler` wrapped in an async span.
|
||||||
fn instrument_with_async<F, FnArgs>(self, f: F) -> Self
|
fn instrument_with_async<F, FnArgs>(self, f: F) -> Self
|
||||||
where
|
where
|
||||||
F: Injectable<DependencyMap, Span, FnArgs> + Send + Sync + Clone + 'static;
|
F: Injectable<DependencyMap, Span, FnArgs> + Send + Sync + 'static;
|
||||||
|
|
||||||
/// Returns an `UpdateHandler` wrapped in a span.
|
/// Returns an `UpdateHandler` wrapped in a span.
|
||||||
fn instrument_with<F, FnArgs>(self, f: F) -> Self
|
fn instrument_with<F, FnArgs>(self, f: F) -> Self
|
||||||
where
|
where
|
||||||
Asyncify<F>: Injectable<DependencyMap, Span, FnArgs> + Send + Sync + Clone + 'static;
|
Asyncify<F>: Injectable<DependencyMap, Span, FnArgs> + Send + Sync + 'static;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: 'static> UpdateHandlerTracingExt<E> for UpdateHandler<E> {
|
impl<E: 'static> UpdateHandlerTracingExt<E> for UpdateHandler<E> {
|
||||||
fn instrument_with_async<F, FnArgs>(self, f: F) -> UpdateHandler<E>
|
fn instrument_with_async<F, FnArgs>(self, f: F) -> UpdateHandler<E>
|
||||||
where
|
where
|
||||||
F: Injectable<DependencyMap, Span, FnArgs> + Send + Sync + Clone + 'static,
|
F: Injectable<DependencyMap, Span, FnArgs> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
// FIXME: This is a hacky replacement for `handler.description().clone()`.
|
// FIXME: This is a hacky replacement for `handler.description().clone()`.
|
||||||
// Ideally cloning `DpHandlerDescription` would be supported by `teloxide`.
|
// Ideally cloning `DpHandlerDescription` would be supported by `teloxide`.
|
||||||
let description = DpHandlerDescription::entry().merge_chain(self.description());
|
let description = DpHandlerDescription::entry().merge_chain(self.description());
|
||||||
|
|
||||||
|
let f = Arc::new(f);
|
||||||
|
|
||||||
dptree::from_fn_with_description(description, move |deps: DependencyMap, cont| {
|
dptree::from_fn_with_description(description, move |deps: DependencyMap, cont| {
|
||||||
let self_c = self.clone();
|
let self_c = self.clone();
|
||||||
let f_c = f.clone();
|
let f = f.clone();
|
||||||
|
|
||||||
async move {
|
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
|
self_c.execute(deps, cont).instrument(span).await
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -40,7 +47,7 @@ impl<E: 'static> UpdateHandlerTracingExt<E> for UpdateHandler<E> {
|
||||||
|
|
||||||
fn instrument_with<F, FnArgs>(self, f: F) -> Self
|
fn instrument_with<F, FnArgs>(self, f: F) -> Self
|
||||||
where
|
where
|
||||||
Asyncify<F>: Injectable<DependencyMap, Span, FnArgs> + Send + Sync + Clone + 'static,
|
Asyncify<F>: Injectable<DependencyMap, Span, FnArgs> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
self.instrument_with_async(Asyncify(f))
|
self.instrument_with_async(Asyncify(f))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue