Skip to content

Commit

Permalink
Publish v0.1.0 to crates.io
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Jul 17, 2024
1 parent b295873 commit b2927b0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
10 changes: 6 additions & 4 deletions Cargo.toml
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]
31 changes: 31 additions & 0 deletions README.md
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/
27 changes: 1 addition & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
//! A wrapper for lazy initialized values.
//!
//! 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
//!
//! ```
//! use lazy_init::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/
#![no_std]
#![doc = include_str!("../README.md")]

use core::cell::UnsafeCell;
use core::fmt;
Expand Down

0 comments on commit b2927b0

Please sign in to comment.