Skip to content

Commit 47de6e3

Browse files
committed
add *vec macros
1 parent 64d490e commit 47de6e3

File tree

3 files changed

+241
-1
lines changed

3 files changed

+241
-1
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.1.3"
3+
version = "1.2.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"

src/lib.rs

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
#[macro_export]
114114
macro_rules! lazy_static_include_counter {
115115
() => (0usize);
116+
( Vec $(, $xs:expr)* $(,)* ) => (lazy_static_include_counter!($($xs, )*));
116117
( $x:expr $(, $xs:expr)* $(,)* ) => (1usize + lazy_static_include_counter!($($xs, )*));
117118
}
118119

@@ -203,6 +204,17 @@ macro_rules! lazy_static_include_str_inner {
203204
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path))
204205
}
205206
};
207+
( $name:ident, Vec, $($paths:expr), + ) => {
208+
{
209+
let mut v: Vec<&'static str> = Vec::with_capacity(lazy_static_include_counter!(Vec $(, $paths)+));
210+
211+
$(
212+
v.push(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $paths)));
213+
)+
214+
215+
v
216+
}
217+
};
206218
( $name:ident, $path:expr, $($paths:expr), + ) => {
207219
{
208220
let mut v: Vec<&'static str> = Vec::with_capacity(lazy_static_include_counter!($path $(, $paths)+));
@@ -249,6 +261,41 @@ macro_rules! lazy_static_include_str_inner {
249261
}
250262
}
251263
};
264+
( $name:ident, Vec, $($paths:expr), + ) => {
265+
{
266+
use ::std::fs::File;
267+
use ::std::io::Read;
268+
use ::std::mem;
269+
270+
let mut v: Vec<&'static str> = Vec::with_capacity(lazy_static_include_counter!(Vec $(, $paths)+));
271+
272+
$(
273+
let vv = {
274+
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/", $paths);
275+
276+
let mut f = File::open(&path).unwrap();
277+
278+
let mut v: Vec<u8> = Vec::new();
279+
280+
f.read_to_end(&mut v).unwrap();
281+
282+
v
283+
};
284+
285+
let s = String::from_utf8(vv).unwrap();
286+
287+
v.push(
288+
unsafe {
289+
let ret = mem::transmute(s.as_str());
290+
mem::forget(s);
291+
ret
292+
}
293+
);
294+
)+
295+
296+
v
297+
}
298+
};
252299
( $name:ident, $path:expr, $($paths:expr), + ) => {
253300
{
254301
use ::std::fs::File;
@@ -317,6 +364,13 @@ macro_rules! lazy_static_include_str {
317364

318365
lazy_static_include_str_impl!($name);
319366
};
367+
( $name:ident, Vec, $($paths:expr), + ) => {
368+
lazy_static! {
369+
static ref $name: Vec<&'static str> = lazy_static_include_str_inner!($name, Vec $(, $paths)+);
370+
}
371+
372+
lazy_static_include_str_multiple_impl!($name);
373+
};
320374
( $name:ident, $path:expr, $($paths:expr), + ) => {
321375
lazy_static! {
322376
static ref $name: Vec<&'static str> = lazy_static_include_str_inner!($name, $path $(, $paths)+);
@@ -331,6 +385,13 @@ macro_rules! lazy_static_include_str {
331385

332386
lazy_static_include_str_impl!($name);
333387
};
388+
( pub $name:ident, Vec, $($paths:expr), + ) => {
389+
lazy_static! {
390+
static ref $name: Vec<&'static str> = lazy_static_include_str_inner!($name, Vec $(, $paths)+);
391+
}
392+
393+
lazy_static_include_str_multiple_impl!($name);
394+
};
334395
( pub $name:ident, $path:expr, $($paths:expr), + ) => {
335396
lazy_static! {
336397
static ref $name: Vec<&'static str> = lazy_static_include_str_inner!($name, $path $(, $paths)+);
@@ -340,6 +401,24 @@ macro_rules! lazy_static_include_str {
340401
};
341402
}
342403

404+
#[macro_export]
405+
macro_rules! lazy_static_include_str_vec {
406+
( $name:ident, $($paths:expr), + ) => {
407+
lazy_static! {
408+
static ref $name: Vec<&'static str> = lazy_static_include_str_inner!($name, Vec $(, $paths)+);
409+
}
410+
411+
lazy_static_include_str_multiple_impl!($name);
412+
};
413+
( pub $name:ident, $($paths:expr), + ) => {
414+
lazy_static! {
415+
static ref $name: Vec<&'static str> = lazy_static_include_str_inner!($name, Vec $(, $paths)+);
416+
}
417+
418+
lazy_static_include_str_multiple_impl!($name);
419+
};
420+
}
421+
343422
// TODO -----include_str END-----
344423

345424
// TODO -----include_bytes START-----
@@ -413,6 +492,17 @@ macro_rules! lazy_static_include_bytes_inner {
413492
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path))
414493
}
415494
};
495+
( $name:ident, Vec, $($paths:expr), + ) => {
496+
{
497+
let mut v: Vec<&'static [u8]> = Vec::with_capacity(lazy_static_include_counter!(Vec $(, $paths)+));
498+
499+
$(
500+
v.push(include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $paths)));
501+
)+
502+
503+
v
504+
}
505+
};
416506
( $name:ident, $path:expr, $($paths:expr), + ) => {
417507
{
418508
let mut v: Vec<&'static [u8]> = Vec::with_capacity(lazy_static_include_counter!($path $(, $paths)+));
@@ -457,6 +547,39 @@ macro_rules! lazy_static_include_bytes_inner {
457547
}
458548
}
459549
};
550+
( $name:ident, Vec, $($paths:expr), + ) => {
551+
{
552+
use ::std::fs::File;
553+
use ::std::io::Read;
554+
use ::std::mem;
555+
556+
let mut v: Vec<&'static [u8]> = Vec::with_capacity(lazy_static_include_counter!(Vec $(, $paths)+));
557+
558+
$(
559+
let vv = {
560+
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/", $paths);
561+
562+
let mut f = File::open(&path).unwrap();
563+
564+
let mut v: Vec<u8> = Vec::new();
565+
566+
f.read_to_end(&mut v).unwrap();
567+
568+
v
569+
};
570+
571+
v.push(
572+
unsafe {
573+
let ret = mem::transmute(vv.as_ref() as &[u8]);
574+
mem::forget(vv);
575+
ret
576+
}
577+
);
578+
)+
579+
580+
v
581+
}
582+
};
460583
( $name:ident, $path:expr, $($paths:expr), + ) => {
461584
{
462585
use ::std::fs::File;
@@ -544,6 +667,24 @@ macro_rules! lazy_static_include_bytes {
544667
};
545668
}
546669

670+
#[macro_export]
671+
macro_rules! lazy_static_include_bytes_vec {
672+
( $name:ident, $($paths:expr), + ) => {
673+
lazy_static! {
674+
static ref $name: Vec<&'static [u8]> = lazy_static_include_bytes_inner!($name, Vec $(, $paths)+);
675+
}
676+
677+
lazy_static_include_bytes_multiple_impl!($name);
678+
};
679+
( pub $name:ident, $($paths:expr), + ) => {
680+
lazy_static! {
681+
static ref $name: Vec<&'static [u8]> = lazy_static_include_bytes_inner!($name, Vec $(, $paths)+);
682+
}
683+
684+
lazy_static_include_bytes_multiple_impl!($name);
685+
};
686+
}
687+
547688
// TODO -----include_bytes END-----
548689

549690
// TODO -----include_array START-----
@@ -557,6 +698,17 @@ macro_rules! lazy_static_include_array_inner {
557698
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path))
558699
}
559700
};
701+
( $name:ident: [&'static str; $s:expr], Vec, $($paths:expr), + ) => {
702+
{
703+
let mut v: Vec<[&'static str; $s]> = Vec::with_capacity(lazy_static_include_counter!(Vec $(, $paths)+));
704+
705+
$(
706+
v.push(include!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $paths)));
707+
)+
708+
709+
v
710+
}
711+
};
560712
( $name:ident: [&'static str; $s:expr], $path:expr, $($paths:expr), + ) => {
561713
{
562714
let mut v: Vec<[&'static str; $s]> = Vec::with_capacity(lazy_static_include_counter!($path $(, $paths)+));
@@ -575,6 +727,17 @@ macro_rules! lazy_static_include_array_inner {
575727
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path))
576728
}
577729
};
730+
( $name:ident: [$t:ident; $s:expr], Vec, $($paths:expr), + ) => {
731+
{
732+
let mut v: Vec<[$t; $s]> = Vec::with_capacity(lazy_static_include_counter!(Vec $(, $paths)+));
733+
734+
$(
735+
v.push(include!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $paths)));
736+
)+
737+
738+
v
739+
}
740+
};
578741
( $name:ident: [$t:ident; $s:expr], $path:expr, $($paths:expr), + ) => {
579742
{
580743
let mut v: Vec<[$t; $s]> = Vec::with_capacity(lazy_static_include_counter!($path $(, $paths)+));
@@ -1243,11 +1406,33 @@ macro_rules! lazy_static_include_array_inner {
12431406
lazy_static_include_array_inner_b!($name: [bool; $s], $path)
12441407
}
12451408
};
1409+
( $name:ident: [&'static str; $s:expr], Vec, $($paths:expr), + ) => {
1410+
{
1411+
let mut v: Vec<[&'static str; $s]> = Vec::with_capacity(lazy_static_include_counter!(Vec $(, $paths)+));
1412+
1413+
$(
1414+
v.push(lazy_static_include_array_inner!($name: [&'static str; $s], $paths));
1415+
)+
1416+
1417+
v
1418+
}
1419+
};
12461420
( $name:ident: [&'static str; $s:expr], $path:expr ) => {
12471421
{
12481422
lazy_static_include_array_inner_s!($name: [&'static str; $s], $path)
12491423
}
12501424
};
1425+
( $name:ident: [$t:ident; $s:expr], Vec, $($paths:expr), + ) => {
1426+
{
1427+
let mut v: Vec<[$t; $s]> = Vec::with_capacity(lazy_static_include_counter!(Vec $(, $paths)+));
1428+
1429+
$(
1430+
v.push(lazy_static_include_array_inner!($name: [$t; $s], $paths));
1431+
)+
1432+
1433+
v
1434+
}
1435+
};
12511436
( $name:ident: [&'static str; $s:expr], $path:expr, $($paths:expr), + ) => {
12521437
{
12531438
let mut v: Vec<[&'static str; $s]> = Vec::with_capacity(lazy_static_include_counter!($path $(, $paths)+));
@@ -1320,4 +1505,28 @@ macro_rules! lazy_static_include_array {
13201505
};
13211506
}
13221507

1508+
#[macro_export]
1509+
macro_rules! lazy_static_include_array_vec {
1510+
( $name:ident: [&'static str; $s:expr] $(, $paths:expr)+ ) => {
1511+
lazy_static! {
1512+
static ref $name: Vec<[&'static str; $s]> = lazy_static_include_array_inner!($name: [&'static str; $s], Vec $(, $paths)+);
1513+
}
1514+
};
1515+
( pub $name:ident: [&'static str; $s:expr], $($paths:expr), + ) => {
1516+
lazy_static! {
1517+
pub static ref $name: Vec<[&'static str; $s]> = lazy_static_include_array_inner!($name: [&'static str; $s], Vec $(, $paths)+);
1518+
}
1519+
};
1520+
( $name:ident: [$t:ident; $s:expr], $($paths:expr), + ) => {
1521+
lazy_static! {
1522+
static ref $name: Vec<[$t; $s]> = lazy_static_include_array_inner!($name: [$t; $s], Vec $(, $paths)+);
1523+
}
1524+
};
1525+
( pub $name:ident: [$t:ident; $s:expr], $($paths:expr), + ) => {
1526+
lazy_static! {
1527+
pub static ref $name: Vec<[$t; $s]> = lazy_static_include_array_inner!($name: [$t; $s], Vec $(, $paths)+);
1528+
}
1529+
};
1530+
}
1531+
13231532
// TODO -----include_array END-----

tests/macro.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ fn test_include_str() {
1414
assert_eq!(TEST2, "Some text...");
1515
}
1616

17+
#[test]
18+
fn test_include_str_vec() {
19+
lazy_static_include_str_vec!(TEST, "data/test.txt");
20+
21+
assert_eq!("This is just a test text.", TEST[0]);
22+
23+
lazy_static_include_str_vec!(pub TEST2, "data/test-2.txt");
24+
25+
assert_eq!(TEST2[0], "Some text...");
26+
}
27+
1728
#[test]
1829
fn test_include_str_multiple() {
1930
lazy_static_include_str!(TEST, "data/test.txt", "data/test-2.txt");
@@ -37,6 +48,17 @@ fn test_include_bytes() {
3748
assert_eq!(TEST2, "Some text...".as_bytes());
3849
}
3950

51+
#[test]
52+
fn test_include_bytes_vec() {
53+
lazy_static_include_bytes_vec!(TEST, "data/test.txt");
54+
55+
assert_eq!("This is just a test text.".as_bytes(), TEST[0]);
56+
57+
lazy_static_include_bytes_vec!(pub TEST2, "data/test-2.txt");
58+
59+
assert_eq!(TEST2[0], "Some text...".as_bytes());
60+
}
61+
4062
#[test]
4163
fn test_include_bytes_multiple() {
4264
lazy_static_include_bytes!(TEST, "data/test.txt", "data/test-2.txt");
@@ -208,6 +230,15 @@ fn test_include_array_string() {
208230
assert_eq!("哈囉", TEST[2]);
209231
}
210232

233+
#[test]
234+
fn test_include_array_string_vec() {
235+
lazy_static_include_array_vec!(TEST: [&'static str; 3], "data/string_array.txt");
236+
237+
assert_eq!("Hi", TEST[0][0]);
238+
assert_eq!("Hello", TEST[0][1]);
239+
assert_eq!("哈囉", TEST[0][2]);
240+
}
241+
211242
#[test]
212243
fn test_include_array_string_multiple() {
213244
lazy_static_include_array!(TEST: [&'static str; 3], "data/string_array.txt", "data/string_array-2.txt");

0 commit comments

Comments
 (0)