Skip to content

Commit a32b699

Browse files
committed
add lazy_static_include_array
1 parent 73976f6 commit a32b699

21 files changed

+1110
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lazy-static-include"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
authors = ["Magic Len <len@magiclen.org>"]
55
repository = "https://github.com/magiclen/lazy-static-include"
66
homepage = "https://magiclen.org/lazy-static-include"

README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ assert_eq!("This is just a test text.".as_bytes(), TEST[0]);
4141
assert_eq!(TEST[1], "Some text...".as_bytes());
4242
```
4343

44-
You should notice that the struct created from `include_bytes` and `include_str` macros isn't equal to `&'static [u8]` or `&'static str`.
44+
You should notice that the struct created from `lazy_static_include_bytes` and `lazy_static_include_str` macros isn't equal to `&'static [u8]` or `&'static str`.
4545
If you want to get an exact `&'static [u8]` or `&'static str` reference, you need to **dereference the struct**.
4646

4747
```rust
@@ -65,6 +65,54 @@ lazy_static_include_str!(TEST, "data/test.txt", "data/test-2.txt");
6565
let v: &Vec<&'static str> = &*TEST;
6666
```
6767

68+
## Include Array
69+
70+
There is a special macro `lazy_static_include_array` which can include arrays from files.
71+
The array is fixed sized and can be one of these following types: `bool`, `char`, `u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64`, `i128`, `f32`, `f64`, `&'static str`.
72+
73+
Also, the `lazy_static_include_array` macro includes data from files into the compiled executable binary file **only** when you are using the **release** profile.
74+
Be careful when you distribute your program.
75+
76+
```rust
77+
#[macro_use] extern crate lazy_static_include;
78+
#[macro_use] extern crate lazy_static;
79+
80+
lazy_static_include_array!(TEST: [u64; 5], "data/u64_array.txt");
81+
assert_eq!(123, TEST[0]);
82+
assert_eq!(456, TEST[1]);
83+
assert_eq!(789, TEST[2]);
84+
assert_eq!(1000, TEST[3]);
85+
assert_eq!(500000000000u64, TEST[4]);
86+
```
87+
88+
```rust
89+
#[macro_use] extern crate lazy_static_include;
90+
#[macro_use] extern crate lazy_static;
91+
92+
lazy_static_include_array!(TEST: [i32; 5], "data/i32_array.txt", "data/i32_array-2.txt");
93+
assert_eq!(123, TEST[0][0]);
94+
assert_eq!(-456, TEST[0][1]);
95+
assert_eq!(789, TEST[0][2]);
96+
assert_eq!(1000, TEST[0][3]);
97+
assert_eq!(5000, TEST[0][4]);
98+
99+
assert_eq!(-1, TEST[1][0]);
100+
assert_eq!(-2, TEST[1][1]);
101+
assert_eq!(-3, TEST[1][2]);
102+
assert_eq!(-4, TEST[1][3]);
103+
assert_eq!(-5, TEST[1][4]);
104+
```
105+
```rust
106+
#[macro_use] extern crate lazy_static_include;
107+
#[macro_use] extern crate lazy_static;
108+
109+
lazy_static_include_array!(pub TEST: [&'static str; 3], "data/string_array.txt");
110+
111+
assert_eq!("Hi", TEST[0]);
112+
assert_eq!("Hello", TEST[1]);
113+
assert_eq!("哈囉", TEST[2]);
114+
```
115+
68116
## Crates.io
69117

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

data/bool_array.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[false, true, false]

data/char_array.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
['a', 'b', 'c']

data/f32_array.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[123.0, -456.0, 789.5, 1_000.123f32, 5000f32]

data/f64_array.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[123.0, -456.0, 789.5, 1_000.123f64, 5000.456f64]

data/i128_array.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[123, -456, 789, 1_000, 500000000000000000000000i128]

data/i16_array.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[123, -456, 789, 1_000, 5000i16]

data/i32_array-2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[-1, -2, -3, -4, -5]

data/i32_array.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[123, -456, 789, 1_000, 5000i32]

0 commit comments

Comments
 (0)