From 4f0e7e1fbe55f866b6fe5fe9fe4d6c7bcae609f5 Mon Sep 17 00:00:00 2001 From: Benno van den Berg Date: Fri, 2 Aug 2024 13:54:55 +0200 Subject: [PATCH] Replace OnceLock with LazyLock Resolve clippies --- fpx-lib/src/api/handlers/spans.rs | 2 +- fpx-lib/src/data.rs | 1 + fpx-lib/src/data/fake_store.rs | 11 +---------- fpx-workers/src/lib.rs | 11 +++-------- 4 files changed, 6 insertions(+), 19 deletions(-) diff --git a/fpx-lib/src/api/handlers/spans.rs b/fpx-lib/src/api/handlers/spans.rs index d4829a346..508686c87 100644 --- a/fpx-lib/src/api/handlers/spans.rs +++ b/fpx-lib/src/api/handlers/spans.rs @@ -7,7 +7,7 @@ use fpx_macros::ApiError; use http::StatusCode; use serde::{Deserialize, Serialize}; use thiserror::Error; -use tracing::{error, info}; +use tracing::error; #[tracing::instrument(skip_all)] pub async fn span_get_handler( diff --git a/fpx-lib/src/data.rs b/fpx-lib/src/data.rs index 87734d8c7..81234c1ea 100644 --- a/fpx-lib/src/data.rs +++ b/fpx-lib/src/data.rs @@ -10,6 +10,7 @@ type Result = anyhow::Result; pub type BoxedStore = Arc; +#[derive(Clone, Default, Debug)] pub struct Transaction {} impl Transaction { diff --git a/fpx-lib/src/data/fake_store.rs b/fpx-lib/src/data/fake_store.rs index 6e27301de..4614a70e5 100644 --- a/fpx-lib/src/data/fake_store.rs +++ b/fpx-lib/src/data/fake_store.rs @@ -1,28 +1,19 @@ use super::{models, DbError, Result, Store, Transaction}; use async_trait::async_trait; use std::sync::{Arc, RwLock}; -use tracing::info; /// A simple in-memory [`Store`] implementation. Currently only intended for /// proof of concept. /// /// This implementation does not provide any Transaction support, nor will it /// work concurrently. -#[derive(Clone, Debug)] +#[derive(Clone, Default, Debug)] pub struct FakeStore { /// Spans are stored in a [`RwLock`] so that we can always mutate the inner /// [`Vec`], even with a reference to this [`FakeStore`]. spans: Arc>>, } -impl FakeStore { - pub fn new() -> Self { - Self { - spans: Arc::new(RwLock::new(vec![])), - } - } -} - #[async_trait] impl Store for FakeStore { async fn start_readonly_transaction(&self) -> Result { diff --git a/fpx-workers/src/lib.rs b/fpx-workers/src/lib.rs index f14208c12..c1c2cacf1 100644 --- a/fpx-workers/src/lib.rs +++ b/fpx-workers/src/lib.rs @@ -1,16 +1,15 @@ use fpx_lib::data::fake_store::FakeStore; use fpx_lib::events::ServerEvents; use fpx_lib::{api, service}; -use std::sync::{Arc, OnceLock}; +use std::sync::{Arc, LazyLock}; use tower_service::Service; -use tracing::info; use tracing_subscriber::fmt::format::Pretty; use tracing_subscriber::fmt::time::UtcTime; use tracing_subscriber::prelude::*; use tracing_web::{performance_layer, MakeConsoleWriter}; use worker::*; -static FAKE_STORE: OnceLock = OnceLock::new(); +static FAKE_STORE: LazyLock = LazyLock::new(FakeStore::default); #[event(start)] fn start() { @@ -24,10 +23,6 @@ fn start() { .with(fmt_layer) .with(perf_layer) .init(); - - FAKE_STORE - .set(FakeStore::new()) - .expect("failed to set FakeStore"); } #[event(fetch)] @@ -38,7 +33,7 @@ async fn fetch( ) -> Result> { console_error_panic_hook::set_once(); - let store = FAKE_STORE.get().unwrap().clone(); + let store = FAKE_STORE.clone(); let boxed_store = Arc::new(store); let events = ServerEvents::new();