Untangle and move around setup structs

This commit is contained in:
Adrian Heine 2023-03-07 18:32:04 +01:00
parent 877af00806
commit ac1c06dd31
7 changed files with 594 additions and 613 deletions

View file

@ -1,6 +1,8 @@
use crate::async_utils::join;
use crate::resources::Resource;
use crate::to_artifact::ToArtifact;
use slog::{Drain, Filter, OwnedKVList, Record};
use async_trait::async_trait;
use slog::{Drain, Filter, Logger, OwnedKVList, Record};
use slog_async::AsyncRecord;
use std::cell::RefCell;
use std::error::Error;
@ -14,6 +16,52 @@ impl<R> AddableResource for R where R: 'static + Resource + Debug {}
pub type AddResult<R> = Result<(<R as ToArtifact>::Artifact, bool), Box<dyn Error>>;
#[async_trait(?Send)]
pub trait Add<R: AddableResource> {
async fn add(&self, logger: &Rc<Logger>, resource: Rc<R>, force_run: bool) -> AddResult<R>;
}
#[async_trait(?Send)]
pub trait AddGeneric<X: ToArtifact> {
async fn add_generic(&self, logger: &Rc<Logger>, x: X, force_run: bool) -> AddResult<X>;
}
macro_rules! add_generic {
( $($name:ident)* ) => (
#[async_trait(?Send)]
#[allow(non_snake_case)]
impl<_S, $($name: AddableResource,)*>
AddGeneric<($($name,)*)> for _S
where
$(
_S: AddGeneric<$name>
),*
{
#[allow(unused, clippy::shadow_unrelated)]
async fn add_generic(&self, logger: &Rc<Logger>, ($($name,)*): ($($name,)*), force_run: bool) -> AddResult<($($name,)*)>
{
let ($($name,)*) = join!($(self.add_generic(logger, $name, force_run),)*);
let mut did_run_any = false;
$(
let (artifact, did_run) = $name?;
did_run_any = did_run_any || did_run;
let $name = artifact;
)*
Ok((($($name,)*), did_run_any))
}
}
);
}
for_each_tuple!(add_generic);
#[async_trait(?Send)]
impl<R: AddableResource + Debug, S: Add<R>> AddGeneric<R> for S {
async fn add_generic(&self, logger: &Rc<Logger>, r: R, force_run: bool) -> AddResult<R> {
self.add(logger, Rc::new(r), force_run).await
}
}
// From https://users.rust-lang.org/t/how-to-send-a-writer-into-a-thread/4965/10
#[derive(Clone)]
struct Output<W>(Rc<RefCell<W>>);