From 566d6ccee21fc52943cf1862222f2e9a8927403c Mon Sep 17 00:00:00 2001 From: pennae Date: Thu, 14 Jul 2022 07:15:54 +0200 Subject: simplify spawn_logged --- src/utils.rs | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 5a2407a..70c8c11 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -29,28 +29,13 @@ macro_rules! try_outcome_data { }; } -pub trait CanBeLogged: Send + 'static { - fn into_message(self) -> Option; - fn not_run() -> Self; -} - -impl CanBeLogged for Result<(), anyhow::Error> { - fn into_message(self) -> Option { - self.err().map(|e| e.to_string()) - } - fn not_run() -> Self { - Ok(()) - } -} - -pub fn spawn_logged(context: &'static str, future: T) -> JoinHandle<()> +pub fn spawn_logged(context: &'static str, future: F) -> JoinHandle<()> where - T: Future + Send + 'static, - T::Output: CanBeLogged + Send + 'static, + F: Future> + Send + 'static, { tokio::spawn(async move { - if let Some(msg) = future.await.into_message() { - warn!("{context}: {msg}"); + if let Err(e) = future.await { + warn!("{context}: {e:?}"); } }) } @@ -103,10 +88,9 @@ impl<'r> FromRequest<'r> for &'r DeferAction { } impl DeferAction { - pub fn spawn_after_success(&self, context: &'static str, future: T) + pub fn spawn_after_success(&self, context: &'static str, future: F) where - T: Future + Send + 'static, - T::Output: CanBeLogged + Send + 'static, + F: Future> + Send + 'static, { let mut r = self.0.subscribe(); spawn_logged(context, async move { @@ -117,7 +101,7 @@ impl DeferAction { r.recv().await.ok(); future.await }, - Err(_) => CanBeLogged::not_run(), + Err(_) => Ok(()), } }); } -- cgit v1.2.3