#惰性计算 #静态 # #包含 #文件时间

lazy-static-include

该crate提供lazy_static_include_byteslazy_static_include_str宏来替换include_bytesinclude_str宏。

29个稳定版本

3.2.1 2023年9月11日
3.2.0 2023年4月24日
3.1.3 2022年11月2日
3.1.2 2022年3月17日
1.2.1 2018年11月14日

#147 in Rust 模式

Download history 370/week @ 2024-04-22 493/week @ 2024-04-29 375/week @ 2024-05-06 695/week @ 2024-05-13 1092/week @ 2024-05-20 491/week @ 2024-05-27 456/week @ 2024-06-03 440/week @ 2024-06-10 749/week @ 2024-06-17 569/week @ 2024-06-24 333/week @ 2024-07-01 286/week @ 2024-07-08 565/week @ 2024-07-15 463/week @ 2024-07-22 406/week @ 2024-07-29 419/week @ 2024-08-05

1,898 每月下载量
8 个crate中(5个直接使用)

MIT 许可证

41KB
718

Lazy Static Include

CI

该crate提供lazy_static_include_byteslazy_static_include_str宏来替换include_bytesinclude_str宏。

为什么要这样做呢?因为原始的include_bytesinclude_str宏会将额外的数据从文件中带到编译的可执行二进制文件中,编译时间会激增。

长的编译时间对软件开发不利。lazy_static_include_byteslazy_static_include_str宏可以在您不使用release配置文件时从文件中懒加载数据。换句话说,如果您使用include_bytesinclude_str宏,并且您认为您的编译时间太高而无法等待,您可以选择使用lazy_static_include_byteslazy_static_include_str宏。

lazy_static_include_byteslazy_static_include_str宏仅在您使用release配置文件时将数据包含到编译的可执行二进制文件中。在分发您的程序时请小心。

用于lazy_static_include_byteslazy_static_include_str的路径相对于CARGO_MANIFEST_DIR

示例

use lazy_static_include::*;

lazy_static_include_str! {
    /// doc
    TEST => "data/test.txt",
}

lazy_static_include_str! {
    /// doc
    pub TEST2 => ("data", "test-2.txt"),
}

assert_eq!("This is just a test text.", TEST);
assert_eq!("Some text...", TEST2);
use lazy_static_include::*;

lazy_static_include_bytes! {
    /// doc
    TEST => "data/test.txt",
}

lazy_static_include_bytes! {
    /// doc
    pub TEST2 => ("data", "test-2.txt"),
}

assert_eq!("This is just a test text.".as_bytes(), TEST);
assert_eq!("Some text...".as_bytes(), TEST2);

您应该注意,由lazy_static_include_byteslazy_static_include_str宏创建的值不等于&'static [u8]&'static str。如果您想得到精确的&'static [u8]&'static str引用,您可以解引用该值。

use lazy_static_include::*;

lazy_static_include_bytes! {
    /// doc
    TEST => "data/test.txt",
}

let data: &'static [u8] = *TEST;

此外,私有项(没有pub)和公共项(带有pub*)不能放在一起。

包含数组

存在一个特殊宏 lazy_static_include_array,它可以包含来自文件的数组。数组大小固定,可以是以下类型之一:boolcharusizeu8u16u32u64u128isizei8i16i32i64i128f32f64&'static str

此外,lazy_static_include_array 宏仅在您使用 release 配置文件时将文件数据包含到编译后的可执行二进制文件中。在分发您的程序时要小心。

lazy_static_include_array 使用的路径相对于 CARGO_MANIFEST_DIR

use lazy_static_include::*;

lazy_static_include_array! {
    /// doc
    TEST: [u64; 5] => "data/u64_array.txt",
}

lazy_static_include_array! {
    /// doc
    pub TEST2: [&'static str; 3] => ("data", "string_array.txt")
}

assert_eq!(123, TEST[0]);
assert_eq!(456, TEST[1]);
assert_eq!(789, TEST[2]);
assert_eq!(1000, TEST[3]);
assert_eq!(500000000000u64, TEST[4]);

assert_eq!("Hi", TEST2[0]);
assert_eq!("Hello", TEST2[1]);
assert_eq!("哈囉", TEST2[2]);

基准测试

使用静态机制可以使您的程序更快。请参见以下基准测试结果(AMD Ryzen 9 3900X 12-Core Processor 12C/24T 3.90GHz,运行于 2020/07/02)

test include_array_lazy_static   ... bench:          46 ns/iter (+/- 3)
test include_array_native_static ... bench:          48 ns/iter (+/- 3)
test include_array_no_static     ... bench:      22,414 ns/iter (+/- 297)
test include_bytes_lazy_static   ... bench:         844 ns/iter (+/- 3)
test include_bytes_native_static ... bench:         863 ns/iter (+/- 5)
test include_bytes_no_static     ... bench:       4,764 ns/iter (+/- 189)
test include_str_lazy_static     ... bench:         857 ns/iter (+/- 8)
test include_str_native_static   ... bench:         842 ns/iter (+/- 10)
test include_str_no_static       ... bench:       4,837 ns/iter (+/- 145)

当使用 release 配置文件时,lazy_static_include_ 的性能与 include_ 非常接近。这意味着您无需担心开销,只需享受更快的编译时间。

您可以通过执行以下命令来运行基准测试程序:

cargo bench

Crates.io

https://crates.io/crates/lazy-static-include

文档

https://docs.rs/lazy-static-include

许可证

MIT

依赖项

~290–760KB
~18K SLoC