From 386341dcbe5171f8b5744c834a8a39970cbd8a75 Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Wed, 26 Apr 2023 16:43:21 +0200 Subject: [PATCH 1/2] Remove lazy_static --- CHANGELOG.md | 4 ++++ Cargo.toml | 17 +++-------------- README.md | 2 +- cached_proc_macro/src/io_cached.rs | 10 +++++----- examples/redis-async.rs | 6 +++--- examples/redis.rs | 6 +++--- src/lib.rs | 7 ++----- src/proc_macro.rs | 9 ++++++--- 8 files changed, 27 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ff58ac..33d4440 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ ## Changed ## Removed +## [0.42.1] +## Removed +- Dependency to `lazy_static` and `async_once` are removed. + ## [0.42.0] / [cached_proc_macro[0.16.0]] ## Added ## Changed diff --git a/Cargo.toml b/Cargo.toml index 288d855..7239ee1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ all-features = true [features] default = ["proc_macro", "async"] proc_macro = ["tokio", "cached_proc_macro", "cached_proc_macro_types"] -async = ["futures", "tokio", "async-trait", "async_once", "lazy_static"] +async = ["futures", "tokio", "async-trait"] async_tokio_rt_multi_thread = ["async", "tokio/rt-multi-thread"] redis_store = ["redis", "r2d2", "serde", "serde_json"] redis_async_std = ["redis_store", "async", "redis/aio", "redis/async-std-comp", "redis/tls", "redis/async-std-tls-comp"] @@ -43,14 +43,6 @@ features = ["raw", "inline-more"] [dependencies.once_cell] version = "1" -[dependencies.lazy_static] -version = "1" -optional = true - -[dependencies.async_once] -version = "0.2" -optional = true - [dependencies.thiserror] version = "1" @@ -83,7 +75,7 @@ optional = true [dependencies.tokio] version = "1" -features = ["macros", "time", "sync"] +features = ["macros", "time", "sync", "parking_lot"] optional = true [dependencies.instant] @@ -96,11 +88,8 @@ features = ["attributes"] [dev-dependencies.smartstring] version = "1" -[dev-dependencies.lazy_static] -version = "1" - [dev-dependencies.serial_test] -version = "0.10" +version = "1" [workspace] members = ["cached_proc_macro","examples/wasm"] diff --git a/README.md b/README.md index 3cc1fa3..803306c 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ Due to the requirements of storing arguments and return values in a global cache where arguments are formatted into `Strings` and values are de/serialized. - Macro-defined functions should not be used to produce side-effectual results! - Macro-defined functions cannot live directly under `impl` blocks since macros expand to a - `once_cell`/`lazy_static` initialization and one or more function definitions. + `once_cell` initialization and one or more function definitions. - Macro-defined functions cannot accept `Self` types as a parameter. diff --git a/cached_proc_macro/src/io_cached.rs b/cached_proc_macro/src/io_cached.rs index eebed93..3f9547e 100644 --- a/cached_proc_macro/src/io_cached.rs +++ b/cached_proc_macro/src/io_cached.rs @@ -337,7 +337,7 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream { // run the function and cache the result async fn inner(#inputs) #output #body; let result = inner(#(#input_names),*).await; - let cache = &#cache_ident.get().await; + let cache = &#cache_ident.get_or_init(init).await; #set_cache_block result } @@ -373,17 +373,16 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream { quote! { // Cached static #[doc = #cache_ident_doc] - ::cached::lazy_static::lazy_static! { - #visibility static ref #cache_ident: ::cached::async_once::AsyncOnce<#cache_ty> = ::cached::async_once::AsyncOnce::new(async move { #cache_create }); - } + #visibility static #cache_ident: ::cached::async_sync::OnceCell<#cache_ty> = ::cached::async_sync::OnceCell::const_new(); // Cached function #(#attributes)* #visibility #signature_no_muts { + let init = || async { #cache_create }; use cached::IOCachedAsync; let key = #key_convert_block; { // check if the result is cached - let cache = &#cache_ident.get().await; + let cache = &#cache_ident.get_or_init(init).await; if let Some(result) = cache.cache_get(&key).await.map_err(#map_error)? { #return_cache_block } @@ -395,6 +394,7 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream { #[allow(dead_code)] #visibility #prime_sig { use cached::IOCachedAsync; + let init = || async { #cache_create }; let key = #key_convert_block; #do_set_return_block } diff --git a/examples/redis-async.rs b/examples/redis-async.rs index 1c29394..ffd6d8c 100644 --- a/examples/redis-async.rs +++ b/examples/redis-async.rs @@ -9,6 +9,7 @@ Cleanup the redis docker container: use cached::proc_macro::io_cached; use cached::AsyncRedisCache; +use once_cell::sync::Lazy; use std::io; use std::io::Write; use std::time::Duration; @@ -59,9 +60,8 @@ impl Config { } } } -lazy_static::lazy_static! { - static ref CONFIG: Config = Config::load(); -} + +static CONFIG: Lazy = Lazy::new(Config::load); #[io_cached( map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##, diff --git a/examples/redis.rs b/examples/redis.rs index a883eeb..278366e 100644 --- a/examples/redis.rs +++ b/examples/redis.rs @@ -9,6 +9,7 @@ Cleanup the redis docker container: use cached::proc_macro::io_cached; use cached::RedisCache; +use once_cell::sync::Lazy; use std::io; use std::io::Write; use std::time::Duration; @@ -55,9 +56,8 @@ impl Config { } } } -lazy_static::lazy_static! { - static ref CONFIG: Config = Config::load(); -} + +static CONFIG: Lazy = Lazy::new(Config::load); #[io_cached( map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##, diff --git a/src/lib.rs b/src/lib.rs index a033044..a3248b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -160,16 +160,12 @@ Due to the requirements of storing arguments and return values in a global cache where arguments are formatted into `Strings` and values are de/serialized. - Macro-defined functions should not be used to produce side-effectual results! - Macro-defined functions cannot live directly under `impl` blocks since macros expand to a - `once_cell`/`lazy_static` initialization and one or more function definitions. + `once_cell` initialization and one or more function definitions. - Macro-defined functions cannot accept `Self` types as a parameter. */ -#[cfg(feature = "async")] -pub extern crate async_once; -#[cfg(feature = "async")] -pub extern crate lazy_static; pub extern crate once_cell; #[cfg(feature = "proc_macro")] @@ -194,6 +190,7 @@ pub use instant; #[cfg(any(feature = "proc_macro", feature = "async"))] pub mod async_sync { pub use tokio::sync::Mutex; + pub use tokio::sync::OnceCell; pub use tokio::sync::RwLock; } diff --git a/src/proc_macro.rs b/src/proc_macro.rs index 711b67f..2f86ab1 100644 --- a/src/proc_macro.rs +++ b/src/proc_macro.rs @@ -256,13 +256,14 @@ fn keyed(a: &str) -> usize { a.len() } pub fn main() { - std::thread::spawn(|| { + let handler = std::thread::spawn(|| { loop { sleep(Duration::from_secs(50)); // this method is generated by the `cached` macro keyed_prime_cache("a"); } }); + handler.join().unwrap(); } ``` @@ -280,13 +281,14 @@ fn keyed() -> String { "some data".to_string() } pub fn main() { - std::thread::spawn(|| { + let handler = std::thread::spawn(|| { loop { sleep(Duration::from_secs(60)); // this method is generated by the `cached` macro keyed_prime_cache(); } }); + handler.join().unwrap(); } ``` @@ -303,7 +305,7 @@ fn keyed(a: &str) -> usize { a.len() } pub fn main() { - std::thread::spawn(|| { + let handler = std::thread::spawn(|| { loop { sleep(Duration::from_secs(60)); let keys: Vec = { @@ -316,6 +318,7 @@ pub fn main() { } } }); + handler.join().unwrap(); } ``` From d2cba81d11473c832b60fc43d6109a868a21e1a2 Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Wed, 26 Apr 2023 23:17:43 +0200 Subject: [PATCH 2/2] clippy --- src/proc_macro.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/proc_macro.rs b/src/proc_macro.rs index 2f86ab1..03ff4a0 100644 --- a/src/proc_macro.rs +++ b/src/proc_macro.rs @@ -256,14 +256,14 @@ fn keyed(a: &str) -> usize { a.len() } pub fn main() { - let handler = std::thread::spawn(|| { + let _handler = std::thread::spawn(|| { loop { sleep(Duration::from_secs(50)); // this method is generated by the `cached` macro keyed_prime_cache("a"); } }); - handler.join().unwrap(); + // handler.join().unwrap(); } ``` @@ -281,14 +281,14 @@ fn keyed() -> String { "some data".to_string() } pub fn main() { - let handler = std::thread::spawn(|| { + let _handler = std::thread::spawn(|| { loop { sleep(Duration::from_secs(60)); // this method is generated by the `cached` macro keyed_prime_cache(); } }); - handler.join().unwrap(); + // handler.join().unwrap(); } ``` @@ -305,7 +305,7 @@ fn keyed(a: &str) -> usize { a.len() } pub fn main() { - let handler = std::thread::spawn(|| { + let _handler = std::thread::spawn(|| { loop { sleep(Duration::from_secs(60)); let keys: Vec = { @@ -318,7 +318,7 @@ pub fn main() { } } }); - handler.join().unwrap(); + // handler.join().unwrap(); } ```