From 41fa23457d65b5c4688a42228734fd8479214a83 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 25 Nov 2023 12:08:12 -0500 Subject: [PATCH 1/5] Update README to indicate how to replace with `std::sync::OnceLock` --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index bd30c61..0bba4de 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ as well as anything that requires non-const function calls to be computed. [![Documentation](https://docs.rs/lazy_static/badge.svg)](https://docs.rs/lazy_static) [![License](https://img.shields.io/crates/l/lazy_static.svg)](https://github.com/rust-lang-nursery/lazy-static.rs#license) + ## Minimum supported `rustc` `1.40.0+` @@ -61,6 +62,35 @@ fn main() { } ``` +# Standard library + +It is now possible to easily replicate this crate's functionality in Rust's standard library with [`std::sync::OnceLock`](https://doc.rust-lang.org/std/sync/struct.OnceLock.html). The example above could be also be written as: + +```rust +use std::collections::HashMap; +use std::sync::OnceLock; + +static HASHMAP: OnceLock> = OnceLock::new(); + +fn hashmap() -> &'static HashMap { + HASHMAP.get_or_init(|| { + let mut m = HashMap::new(); + m.insert(0, "foo"); + m.insert(1, "bar"); + m.insert(2, "baz"); + m + }) +} + +fn main() { + // First access to `HASHMAP` initializes it + println!("The entry for `0` is \"{}\".", hashmap().get(&0).unwrap()); + + // Any further access to `HASHMAP` just returns the computed value + println!("The entry for `1` is \"{}\".", hashmap().get(&1).unwrap()); +} +``` + ## License Licensed under either of From a8afb21bfd9dc53b51111bd1d9f19c2275fc97fe Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 25 Nov 2023 12:08:52 -0500 Subject: [PATCH 2/5] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 0bba4de..4d91e95 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ as well as anything that requires non-const function calls to be computed. [![Documentation](https://docs.rs/lazy_static/badge.svg)](https://docs.rs/lazy_static) [![License](https://img.shields.io/crates/l/lazy_static.svg)](https://github.com/rust-lang-nursery/lazy-static.rs#license) - ## Minimum supported `rustc` `1.40.0+` From fc8e466b94cc9c5f38d6002d3cc865f19a9ca004 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 25 Nov 2023 13:55:04 -0500 Subject: [PATCH 3/5] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d91e95..b48182a 100644 --- a/README.md +++ b/README.md @@ -69,9 +69,8 @@ It is now possible to easily replicate this crate's functionality in Rust's stan use std::collections::HashMap; use std::sync::OnceLock; -static HASHMAP: OnceLock> = OnceLock::new(); - fn hashmap() -> &'static HashMap { + static HASHMAP: OnceLock> = OnceLock::new(); HASHMAP.get_or_init(|| { let mut m = HashMap::new(); m.insert(0, "foo"); From cfd89ac7fcbc3fb8ee5ee20b374b2b1dfb357198 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 25 Nov 2023 13:56:03 -0500 Subject: [PATCH 4/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b48182a..4a9308f 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ use lazy_static::lazy_static; use std::collections::HashMap; lazy_static! { - static ref HASHMAP: HashMap = { + static ref HASHMAP: HashMap = { let mut m = HashMap::new(); m.insert(0, "foo"); m.insert(1, "bar"); From a2031053df502518e44fec72712db8f927c6ee5c Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 25 Nov 2023 13:56:36 -0500 Subject: [PATCH 5/5] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4a9308f..045244a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ use lazy_static::lazy_static; use std::collections::HashMap; lazy_static! { - static ref HASHMAP: HashMap = { + static ref HASHMAP: HashMap = { let mut m = HashMap::new(); m.insert(0, "foo"); m.insert(1, "bar"); @@ -70,7 +70,7 @@ use std::collections::HashMap; use std::sync::OnceLock; fn hashmap() -> &'static HashMap { - static HASHMAP: OnceLock> = OnceLock::new(); + static HASHMAP: OnceLock> = OnceLock::new(); HASHMAP.get_or_init(|| { let mut m = HashMap::new(); m.insert(0, "foo");