diff options
author | pennae <github@quasiparticle.net> | 2022-07-14 07:15:54 +0200 |
---|---|---|
committer | pennae <github@quasiparticle.net> | 2022-07-14 07:15:54 +0200 |
commit | 566d6ccee21fc52943cf1862222f2e9a8927403c (patch) | |
tree | 50731ec8d0fb45e41921fedfc114a37b9b436bac /src | |
parent | 3986c4f2c75f0e05f7e99a87526777eaa8a53a6a (diff) | |
download | minor-skulk-566d6ccee21fc52943cf1862222f2e9a8927403c.tar.gz minor-skulk-566d6ccee21fc52943cf1862222f2e9a8927403c.tar.xz minor-skulk-566d6ccee21fc52943cf1862222f2e9a8927403c.zip |
simplify spawn_logged
Diffstat (limited to 'src')
-rw-r--r-- | src/utils.rs | 30 |
1 files 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<String>; - fn not_run() -> Self; -} - -impl CanBeLogged for Result<(), anyhow::Error> { - fn into_message(self) -> Option<String> { - self.err().map(|e| e.to_string()) - } - fn not_run() -> Self { - Ok(()) - } -} - -pub fn spawn_logged<T>(context: &'static str, future: T) -> JoinHandle<()> +pub fn spawn_logged<F>(context: &'static str, future: F) -> JoinHandle<()> where - T: Future + Send + 'static, - T::Output: CanBeLogged + Send + 'static, + F: Future<Output = anyhow::Result<()>> + 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<T>(&self, context: &'static str, future: T) + pub fn spawn_after_success<F>(&self, context: &'static str, future: F) where - T: Future + Send + 'static, - T::Output: CanBeLogged + Send + 'static, + F: Future<Output = anyhow::Result<()>> + 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(()), } }); } |