Skip to content

Commit

Permalink
Merge pull request #153 from omid/remove_lazy_static
Browse files Browse the repository at this point in the history
Remove lazy_static
  • Loading branch information
jaemk authored Jun 3, 2023
2 parents 271fdc8 + 780a9e8 commit 4e06daf
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
```
## Removed

## [0.43.1]
## Removed
- Dependency to `lazy_static` and `async_once` are removed.

## [0.43.0]
## Added
## Changed
Expand Down
15 changes: 2 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[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"]
Expand All @@ -44,14 +44,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"

Expand Down Expand Up @@ -84,7 +76,7 @@ optional = true

[dependencies.tokio]
version = "1"
features = ["macros", "time", "sync"]
features = ["macros", "time", "sync", "parking_lot"]
optional = true

[dependencies.instant]
Expand All @@ -97,9 +89,6 @@ features = ["attributes"]
[dev-dependencies.smartstring]
version = "1"

[dev-dependencies.lazy_static]
version = "1"

[dev-dependencies.serial_test]
version = "2"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down
10 changes: 5 additions & 5 deletions cached_proc_macro/src/io_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,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
}
Expand Down Expand Up @@ -347,17 +347,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
}
Expand All @@ -369,6 +368,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
}
Expand Down
6 changes: 3 additions & 3 deletions examples/redis-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,9 +60,8 @@ impl Config {
}
}
}
lazy_static::lazy_static! {
static ref CONFIG: Config = Config::load();
}

static CONFIG: Lazy<Config> = Lazy::new(Config::load);

#[io_cached(
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
Expand Down
6 changes: 3 additions & 3 deletions examples/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,9 +56,8 @@ impl Config {
}
}
}
lazy_static::lazy_static! {
static ref CONFIG: Config = Config::load();
}

static CONFIG: Lazy<Config> = Lazy::new(Config::load);

#[io_cached(
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
Expand Down
9 changes: 2 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,14 @@ 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_attr(docsrs, feature(doc_cfg))]

#[cfg(feature = "async")]
#[doc(hidden)]
pub extern crate async_once;
#[cfg(feature = "async")]
#[doc(hidden)]
pub extern crate lazy_static;
#[doc(hidden)]
pub extern crate once_cell;

Expand Down Expand Up @@ -209,6 +203,7 @@ pub use instant;
#[doc(hidden)]
pub mod async_sync {
pub use tokio::sync::Mutex;
pub use tokio::sync::OnceCell;
pub use tokio::sync::RwLock;
}

Expand Down
9 changes: 6 additions & 3 deletions src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,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();
}
```
Expand All @@ -282,13 +283,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();
}
```
Expand All @@ -305,7 +307,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<String> = {
Expand All @@ -318,6 +320,7 @@ pub fn main() {
}
}
});
// handler.join().unwrap();
}
```
Expand Down

0 comments on commit 4e06daf

Please sign in to comment.