From 877af0080651231b114e0b0ed422d066db5c9b05 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Sat, 4 Mar 2023 16:43:29 +0100 Subject: [PATCH] Simplify setup a bit --- src/setup/core.rs | 22 +++++++++++----------- src/setup/setup.rs | 18 ++++++------------ src/setup/util.rs | 1 - 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/setup/core.rs b/src/setup/core.rs index e9e8a26..4b911a1 100644 --- a/src/setup/core.rs +++ b/src/setup/core.rs @@ -1,6 +1,6 @@ use super::runnable::Runnable; use super::setup::Setup; -use super::util::{AddableResource, InternalAddResult}; +use super::util::{AddResult, AddableResource}; use super::SymbolRunner; use crate::async_utils::join; use crate::resources::{FromArtifact, FromResource}; @@ -17,7 +17,7 @@ use std::rc::Rc; #[async_trait(?Send)] pub trait AddGeneric { - async fn add_generic(&self, logger: &Rc, x: X) -> InternalAddResult; + async fn add_generic(&self, logger: &Rc, x: X) -> AddResult; } macro_rules! add_generic { @@ -35,7 +35,7 @@ macro_rules! add_generic { ),* { #[allow(unused, clippy::shadow_unrelated)] - async fn add_generic(&self, logger: &Rc, ($($name,)*): ($($name,)*)) -> Result<(($($name::Artifact,)*), bool), Box> + async fn add_generic(&self, logger: &Rc, ($($name,)*): ($($name,)*)) -> AddResult<($($name,)*)> { let ($($name,)*) = join!($(self.add(logger, $name, false),)*); let mut did_run_any = false; @@ -72,7 +72,7 @@ where // because the prerequisites are Option, too, and thus this would // require AddGeneric> to already be implemented { - async fn add_generic(&self, logger: &Rc, r: Option) -> InternalAddResult> { + async fn add_generic(&self, logger: &Rc, r: Option) -> AddResult> { Ok(match r { Some(r) => { let (result, did_run) = self.add(logger, r, false).await?; @@ -96,20 +96,20 @@ where T::Artifact: Clone, RegularSetupCore: 'static + SetupCore, { - async fn add_generic(&self, logger: &Rc, r: T) -> InternalAddResult { + async fn add_generic(&self, logger: &Rc, r: T) -> AddResult { self.add(logger, r, false).await } } #[async_trait(?Send)] pub trait SetupCore { - async fn add>( + async fn add( &self, setup: &S, logger: &Rc, - resource: RR, + resource: impl AsRef, force_run: bool, - ) -> InternalAddResult; + ) -> AddResult; } #[derive(Debug)] @@ -135,13 +135,13 @@ where L: ResourceLocator, S: AddGeneric + AddGeneric<>::Prerequisites>, { - async fn add>( + async fn add( &self, setup: &S, logger: &Rc, - resource: RR, + resource: impl AsRef, force_run: bool, - ) -> InternalAddResult { + ) -> AddResult { let resource = resource.as_ref(); let logger = Rc::new(logger.new(o!("resource" => format!("{resource:?}")))); trace!(logger, "(force_run is {})", force_run); diff --git a/src/setup/setup.rs b/src/setup/setup.rs index 7536146..976d17e 100644 --- a/src/setup/setup.rs +++ b/src/setup/setup.rs @@ -1,5 +1,5 @@ use super::core::{RegularSetupCore, SetupCore}; -use super::util::{AddResult, AddableResource, InternalAddResult, Recorder}; +use super::util::{AddResult, AddableResource, Recorder}; use super::SymbolRunner; use crate::async_utils::sleep; use crate::loggers::Logger; @@ -8,7 +8,7 @@ use crate::symbols::Symbol; use crate::{DefaultBuilder, DefaultLocator}; use futures_util::future::{FutureExt, Shared}; use slog::{o, trace, Logger as SlogLogger}; -use std::cell::{RefCell, RefMut}; +use std::cell::RefCell; use std::collections::HashMap; use std::error::Error; use std::fmt::Debug; @@ -40,10 +40,6 @@ impl Setup { impl Setup { - fn borrow_resources(&self) -> RefMut<'_, Cache> { - self.0.resources.borrow_mut() - } - // FIXME: https://github.com/rust-lang/rust-clippy/issues/6353 #[allow(clippy::await_holding_refcell_ref)] pub async fn add( @@ -51,15 +47,14 @@ impl logger: &Rc, resource: R, force_run: bool, - ) -> InternalAddResult + ) -> AddResult where Rs: FromResource, As: FromArtifact + Clone, - R::Artifact: Clone, RegularSetupCore: SetupCore, { let (storable_resource, weak_resource) = Rs::from_resource(resource); - let mut resources = self.borrow_resources(); + let mut resources = self.0.resources.borrow_mut(); let result = if let Some(future) = resources.remove(&storable_resource) { assert!( !force_run, @@ -140,7 +135,6 @@ impl< where Rs: FromResource, As: FromArtifact + Clone, - R::Artifact: Clone, RegularSetupCore: SetupCore>, { let recorder = Recorder::default(); @@ -151,13 +145,13 @@ impl< self.log_result(recorder, result.as_ref().map(|(_, did_run)| *did_run)); result } + pub async fn add(&self, resource: R) -> AddResult where - RegularSetupCore: SetupCore>, Rs: FromResource, As: FromArtifact + Clone, R::Artifact: Clone, - SR: SymbolRunner, + RegularSetupCore: SetupCore>, { self.add_force(resource, false).await } diff --git a/src/setup/util.rs b/src/setup/util.rs index 402adc2..4602f2d 100644 --- a/src/setup/util.rs +++ b/src/setup/util.rs @@ -13,7 +13,6 @@ pub trait AddableResource: 'static + Resource + Debug {} impl AddableResource for R where R: 'static + Resource + Debug {} pub type AddResult = Result<(::Artifact, bool), Box>; -pub type InternalAddResult = Result<(::Artifact, bool), Box>; // From https://users.rust-lang.org/t/how-to-send-a-writer-into-a-thread/4965/10 #[derive(Clone)]