Skip to content

Commit e3ecaef

Browse files
committed
Use env::temp_dir for constime base instead of storing relative to
project. Also joins to the crate name now, just in case there's a collision across two crates that for some reason have an identical constime! invocation.
1 parent 1159e52 commit e3ecaef

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,4 @@ fn main() {
6060

6161
**You should get autocomplete and error feedback when using the macro.**
6262

63-
This works with temporary files, which currently aren't automatically cleared.
64-
65-
If you notice your project getting a little large, periodically run `cargo clean` to remove these.
63+
This by creating temporary binaries running each of your constime! invocations, stored in [`std::env::temp_dir()`](https://doc.rust-lang.org/std/env/fn.temp_dir.html)`.join("constime")`

src/lib.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,32 @@ pub fn comptime(mut code: TokenStream) -> TokenStream {
3434
}
3535
}
3636

37-
if out_dir.is_none() {
38-
let out = std::env::current_dir().unwrap().join("target").join("debug").join("deps");
39-
if out.exists() {
40-
out_dir = Some(out);
41-
}
42-
}
43-
4437
let Some(out_dir) = out_dir else {
4538
return build_error!("Could not find output directory.");
4639
};
4740

41+
#[rustfmt::skip]
4842
let wrapped_code = format!(r#"
4943
fn main() {{
5044
println!("{{:?}}", {{ {code} }});
5145
}}
5246
"#);
5347

54-
let hash = RandomState::new().hash_one(&wrapped_code);
48+
let code_hash = RandomState::new().hash_one(&wrapped_code);
49+
let crate_name = std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "unknown".to_owned());
5550

56-
let constime_base = out_dir.join("constime");
51+
// Join by crate_name in the rare case of identical blocks across crates
52+
let constime_base = std::env::temp_dir().join("constime").join(crate_name);
5753
if !constime_base.exists() {
58-
std::fs::create_dir(&constime_base).unwrap();
54+
if let Err(why) = std::fs::create_dir(&constime_base) {
55+
return build_error!("Failed to create temp directory: {why}");
56+
}
5957
}
6058

61-
let evaluator_base = constime_base
62-
.join(hash.to_string());
59+
let evaluator_base = constime_base.join(code_hash.to_string());
6360

64-
if !evaluator_base.exists() { // This hasn't been compiled yet.
61+
if !evaluator_base.exists() {
62+
// This hasn't been compiled yet.
6563
let mut rustc = std::process::Command::new("rustc");
6664
rustc
6765
.stderr(std::process::Stdio::piped())

0 commit comments

Comments
 (0)