-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Description
Right now, include_str!()
works on a syntax level and does not allow a user to pass in a &'static str
for the file path. I stumbled upon this after the following code didn't compile in release mode:
const CSS_PATH: &str = "C:\\my\\file\\path\\test.css";
#[cfg(debug_assertions)]
let css = Css::hot_reload(CSS_PATH).unwrap();
#[cfg(not(debug_assertions))]
let css = Css::new_from_str(include_str!(CSS_PATH)).unwrap();
Notice the Css::hot_reload
vs the Css::new_from_str
- in debug mode I want to hot-reload the file from disk (for quick style changes), but in release mode I want to include it in the binary.
Now I can solve it by doing:
#[cfg(debug_assertions)]
let css = Css::hot_reload("C:\\my\\file\\path\\test.css").unwrap();
#[cfg(not(debug_assertions))]
let css = Css::new_from_str(include_str!("C:\\my\\file\\path\\test.css")).unwrap();
... but then I need to update the file path in two places, which isn't good.
Looking at the source of include_str!()
, it seems that include_str!
works directly on the AST, i.e. the compiler doesn't know about whether the given string (the file path) is const
or not, so if it is implemented it would maybe be necessary to restructure this part of the compiler.
I realize that it's a low-priority issue, but it would be nice to have. And yes, I can solve it with a build script, but that's just a last line of defense - I currently don't see any reason (except from the technical implementation problem) to not have it in the compiler - maybe a potential security concern if the strings are built at compile time from a const fn
, but then again include_str!("/usr/bin/passwd")
can already be done today, so it doesn't decrease or increase security.