From 144032ca545a2073c7b72053ead6d88322a14c6c Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Thu, 2 Jan 2025 10:47:27 +0100 Subject: [PATCH] don't use async-trait for context --- src/bin/cratesfyi.rs | 2 -- src/build_queue.rs | 4 ++-- src/context.rs | 12 +++++------- src/docbuilder/rustwide_builder.rs | 8 ++++---- src/test/mod.rs | 3 +-- src/utils/consistency/mod.rs | 5 +++-- src/utils/daemon.rs | 8 ++++---- src/utils/queue_builder.rs | 4 ++-- src/web/mod.rs | 16 ++++++++-------- 9 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/bin/cratesfyi.rs b/src/bin/cratesfyi.rs index 3e7a1713b..a73e31dd1 100644 --- a/src/bin/cratesfyi.rs +++ b/src/bin/cratesfyi.rs @@ -6,7 +6,6 @@ use std::str::FromStr; use std::sync::Arc; use anyhow::{anyhow, Context as _, Error, Result}; -use axum::async_trait; use clap::{Parser, Subcommand, ValueEnum}; use docs_rs::cdn::CdnBackend; use docs_rs::db::{self, add_path_into_database, CrateId, Overrides, Pool}; @@ -877,7 +876,6 @@ macro_rules! lazy { } } -#[async_trait] impl Context for BinContext { lazy! { fn build_queue(self) -> BuildQueue = { diff --git a/src/build_queue.rs b/src/build_queue.rs index 4d18d03b5..13cf21b95 100644 --- a/src/build_queue.rs +++ b/src/build_queue.rs @@ -608,9 +608,9 @@ impl BuildQueue { /// Builds the top package from the queue. Returns whether there was a package in the queue. /// /// Note that this will return `Ok(true)` even if the package failed to build. - pub(crate) fn build_next_queue_package( + pub(crate) fn build_next_queue_package( &self, - context: &dyn Context, + context: &C, builder: &mut RustwideBuilder, ) -> Result { let mut processed = false; diff --git a/src/context.rs b/src/context.rs index 948415af6..9629790ed 100644 --- a/src/context.rs +++ b/src/context.rs @@ -6,20 +6,18 @@ use crate::{ AsyncBuildQueue, AsyncStorage, BuildQueue, Config, Index, InstanceMetrics, RegistryApi, ServiceMetrics, Storage, }; -use axum::async_trait; -use std::sync::Arc; +use std::{future::Future, sync::Arc}; use tokio::runtime::Runtime; -#[async_trait] pub trait Context { fn config(&self) -> Result>; - async fn async_build_queue(&self) -> Result>; + fn async_build_queue(&self) -> impl Future>> + Send; fn build_queue(&self) -> Result>; fn storage(&self) -> Result>; - async fn async_storage(&self) -> Result>; - async fn cdn(&self) -> Result>; + fn async_storage(&self) -> impl Future>> + Send; + fn cdn(&self) -> impl Future>> + Send; fn pool(&self) -> Result; - async fn async_pool(&self) -> Result; + fn async_pool(&self) -> impl Future> + Send; fn service_metrics(&self) -> Result>; fn instance_metrics(&self) -> Result>; fn index(&self) -> Result>; diff --git a/src/docbuilder/rustwide_builder.rs b/src/docbuilder/rustwide_builder.rs index e4862372d..e75eebee0 100644 --- a/src/docbuilder/rustwide_builder.rs +++ b/src/docbuilder/rustwide_builder.rs @@ -55,7 +55,7 @@ async fn get_configured_toolchain(conn: &mut sqlx::PgConnection) -> Result Result { +fn build_workspace(context: &C) -> Result { let config = context.config()?; let mut builder = WorkspaceBuilder::new(&config.rustwide_workspace, USER_AGENT) @@ -99,7 +99,7 @@ pub struct RustwideBuilder { } impl RustwideBuilder { - pub fn init(context: &dyn Context) -> Result { + pub fn init(context: &C) -> Result { let config = context.config()?; let pool = context.pool()?; let runtime = context.runtime()?; @@ -123,9 +123,9 @@ impl RustwideBuilder { }) } - pub fn reinitialize_workspace_if_interval_passed( + pub fn reinitialize_workspace_if_interval_passed( &mut self, - context: &dyn Context, + context: &C, ) -> Result<()> { let interval = context.config()?.build_workspace_reinitialization_interval; if self.workspace_initialize_time.elapsed() >= interval { diff --git a/src/test/mod.rs b/src/test/mod.rs index 937366a8b..5b8939e5b 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -13,7 +13,7 @@ use crate::{ }; use anyhow::Context as _; use axum::body::Bytes; -use axum::{async_trait, body::Body, http::Request, response::Response as AxumResponse, Router}; +use axum::{body::Body, http::Request, response::Response as AxumResponse, Router}; use fn_error_context::context; use futures_util::{stream::TryStreamExt, FutureExt}; use http_body_util::BodyExt; // for `collect` @@ -621,7 +621,6 @@ impl TestEnvironment { } } -#[async_trait] impl Context for TestEnvironment { fn config(&self) -> Result> { Ok(TestEnvironment::config(self)) diff --git a/src/utils/consistency/mod.rs b/src/utils/consistency/mod.rs index 389b08ee9..af90bab58 100644 --- a/src/utils/consistency/mod.rs +++ b/src/utils/consistency/mod.rs @@ -24,7 +24,7 @@ const BUILD_PRIORITY: i32 = 15; /// /// Even when activities fail, the command can just be re-run. While the diff calculation will /// be repeated, we won't re-execute fixing activities. -pub fn run_check(ctx: &dyn Context, dry_run: bool) -> Result<()> { +pub fn run_check(ctx: &C, dry_run: bool) -> Result<()> { let index = ctx.index()?; info!("Loading data from database..."); @@ -79,9 +79,10 @@ struct HandleResult { yanks_corrected: u32, } -fn handle_diff<'a, I>(ctx: &dyn Context, iter: I, dry_run: bool) -> Result +fn handle_diff<'a, I, C>(ctx: &C, iter: I, dry_run: bool) -> Result where I: Iterator, + C: Context, { let mut result = HandleResult::default(); diff --git a/src/utils/daemon.rs b/src/utils/daemon.rs index dcc762194..f0b00b2c5 100644 --- a/src/utils/daemon.rs +++ b/src/utils/daemon.rs @@ -53,7 +53,7 @@ pub async fn watch_registry( } } -fn start_registry_watcher(context: &dyn Context) -> Result<(), Error> { +fn start_registry_watcher(context: &C) -> Result<(), Error> { let build_queue = context.runtime()?.block_on(context.async_build_queue())?; let config = context.config()?; let index = context.index()?; @@ -69,7 +69,7 @@ fn start_registry_watcher(context: &dyn Context) -> Result<(), Error> { Ok(()) } -pub fn start_background_repository_stats_updater(context: &dyn Context) -> Result<(), Error> { +pub fn start_background_repository_stats_updater(context: &C) -> Result<(), Error> { // This call will still skip github repositories updates and continue if no token is provided // (gitlab doesn't require to have a token). The only time this can return an error is when // creating a pool or if config fails, which shouldn't happen here because this is run right at @@ -91,7 +91,7 @@ pub fn start_background_repository_stats_updater(context: &dyn Context) -> Resul Ok(()) } -pub fn start_background_queue_rebuild(context: &dyn Context) -> Result<(), Error> { +pub fn start_background_queue_rebuild(context: &C) -> Result<(), Error> { let runtime = context.runtime()?; let pool = context.pool()?; let config = context.config()?; @@ -120,7 +120,7 @@ pub fn start_background_queue_rebuild(context: &dyn Context) -> Result<(), Error Ok(()) } -pub fn start_background_cdn_invalidator(context: &dyn Context) -> Result<(), Error> { +pub fn start_background_cdn_invalidator(context: &C) -> Result<(), Error> { let metrics = context.instance_metrics()?; let config = context.config()?; let pool = context.pool()?; diff --git a/src/utils/queue_builder.rs b/src/utils/queue_builder.rs index 7b6cfbfdf..0cca02a8d 100644 --- a/src/utils/queue_builder.rs +++ b/src/utils/queue_builder.rs @@ -7,8 +7,8 @@ use std::time::Duration; use std::{fs, io, path::Path, thread}; use tracing::{debug, error, warn}; -pub fn queue_builder( - context: &dyn Context, +pub fn queue_builder( + context: &C, mut builder: RustwideBuilder, build_queue: Arc, config: Arc, diff --git a/src/web/mod.rs b/src/web/mod.rs index 914bada88..702513939 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -399,9 +399,9 @@ async fn set_sentry_transaction_name_from_axum_route( next.run(request).await } -async fn apply_middleware( +async fn apply_middleware( router: AxumRouter, - context: &dyn Context, + context: &C, template_data: Option>, ) -> Result { let config = context.config()?; @@ -441,20 +441,20 @@ async fn apply_middleware( )) } -pub(crate) async fn build_axum_app( - context: &dyn Context, +pub(crate) async fn build_axum_app( + context: &C, template_data: Arc, ) -> Result { apply_middleware(routes::build_axum_routes(), context, Some(template_data)).await } -pub(crate) async fn build_metrics_axum_app(context: &dyn Context) -> Result { +pub(crate) async fn build_metrics_axum_app(context: &C) -> Result { apply_middleware(routes::build_metric_routes(), context, None).await } -pub fn start_background_metrics_webserver( +pub fn start_background_metrics_webserver( addr: Option, - context: &dyn Context, + context: &C, ) -> Result<(), Error> { let axum_addr: SocketAddr = addr.unwrap_or(DEFAULT_BIND); @@ -492,7 +492,7 @@ pub fn start_background_metrics_webserver( } #[instrument(skip_all)] -pub fn start_web_server(addr: Option, context: &dyn Context) -> Result<(), Error> { +pub fn start_web_server(addr: Option, context: &C) -> Result<(), Error> { let template_data = Arc::new(TemplateData::new(context.config()?.render_threads)?); let axum_addr = addr.unwrap_or(DEFAULT_BIND);