跳过正文

lazy_static

··523 字
Rust Rust-Crate
rust crate - 这篇文章属于一个选集。
§ 14: 本文

https://docs.rs/lazy_static/latest/lazy_static/

Rust 1.80 support LazyLock, lazy_static can be replaced by std::sync::LazyLock。

注:建议使用较新的 once_cell crate, 它功能更加强大,支持 local variables 的延迟初始化,所以也可以用于数组 field。

Rust 对 static 变量的 编译时初始化 有一些限制:只能使用常量表达式、const 函数来初始化。而不支持非 const 函数,复杂表达式的初始化。

lazy_static 宏提供了在运行时对 static 变量进行初始化的能力,而且支持复杂表达式、调用非 const 函数的初始化。

语法:

lazy_static! {
    [pub] static ref NAME_1: TYPE_1 = EXPR_1;
    [pub] static ref NAME_2: TYPE_2 = EXPR_2;
    ...
    [pub] static ref NAME_N: TYPE_N = EXPR_N;
}

// 或者
lazy_static! {
    /// This is an example for using doc comment attributes
    static ref EXAMPLE: u8 = 42;
}

实现细节:For a given static ref NAME: TYPE = EXPR;, the macro generates a unique type that implements Deref<TYPE> and stores it in a static with name NAME. (Attributes end up attaching to this type.)。 On first deref, EXPR gets evaluated and stored internally, such that all further derefs can return a reference to the same object.

  • The Deref implementation uses a hidden static variable that is guarded by an atomic check on each access.

总结:

  1. lay_static! 实现了一个内部类型,它实现了 Deref<TYPE>, 该内部类型作为 NAME 的类型;
  2. 当对 NAME 第一次 deref 时,Deref 的实现会用 EXPR 初始化 NAME,然后后续每次再 deref 时复用第一次初始化的结果;《=== 延迟初始化
lazy_static! {
    /// This is an example for using doc comment attributes
    static ref EXAMPLE: u8 = 42;
}

// 复杂例子,可以用任意表达式初始化 lazy static 变量。
#[macro_use]
extern crate lazy_static;

use std::collections::HashMap;

lazy_static! {
    static ref HASHMAP: HashMap<u32, &'static str> = {
        let mut m = HashMap::new();
        m.insert(0, "foo");
        m.insert(1, "bar");
        m.insert(2, "baz");
        m
    };
    static ref COUNT: usize = HASHMAP.len();
    static ref NUMBER: u32 = times_two(21);
}

fn times_two(n: u32) -> u32 { n * 2 }

fn main() {
    println!("The map has {} entries.", *COUNT);
    println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap());
    println!("A expensive calculation on a static results in: {}.", *NUMBER);
}
rust crate - 这篇文章属于一个选集。
§ 14: 本文

相关文章

axum
··12636 字
Rust Rust-Crate
axum 是基于 hyper 实现的高性能异步 HTTP 1/2 Server 库。
clap
··5086 字
Rust Rust-Crate
diesel
··34358 字
Rust Rust-Crate
diesel 是高性能的 ORM 和 Query Builder,crates.io 使用它来操作数据库。
http/http_body
··4783 字
Rust Rust-Crate
http/http_body crate 是公共的 http 和 body 定义,在 tokio 系列的 HTTP 库,如 hyper/axum/reqwest 中得到广泛应用。