-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b295873
commit b2927b0
Showing
3 changed files
with
38 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
[package] | ||
name = "lazy_init" | ||
name = "lazyinit" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Yuekai Jia <[email protected]>"] | ||
description = "A wrapper for lazy initialized values without concurrency safety but more efficient" | ||
description = "Initialize a static value lazily." | ||
license = "GPL-3.0-or-later OR Apache-2.0 OR MulanPSL-2.0" | ||
homepage = "https://github.com/arceos-org/arceos" | ||
repository = "https://github.com/arceos-org/lazy_init" | ||
documentation = "https://arceos-org.github.io/lazy_init" | ||
repository = "https://github.com/arceos-org/lazyinit" | ||
documentation = "https://docs.rs/lazyinit" | ||
keywords = ["lazy", "initialization", "static"] | ||
categories = ["no-std", "rust-patterns"] | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# lazyinit | ||
|
||
[![Crates.io](https://img.shields.io/crates/v/lazyinit)](https://crates.io/crates/lazyinit) | ||
[![Docs.rs](https://docs.rs/lazyinit/badge.svg)](https://docs.rs/lazyinit) | ||
[![CI](https://github.com/arceos-org/lazyinit/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/arceos-org/lazyinit/actions/workflows/ci.yml) | ||
|
||
Initialize a static value lazily. | ||
|
||
Unlike [`lazy_static`][1], this crate does not provide concurrency safety. | ||
The value **MUST** be used after only **ONE** initialization. However, it | ||
can be more efficient, as there is no need to check whether other threads | ||
are also performing initialization at the same time. | ||
|
||
## Examples | ||
|
||
```rust | ||
use lazyinit::LazyInit; | ||
|
||
static VALUE: LazyInit<u32> = LazyInit::new(); | ||
assert!(!VALUE.is_init()); | ||
// println!("{}", *VALUE); // panic: use uninitialized value | ||
assert_eq!(VALUE.try_get(), None); | ||
|
||
VALUE.init_by(233); | ||
// VALUE.init_by(666); // panic: already initialized | ||
assert!(VALUE.is_init()); | ||
assert_eq!(*VALUE, 233); | ||
assert_eq!(VALUE.try_get(), Some(&233)); | ||
``` | ||
|
||
[1]: https://docs.rs/lazy_static/latest/lazy_static/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters