Skip to content

Commit 46507d1

Browse files
committed
fix API
1 parent 74a9616 commit 46507d1

File tree

6 files changed

+138
-53
lines changed

6 files changed

+138
-53
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 = "3.0.1"
3+
version = "3.0.2"
44
authors = ["Magic Len <len@magiclen.org>"]
55
edition = "2018"
66
repository = "https://github.com/magiclen/lazy-static-include"

README.md

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ assert_eq!("This is just a test text.".as_bytes(), TEST);
4747
assert_eq!("Some text...".as_bytes(), TEST2);
4848
```
4949

50-
You should notice that the value created from `lazy_static_include_bytes` and `lazy_static_include_str` macros isn't equal to `&'static [u8]` or `&'static str` when you are not using the **release** profile. If you want to get an exact `&'static [u8]` or `&'static str` reference, you can **dereference** the value or just use the `as_ref` method.
50+
You should notice that the value created from `lazy_static_include_bytes` and `lazy_static_include_str` macros isn't equal to `&'static [u8]` or `&'static str`. If you want to get an exact `&'static [u8]` or `&'static str` reference, you can **dereference** the value.
5151

5252
```rust
5353
#[macro_use] extern crate lazy_static_include;
@@ -57,13 +57,7 @@ lazy_static_include_bytes! {
5757
TEST => "data/test.txt",
5858
}
5959

60-
#[cfg(debug_assertions)]
6160
let data: &'static [u8] = *TEST;
62-
63-
#[cfg(not(debug_assertions))]
64-
let data: &'static [u8] = TEST;
65-
66-
let data: &'static [u8] = TEST.as_ref();
6761
```
6862

6963
## Include Array
@@ -102,18 +96,18 @@ assert_eq!("哈囉", TEST2[2]);
10296
Using static mechanisms makes your program faster. See my benchmark result below (AMD Ryzen 9 3900X 12-Core Processor 12C/24T 3.90GHz, ran on 2020/07/02):
10397

10498
```text
105-
test include_array_lazy_static ... bench: 45 ns/iter (+/- 3)
106-
test include_array_native_static ... bench: 45 ns/iter (+/- 3)
107-
test include_array_no_static ... bench: 20,959 ns/iter (+/- 295)
108-
test include_bytes_lazy_static ... bench: 754 ns/iter (+/- 7)
109-
test include_bytes_native_static ... bench: 755 ns/iter (+/- 11)
110-
test include_bytes_no_static ... bench: 4,560 ns/iter (+/- 179)
111-
test include_str_lazy_static ... bench: 753 ns/iter (+/- 10)
112-
test include_str_native_static ... bench: 755 ns/iter (+/- 7)
113-
test include_str_no_static ... bench: 4,830 ns/iter (+/- 198)
99+
test include_array_lazy_static ... bench: 46 ns/iter (+/- 3)
100+
test include_array_native_static ... bench: 48 ns/iter (+/- 3)
101+
test include_array_no_static ... bench: 22,414 ns/iter (+/- 297)
102+
test include_bytes_lazy_static ... bench: 844 ns/iter (+/- 3)
103+
test include_bytes_native_static ... bench: 863 ns/iter (+/- 5)
104+
test include_bytes_no_static ... bench: 4,764 ns/iter (+/- 189)
105+
test include_str_lazy_static ... bench: 857 ns/iter (+/- 8)
106+
test include_str_native_static ... bench: 842 ns/iter (+/- 10)
107+
test include_str_no_static ... bench: 4,837 ns/iter (+/- 145)
114108
```
115109

116-
When using the **release** profile, the performance of `lazy_static_include_*` is very close to `include_*` (in fast, they are the same). That means you don't need to worry about the overhead, but just enjoy the faster compilation time.
110+
When using the **release** profile, the performance of `lazy_static_include_*` is very close to `include_*`. That means you don't need to worry about the overhead, but just enjoy the faster compilation time.
117111

118112
You can run the benchmark program by executing,
119113

src/lib.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,17 @@ assert_eq!("This is just a test text.".as_bytes(), TEST);
4545
assert_eq!("Some text...".as_bytes(), TEST2);
4646
```
4747
48-
You should notice that the value created from `lazy_static_include_bytes` and `lazy_static_include_str` macros isn't equal to `&'static [u8]` or `&'static str` when you are not using the **release** profile. If you want to get an exact `&'static [u8]` or `&'static str` reference, you can **dereference** the value or just use the `as_ref` method.
48+
You should notice that the value created from `lazy_static_include_bytes` and `lazy_static_include_str` macros isn't equal to `&'static [u8]` or `&'static str`. If you want to get an exact `&'static [u8]` or `&'static str` reference, you can **dereference** the value.
4949
50-
```rust,ignore
50+
```rust
5151
#[macro_use] extern crate lazy_static_include;
5252
5353
lazy_static_include_bytes! {
5454
/// doc
5555
TEST => "data/test.txt",
5656
}
5757
58-
#[cfg(debug_assertions)]
5958
let data: &'static [u8] = *TEST;
60-
61-
#[cfg(not(debug_assertions))]
62-
let data: &'static [u8] = TEST;
63-
64-
let data: &'static [u8] = TEST.as_ref();
6559
```
6660
6761
## Include Array
@@ -100,18 +94,18 @@ assert_eq!("哈囉", TEST2[2]);
10094
Using static mechanisms makes your program faster. See my benchmark result below (AMD Ryzen 9 3900X 12-Core Processor 12C/24T 3.90GHz, ran on 2020/07/02):
10195
10296
```text
103-
test include_array_lazy_static ... bench: 45 ns/iter (+/- 3)
104-
test include_array_native_static ... bench: 45 ns/iter (+/- 3)
105-
test include_array_no_static ... bench: 20,959 ns/iter (+/- 295)
106-
test include_bytes_lazy_static ... bench: 754 ns/iter (+/- 7)
107-
test include_bytes_native_static ... bench: 755 ns/iter (+/- 11)
108-
test include_bytes_no_static ... bench: 4,560 ns/iter (+/- 179)
109-
test include_str_lazy_static ... bench: 753 ns/iter (+/- 10)
110-
test include_str_native_static ... bench: 755 ns/iter (+/- 7)
111-
test include_str_no_static ... bench: 4,830 ns/iter (+/- 198)
97+
test include_array_lazy_static ... bench: 46 ns/iter (+/- 3)
98+
test include_array_native_static ... bench: 48 ns/iter (+/- 3)
99+
test include_array_no_static ... bench: 22,414 ns/iter (+/- 297)
100+
test include_bytes_lazy_static ... bench: 844 ns/iter (+/- 3)
101+
test include_bytes_native_static ... bench: 863 ns/iter (+/- 5)
102+
test include_bytes_no_static ... bench: 4,764 ns/iter (+/- 189)
103+
test include_str_lazy_static ... bench: 857 ns/iter (+/- 8)
104+
test include_str_native_static ... bench: 842 ns/iter (+/- 10)
105+
test include_str_no_static ... bench: 4,837 ns/iter (+/- 145)
112106
```
113107
114-
When using the **release** profile, the performance of `lazy_static_include_*` is very close to `include_*` (in fast, they are the same). That means you don't need to worry about the overhead, but just enjoy the faster compilation time.
108+
When using the **release** profile, the performance of `lazy_static_include_*` is very close to `include_*`. That means you don't need to worry about the overhead, but just enjoy the faster compilation time.
115109
116110
You can run the benchmark program by executing,
117111

src/macro_include_bytes.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,56 @@ macro_rules! lazy_static_include_bytes {
8888
/// The file is located relative to the directory containing the manifest of your package.
8989
#[macro_export]
9090
macro_rules! lazy_static_include_bytes {
91+
( @impl $name:ident ) => {
92+
impl<'a> ::std::cmp::PartialEq<&'a [u8]> for $name {
93+
fn eq(&self, other: &&'a [u8]) -> bool {
94+
(&*$name).eq(other)
95+
}
96+
}
97+
98+
impl ::std::cmp::PartialEq for $name {
99+
fn eq(&self, other: &$name) -> bool {
100+
true
101+
}
102+
}
103+
104+
impl<'a> ::std::cmp::PartialEq<$name> for &'a [u8] {
105+
fn eq(&self, other: &$name) -> bool {
106+
self.eq(&*$name)
107+
}
108+
}
109+
110+
impl ::std::fmt::Debug for $name {
111+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
112+
::std::fmt::Debug::fmt(*$name, f)
113+
}
114+
}
115+
116+
impl<T> ::std::convert::AsRef<T> for $name
117+
where
118+
T: ?Sized,
119+
[u8]: ::std::convert::AsRef<T>,
120+
{
121+
fn as_ref(&self) -> &T {
122+
(*$name).as_ref()
123+
}
124+
}
125+
};
91126
( @unit $(#[$attr: meta])* ($v:tt) $name:ident => $path:expr ) => {
92-
static $name: &'static [u8] = include_bytes!($crate::concat_with_file_separator!(env!("CARGO_MANIFEST_DIR"), $path));
127+
$crate::lazy_static! {
128+
$(#[$attr])*
129+
static ref $name: &'static [u8] = include_bytes!($crate::concat_with_file_separator!(env!("CARGO_MANIFEST_DIR"), $path));
130+
}
131+
132+
$crate::lazy_static_include_bytes!(@impl $name);
93133
};
94134
( @unit $(#[$attr: meta])* (pub$(($($v:tt)+))?) $name:ident => $path:expr ) => {
95-
pub$(($($v)+))? static $name: &'static [u8] = include_bytes!($crate::concat_with_file_separator!(env!("CARGO_MANIFEST_DIR"), $path));
135+
$crate::lazy_static! {
136+
$(#[$attr])*
137+
pub$(($($v)+))? static ref $name: &'static [u8] = include_bytes!($crate::concat_with_file_separator!(env!("CARGO_MANIFEST_DIR"), $path));
138+
}
139+
140+
$crate::lazy_static_include_bytes!(@impl $name);
96141
};
97142
( $($(#[$attr: meta])* $v:vis $name:ident => $path:expr),* $(,)* ) => {
98143
$(

src/macro_include_str.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,75 @@ macro_rules! lazy_static_include_str {
107107
/// The file is located relative to the directory containing the manifest of your package.
108108
#[macro_export]
109109
macro_rules! lazy_static_include_str {
110+
( @impl $name:ident ) => {
111+
impl ::std::cmp::PartialEq<str> for $name {
112+
#[inline]
113+
fn eq(&self, other: &str) -> bool {
114+
(*$name).eq(other)
115+
}
116+
}
117+
118+
impl<'a> ::std::cmp::PartialEq<&'a str> for $name {
119+
#[inline]
120+
fn eq(&self, other: &&'a str) -> bool {
121+
(&*$name).eq(other)
122+
}
123+
}
124+
125+
impl ::std::cmp::PartialEq for $name {
126+
#[inline]
127+
fn eq(&self, other: &$name) -> bool {
128+
true
129+
}
130+
}
131+
132+
impl<'a> ::std::cmp::PartialEq<$name> for &'a str {
133+
#[inline]
134+
fn eq(&self, other: &$name) -> bool {
135+
self.eq(&*$name)
136+
}
137+
}
138+
139+
impl ::std::fmt::Debug for $name {
140+
#[inline]
141+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
142+
::std::fmt::Debug::fmt(*$name, f)
143+
}
144+
}
145+
146+
impl ::std::fmt::Display for $name {
147+
#[inline]
148+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
149+
::std::fmt::Display::fmt(*$name, f)
150+
}
151+
}
152+
153+
impl<T> ::std::convert::AsRef<T> for $name
154+
where
155+
T: ?Sized,
156+
str: ::std::convert::AsRef<T>,
157+
{
158+
#[inline]
159+
fn as_ref(&self) -> &T {
160+
(*$name).as_ref()
161+
}
162+
}
163+
};
110164
( @unit $(#[$attr: meta])* ($v:tt) $name:ident => $path:expr ) => {
111-
static $name: &'static str = include_str!($crate::concat_with_file_separator!(env!("CARGO_MANIFEST_DIR"), $path));
165+
$crate::lazy_static! {
166+
$(#[$attr])*
167+
static ref $name: &'static str = include_str!($crate::concat_with_file_separator!(env!("CARGO_MANIFEST_DIR"), $path));
168+
}
169+
170+
$crate::lazy_static_include_str!(@impl $name);
112171
};
113172
( @unit $(#[$attr: meta])* (pub$(($($v:tt)+))?) $name:ident => $path:expr ) => {
114-
pub$(($($v)+))? static $name: &'static str = include_str!($crate::concat_with_file_separator!(env!("CARGO_MANIFEST_DIR"), $path));
173+
$crate::lazy_static! {
174+
$(#[$attr])*
175+
pub$(($($v)+))? static ref $name: &'static str = include_str!($crate::concat_with_file_separator!(env!("CARGO_MANIFEST_DIR"), $path));
176+
}
177+
178+
$crate::lazy_static_include_str!(@impl $name);
115179
};
116180
( $($(#[$attr: meta])* $v:vis $name:ident => $path:expr),* $(,)* ) => {
117181
$(

tests/macros.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ fn include_str() {
1111
pub TEST2 => "data/test-2.txt",
1212
}
1313

14-
#[cfg(debug_assertions)]
1514
let _data: &'static str = *TEST;
1615

17-
#[cfg(not(debug_assertions))]
18-
let _data: &'static str = TEST;
19-
20-
let _data: &'static str = TEST.as_ref();
21-
2216
assert_eq!("This is just a test text.", TEST);
2317
assert_eq!("Some text...", TEST2);
2418
}
@@ -30,13 +24,7 @@ fn include_bytes() {
3024
pub TEST2 => "data/test-2.txt",
3125
}
3226

33-
#[cfg(debug_assertions)]
34-
let _data: &'static [u8] = *TEST;
35-
36-
#[cfg(not(debug_assertions))]
37-
let _data: &'static [u8] = TEST;
38-
39-
let _data: &'static [u8] = TEST.as_ref();
27+
let _data: &'static [u8] = *TEST;
4028

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

0 commit comments

Comments
 (0)