Skip to content

Commit aecc6c7

Browse files
committed
refactor: simplify vendor macro
1 parent 0e87766 commit aecc6c7

File tree

1 file changed

+18
-47
lines changed

1 file changed

+18
-47
lines changed

encoderfile/src/assets.rs

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,33 @@
11
macro_rules! embed_in_section {
22
($name:ident, $path:expr, $section:expr, Bytes) => {
3-
#[cfg(target_os = "macos")]
4-
#[unsafe(link_section = concat!("__DATA,", $section))]
5-
#[used]
6-
#[unsafe(no_mangle)]
7-
pub static $name: [u8; include_bytes!(env!($path)).len()] = *include_bytes!(env!($path));
8-
9-
#[cfg(target_os = "linux")]
10-
#[unsafe(link_section = concat!(".", $section))]
11-
#[used]
12-
#[unsafe(no_mangle)]
13-
pub static $name: [u8; include_bytes!(env!($path)).len()] = *include_bytes!(env!($path));
14-
15-
#[cfg(target_os = "windows")]
16-
#[unsafe(link_section = concat!(".rdata$", $section))]
17-
#[used]
18-
#[unsafe(no_mangle)]
19-
pub static $name: [u8; include_bytes!(env!($path)).len()] = *include_bytes!(env!($path));
3+
embed_in_section!(
4+
$name,
5+
[u8; include_bytes!(env!($path)).len()],
6+
$section,
7+
*include_bytes!(env!($path))
8+
);
209
};
2110

2211
($name:ident, $path:expr, $section:expr, String) => {
23-
#[cfg(target_os = "macos")]
24-
#[unsafe(link_section = concat!("__DATA,", $section))]
25-
#[used]
26-
#[unsafe(no_mangle)]
27-
pub static $name: &str = include_str!(env!($path));
28-
29-
#[cfg(target_os = "linux")]
30-
#[unsafe(link_section = concat!(".", $section))]
31-
#[used]
32-
#[unsafe(no_mangle)]
33-
pub static $name: &str = include_str!(env!($path));
34-
35-
#[cfg(target_os = "windows")]
36-
#[unsafe(link_section = concat!(".rdata$", $section))]
37-
#[used]
38-
#[unsafe(no_mangle)]
39-
pub static $name: &str = include_str!(env!($path));
12+
embed_in_section!($name, &str, $section, include_str!(env!($path)));
4013
};
4114

4215
($name:ident, $path:expr, $section:expr, Env) => {
43-
#[cfg(target_os = "macos")]
44-
#[unsafe(link_section = concat!("__DATA,", $section))]
45-
#[used]
46-
#[unsafe(no_mangle)]
47-
pub static $name: &str = env!($path);
16+
embed_in_section!($name, &str, $section, env!($path));
17+
};
4818

49-
#[cfg(target_os = "linux")]
50-
#[unsafe(link_section = concat!(".", $section))]
51-
#[used]
52-
#[unsafe(no_mangle)]
53-
pub static $name: &str = env!($path);
19+
($name:ident, $dtype:ty, $section:expr, $res:expr) => {
20+
embed_in_section!($name, $dtype, concat!("__DATA,", $section), $res, "macos");
21+
embed_in_section!($name, $dtype, concat!(".", $section), $res, "linux");
22+
embed_in_section!($name, $dtype, concat!(".rdata$", $section), $res, "windows");
23+
};
5424

55-
#[cfg(target_os = "windows")]
56-
#[unsafe(link_section = concat!(".rdata$", $section))]
25+
($name:ident, $dtype:ty, $section:expr, $res:expr, $target_os:expr) => {
26+
#[cfg(target_os = $target_os)]
27+
#[unsafe(link_section = $section)]
5728
#[used]
5829
#[unsafe(no_mangle)]
59-
pub static $name: &str = env!($path);
30+
pub static $name: $dtype = $res;
6031
};
6132
}
6233

0 commit comments

Comments
 (0)