Browse Source

Simplify setup a bit

master
Adrian Heine 2 years ago
parent
commit
877af00806
  1. 22
      src/setup/core.rs
  2. 18
      src/setup/setup.rs
  3. 1
      src/setup/util.rs

22
src/setup/core.rs

@ -1,6 +1,6 @@
use super::runnable::Runnable; use super::runnable::Runnable;
use super::setup::Setup; use super::setup::Setup;
use super::util::{AddableResource, InternalAddResult};
use super::util::{AddResult, AddableResource};
use super::SymbolRunner; use super::SymbolRunner;
use crate::async_utils::join; use crate::async_utils::join;
use crate::resources::{FromArtifact, FromResource}; use crate::resources::{FromArtifact, FromResource};
@ -17,7 +17,7 @@ use std::rc::Rc;
#[async_trait(?Send)] #[async_trait(?Send)]
pub trait AddGeneric<X: ToArtifact> { pub trait AddGeneric<X: ToArtifact> {
async fn add_generic(&self, logger: &Rc<Logger>, x: X) -> InternalAddResult<X>;
async fn add_generic(&self, logger: &Rc<Logger>, x: X) -> AddResult<X>;
} }
macro_rules! add_generic { macro_rules! add_generic {
@ -35,7 +35,7 @@ macro_rules! add_generic {
),* ),*
{ {
#[allow(unused, clippy::shadow_unrelated)] #[allow(unused, clippy::shadow_unrelated)]
async fn add_generic(&self, logger: &Rc<Logger>, ($($name,)*): ($($name,)*)) -> Result<(($($name::Artifact,)*), bool), Box<dyn Error>>
async fn add_generic(&self, logger: &Rc<Logger>, ($($name,)*): ($($name,)*)) -> AddResult<($($name,)*)>
{ {
let ($($name,)*) = join!($(self.add(logger, $name, false),)*); let ($($name,)*) = join!($(self.add(logger, $name, false),)*);
let mut did_run_any = false; let mut did_run_any = false;
@ -72,7 +72,7 @@ where
// because the prerequisites are Option<T>, too, and thus this would // because the prerequisites are Option<T>, too, and thus this would
// require AddGeneric<Option<T>> to already be implemented // require AddGeneric<Option<T>> to already be implemented
{ {
async fn add_generic(&self, logger: &Rc<Logger>, r: Option<T>) -> InternalAddResult<Option<T>> {
async fn add_generic(&self, logger: &Rc<Logger>, r: Option<T>) -> AddResult<Option<T>> {
Ok(match r { Ok(match r {
Some(r) => { Some(r) => {
let (result, did_run) = self.add(logger, r, false).await?; let (result, did_run) = self.add(logger, r, false).await?;
@ -96,20 +96,20 @@ where
T::Artifact: Clone, T::Artifact: Clone,
RegularSetupCore<SR, L, B>: 'static + SetupCore<T, Self>, RegularSetupCore<SR, L, B>: 'static + SetupCore<T, Self>,
{ {
async fn add_generic(&self, logger: &Rc<Logger>, r: T) -> InternalAddResult<T> {
async fn add_generic(&self, logger: &Rc<Logger>, r: T) -> AddResult<T> {
self.add(logger, r, false).await self.add(logger, r, false).await
} }
} }
#[async_trait(?Send)] #[async_trait(?Send)]
pub trait SetupCore<R: AddableResource, S> { pub trait SetupCore<R: AddableResource, S> {
async fn add<RR: AsRef<R>>(
async fn add(
&self, &self,
setup: &S, setup: &S,
logger: &Rc<Logger>, logger: &Rc<Logger>,
resource: RR,
resource: impl AsRef<R>,
force_run: bool, force_run: bool,
) -> InternalAddResult<R>;
) -> AddResult<R>;
} }
#[derive(Debug)] #[derive(Debug)]
@ -135,13 +135,13 @@ where
L: ResourceLocator<R>, L: ResourceLocator<R>,
S: AddGeneric<B::Prerequisites> + AddGeneric<<L as ResourceLocator<R>>::Prerequisites>, S: AddGeneric<B::Prerequisites> + AddGeneric<<L as ResourceLocator<R>>::Prerequisites>,
{ {
async fn add<RR: AsRef<R>>(
async fn add(
&self, &self,
setup: &S, setup: &S,
logger: &Rc<Logger>, logger: &Rc<Logger>,
resource: RR,
resource: impl AsRef<R>,
force_run: bool, force_run: bool,
) -> InternalAddResult<R> {
) -> AddResult<R> {
let resource = resource.as_ref(); let resource = resource.as_ref();
let logger = Rc::new(logger.new(o!("resource" => format!("{resource:?}")))); let logger = Rc::new(logger.new(o!("resource" => format!("{resource:?}"))));
trace!(logger, "(force_run is {})", force_run); trace!(logger, "(force_run is {})", force_run);

18
src/setup/setup.rs

@ -1,5 +1,5 @@
use super::core::{RegularSetupCore, SetupCore}; use super::core::{RegularSetupCore, SetupCore};
use super::util::{AddResult, AddableResource, InternalAddResult, Recorder};
use super::util::{AddResult, AddableResource, Recorder};
use super::SymbolRunner; use super::SymbolRunner;
use crate::async_utils::sleep; use crate::async_utils::sleep;
use crate::loggers::Logger; use crate::loggers::Logger;
@ -8,7 +8,7 @@ use crate::symbols::Symbol;
use crate::{DefaultBuilder, DefaultLocator}; use crate::{DefaultBuilder, DefaultLocator};
use futures_util::future::{FutureExt, Shared}; use futures_util::future::{FutureExt, Shared};
use slog::{o, trace, Logger as SlogLogger}; use slog::{o, trace, Logger as SlogLogger};
use std::cell::{RefCell, RefMut};
use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::fmt::Debug; use std::fmt::Debug;
@ -40,10 +40,6 @@ impl<SR, L, B, Rs, As> Setup<SR, L, B, Rs, As> {
impl<L: 'static, B: 'static, SR: 'static, Rs: Hash + Eq + 'static, As: 'static> impl<L: 'static, B: 'static, SR: 'static, Rs: Hash + Eq + 'static, As: 'static>
Setup<SR, L, B, Rs, As> Setup<SR, L, B, Rs, As>
{ {
fn borrow_resources(&self) -> RefMut<'_, Cache<Rs, As>> {
self.0.resources.borrow_mut()
}
// FIXME: https://github.com/rust-lang/rust-clippy/issues/6353 // FIXME: https://github.com/rust-lang/rust-clippy/issues/6353
#[allow(clippy::await_holding_refcell_ref)] #[allow(clippy::await_holding_refcell_ref)]
pub async fn add<R: AddableResource>( pub async fn add<R: AddableResource>(
@ -51,15 +47,14 @@ impl<L: 'static, B: 'static, SR: 'static, Rs: Hash + Eq + 'static, As: 'static>
logger: &Rc<SlogLogger>, logger: &Rc<SlogLogger>,
resource: R, resource: R,
force_run: bool, force_run: bool,
) -> InternalAddResult<R>
) -> AddResult<R>
where where
Rs: FromResource<R>, Rs: FromResource<R>,
As: FromArtifact<R> + Clone, As: FromArtifact<R> + Clone,
R::Artifact: Clone,
RegularSetupCore<SR, L, B>: SetupCore<R, Self>, RegularSetupCore<SR, L, B>: SetupCore<R, Self>,
{ {
let (storable_resource, weak_resource) = Rs::from_resource(resource); 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) { let result = if let Some(future) = resources.remove(&storable_resource) {
assert!( assert!(
!force_run, !force_run,
@ -140,7 +135,6 @@ impl<
where where
Rs: FromResource<R>, Rs: FromResource<R>,
As: FromArtifact<R> + Clone, As: FromArtifact<R> + Clone,
R::Artifact: Clone,
RegularSetupCore<SR, L, B>: SetupCore<R, Setup<SR, L, B, Rs, As>>, RegularSetupCore<SR, L, B>: SetupCore<R, Setup<SR, L, B, Rs, As>>,
{ {
let recorder = Recorder::default(); let recorder = Recorder::default();
@ -151,13 +145,13 @@ impl<
self.log_result(recorder, result.as_ref().map(|(_, did_run)| *did_run)); self.log_result(recorder, result.as_ref().map(|(_, did_run)| *did_run));
result result
} }
pub async fn add<R: AddableResource>(&self, resource: R) -> AddResult<R> pub async fn add<R: AddableResource>(&self, resource: R) -> AddResult<R>
where where
RegularSetupCore<SR, L, B>: SetupCore<R, Setup<SR, L, B, Rs, As>>,
Rs: FromResource<R>, Rs: FromResource<R>,
As: FromArtifact<R> + Clone, As: FromArtifact<R> + Clone,
R::Artifact: Clone, R::Artifact: Clone,
SR: SymbolRunner,
RegularSetupCore<SR, L, B>: SetupCore<R, Setup<SR, L, B, Rs, As>>,
{ {
self.add_force(resource, false).await self.add_force(resource, false).await
} }

1
src/setup/util.rs

@ -13,7 +13,6 @@ pub trait AddableResource: 'static + Resource + Debug {}
impl<R> AddableResource for R where R: 'static + Resource + Debug {} impl<R> AddableResource for R where R: 'static + Resource + Debug {}
pub type AddResult<R> = Result<(<R as ToArtifact>::Artifact, bool), Box<dyn Error>>; pub type AddResult<R> = Result<(<R as ToArtifact>::Artifact, bool), Box<dyn Error>>;
pub type InternalAddResult<R> = Result<(<R as ToArtifact>::Artifact, bool), Box<dyn Error>>;
// From https://users.rust-lang.org/t/how-to-send-a-writer-into-a-thread/4965/10 // From https://users.rust-lang.org/t/how-to-send-a-writer-into-a-thread/4965/10
#[derive(Clone)] #[derive(Clone)]

Loading…
Cancel
Save