Skip to content

Commit 144032c

Browse files
sypharGuillaumeGomez
authored andcommitted
don't use async-trait for context
1 parent 0b3bab3 commit 144032c

File tree

9 files changed

+29
-33
lines changed

9 files changed

+29
-33
lines changed

src/bin/cratesfyi.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::str::FromStr;
66
use std::sync::Arc;
77

88
use anyhow::{anyhow, Context as _, Error, Result};
9-
use axum::async_trait;
109
use clap::{Parser, Subcommand, ValueEnum};
1110
use docs_rs::cdn::CdnBackend;
1211
use docs_rs::db::{self, add_path_into_database, CrateId, Overrides, Pool};
@@ -877,7 +876,6 @@ macro_rules! lazy {
877876
}
878877
}
879878

880-
#[async_trait]
881879
impl Context for BinContext {
882880
lazy! {
883881
fn build_queue(self) -> BuildQueue = {

src/build_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,9 @@ impl BuildQueue {
608608
/// Builds the top package from the queue. Returns whether there was a package in the queue.
609609
///
610610
/// Note that this will return `Ok(true)` even if the package failed to build.
611-
pub(crate) fn build_next_queue_package(
611+
pub(crate) fn build_next_queue_package<C: Context>(
612612
&self,
613-
context: &dyn Context,
613+
context: &C,
614614
builder: &mut RustwideBuilder,
615615
) -> Result<bool> {
616616
let mut processed = false;

src/context.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ use crate::{
66
AsyncBuildQueue, AsyncStorage, BuildQueue, Config, Index, InstanceMetrics, RegistryApi,
77
ServiceMetrics, Storage,
88
};
9-
use axum::async_trait;
10-
use std::sync::Arc;
9+
use std::{future::Future, sync::Arc};
1110
use tokio::runtime::Runtime;
1211

13-
#[async_trait]
1412
pub trait Context {
1513
fn config(&self) -> Result<Arc<Config>>;
16-
async fn async_build_queue(&self) -> Result<Arc<AsyncBuildQueue>>;
14+
fn async_build_queue(&self) -> impl Future<Output = Result<Arc<AsyncBuildQueue>>> + Send;
1715
fn build_queue(&self) -> Result<Arc<BuildQueue>>;
1816
fn storage(&self) -> Result<Arc<Storage>>;
19-
async fn async_storage(&self) -> Result<Arc<AsyncStorage>>;
20-
async fn cdn(&self) -> Result<Arc<CdnBackend>>;
17+
fn async_storage(&self) -> impl Future<Output = Result<Arc<AsyncStorage>>> + Send;
18+
fn cdn(&self) -> impl Future<Output = Result<Arc<CdnBackend>>> + Send;
2119
fn pool(&self) -> Result<Pool>;
22-
async fn async_pool(&self) -> Result<Pool>;
20+
fn async_pool(&self) -> impl Future<Output = Result<Pool>> + Send;
2321
fn service_metrics(&self) -> Result<Arc<ServiceMetrics>>;
2422
fn instance_metrics(&self) -> Result<Arc<InstanceMetrics>>;
2523
fn index(&self) -> Result<Arc<Index>>;

src/docbuilder/rustwide_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async fn get_configured_toolchain(conn: &mut sqlx::PgConnection) -> Result<Toolc
5555
}
5656
}
5757

58-
fn build_workspace(context: &dyn Context) -> Result<Workspace> {
58+
fn build_workspace<C: Context>(context: &C) -> Result<Workspace> {
5959
let config = context.config()?;
6060

6161
let mut builder = WorkspaceBuilder::new(&config.rustwide_workspace, USER_AGENT)
@@ -99,7 +99,7 @@ pub struct RustwideBuilder {
9999
}
100100

101101
impl RustwideBuilder {
102-
pub fn init(context: &dyn Context) -> Result<Self> {
102+
pub fn init<C: Context>(context: &C) -> Result<Self> {
103103
let config = context.config()?;
104104
let pool = context.pool()?;
105105
let runtime = context.runtime()?;
@@ -123,9 +123,9 @@ impl RustwideBuilder {
123123
})
124124
}
125125

126-
pub fn reinitialize_workspace_if_interval_passed(
126+
pub fn reinitialize_workspace_if_interval_passed<C: Context>(
127127
&mut self,
128-
context: &dyn Context,
128+
context: &C,
129129
) -> Result<()> {
130130
let interval = context.config()?.build_workspace_reinitialization_interval;
131131
if self.workspace_initialize_time.elapsed() >= interval {

src/test/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
};
1414
use anyhow::Context as _;
1515
use axum::body::Bytes;
16-
use axum::{async_trait, body::Body, http::Request, response::Response as AxumResponse, Router};
16+
use axum::{body::Body, http::Request, response::Response as AxumResponse, Router};
1717
use fn_error_context::context;
1818
use futures_util::{stream::TryStreamExt, FutureExt};
1919
use http_body_util::BodyExt; // for `collect`
@@ -621,7 +621,6 @@ impl TestEnvironment {
621621
}
622622
}
623623

624-
#[async_trait]
625624
impl Context for TestEnvironment {
626625
fn config(&self) -> Result<Arc<Config>> {
627626
Ok(TestEnvironment::config(self))

src/utils/consistency/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const BUILD_PRIORITY: i32 = 15;
2424
///
2525
/// Even when activities fail, the command can just be re-run. While the diff calculation will
2626
/// be repeated, we won't re-execute fixing activities.
27-
pub fn run_check(ctx: &dyn Context, dry_run: bool) -> Result<()> {
27+
pub fn run_check<C: Context>(ctx: &C, dry_run: bool) -> Result<()> {
2828
let index = ctx.index()?;
2929

3030
info!("Loading data from database...");
@@ -79,9 +79,10 @@ struct HandleResult {
7979
yanks_corrected: u32,
8080
}
8181

82-
fn handle_diff<'a, I>(ctx: &dyn Context, iter: I, dry_run: bool) -> Result<HandleResult>
82+
fn handle_diff<'a, I, C>(ctx: &C, iter: I, dry_run: bool) -> Result<HandleResult>
8383
where
8484
I: Iterator<Item = &'a diff::Difference>,
85+
C: Context,
8586
{
8687
let mut result = HandleResult::default();
8788

src/utils/daemon.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub async fn watch_registry(
5353
}
5454
}
5555

56-
fn start_registry_watcher(context: &dyn Context) -> Result<(), Error> {
56+
fn start_registry_watcher<C: Context>(context: &C) -> Result<(), Error> {
5757
let build_queue = context.runtime()?.block_on(context.async_build_queue())?;
5858
let config = context.config()?;
5959
let index = context.index()?;
@@ -69,7 +69,7 @@ fn start_registry_watcher(context: &dyn Context) -> Result<(), Error> {
6969
Ok(())
7070
}
7171

72-
pub fn start_background_repository_stats_updater(context: &dyn Context) -> Result<(), Error> {
72+
pub fn start_background_repository_stats_updater<C: Context>(context: &C) -> Result<(), Error> {
7373
// This call will still skip github repositories updates and continue if no token is provided
7474
// (gitlab doesn't require to have a token). The only time this can return an error is when
7575
// 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
9191
Ok(())
9292
}
9393

94-
pub fn start_background_queue_rebuild(context: &dyn Context) -> Result<(), Error> {
94+
pub fn start_background_queue_rebuild<C: Context>(context: &C) -> Result<(), Error> {
9595
let runtime = context.runtime()?;
9696
let pool = context.pool()?;
9797
let config = context.config()?;
@@ -120,7 +120,7 @@ pub fn start_background_queue_rebuild(context: &dyn Context) -> Result<(), Error
120120
Ok(())
121121
}
122122

123-
pub fn start_background_cdn_invalidator(context: &dyn Context) -> Result<(), Error> {
123+
pub fn start_background_cdn_invalidator<C: Context>(context: &C) -> Result<(), Error> {
124124
let metrics = context.instance_metrics()?;
125125
let config = context.config()?;
126126
let pool = context.pool()?;

src/utils/queue_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::time::Duration;
77
use std::{fs, io, path::Path, thread};
88
use tracing::{debug, error, warn};
99

10-
pub fn queue_builder(
11-
context: &dyn Context,
10+
pub fn queue_builder<C: Context>(
11+
context: &C,
1212
mut builder: RustwideBuilder,
1313
build_queue: Arc<BuildQueue>,
1414
config: Arc<Config>,

src/web/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ async fn set_sentry_transaction_name_from_axum_route(
399399
next.run(request).await
400400
}
401401

402-
async fn apply_middleware(
402+
async fn apply_middleware<C: Context>(
403403
router: AxumRouter,
404-
context: &dyn Context,
404+
context: &C,
405405
template_data: Option<Arc<TemplateData>>,
406406
) -> Result<AxumRouter> {
407407
let config = context.config()?;
@@ -441,20 +441,20 @@ async fn apply_middleware(
441441
))
442442
}
443443

444-
pub(crate) async fn build_axum_app(
445-
context: &dyn Context,
444+
pub(crate) async fn build_axum_app<C: Context>(
445+
context: &C,
446446
template_data: Arc<TemplateData>,
447447
) -> Result<AxumRouter, Error> {
448448
apply_middleware(routes::build_axum_routes(), context, Some(template_data)).await
449449
}
450450

451-
pub(crate) async fn build_metrics_axum_app(context: &dyn Context) -> Result<AxumRouter, Error> {
451+
pub(crate) async fn build_metrics_axum_app<C: Context>(context: &C) -> Result<AxumRouter, Error> {
452452
apply_middleware(routes::build_metric_routes(), context, None).await
453453
}
454454

455-
pub fn start_background_metrics_webserver(
455+
pub fn start_background_metrics_webserver<C: Context>(
456456
addr: Option<SocketAddr>,
457-
context: &dyn Context,
457+
context: &C,
458458
) -> Result<(), Error> {
459459
let axum_addr: SocketAddr = addr.unwrap_or(DEFAULT_BIND);
460460

@@ -492,7 +492,7 @@ pub fn start_background_metrics_webserver(
492492
}
493493

494494
#[instrument(skip_all)]
495-
pub fn start_web_server(addr: Option<SocketAddr>, context: &dyn Context) -> Result<(), Error> {
495+
pub fn start_web_server<C: Context>(addr: Option<SocketAddr>, context: &C) -> Result<(), Error> {
496496
let template_data = Arc::new(TemplateData::new(context.config()?.render_threads)?);
497497

498498
let axum_addr = addr.unwrap_or(DEFAULT_BIND);

0 commit comments

Comments
 (0)