Skip to content

Commit 3b22f9b

Browse files
committed
Add example with generated cfg option
1 parent 28c3dbb commit 3b22f9b

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed

build.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ fn main() {
2424
}
2525
");
2626
println!("cargo:rustc-env=GENERATED_ENV={}", gen_dir.display());
27+
28+
println!("cargo:rustc-cfg=has_generated_feature")
2729
}
2830

2931
fn generate_file<P: AsRef<Path>>(path: P, text: &[u8]) {

examples/generated_cfg.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use code_generation_example::function_under_cfg;
2+
3+
fn main() {
4+
function_under_cfg();
5+
}

src/disabled.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn function_under_cfg() {
2+
println!("'has_generated_feature' is disabled")
3+
}

src/enabled.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn function_under_cfg() {
2+
println!("'has_generated_feature' is enabled")
3+
}

src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
include!(concat!(env!("OUT_DIR"), "/hello.rs"));
22
include!(concat!(env!("GENERATED_ENV"), "/hello2.rs"));
3+
4+
#[cfg(not(has_generated_feature))]
5+
mod disabled;
6+
#[cfg(has_generated_feature)]
7+
mod enabled;
8+
9+
#[cfg(not(has_generated_feature))]
10+
pub use disabled::function_under_cfg;
11+
#[cfg(has_generated_feature)]
12+
pub use enabled::function_under_cfg;

0 commit comments

Comments
 (0)