sphere_dance icon indicating copy to clipboard operation
sphere_dance copied to clipboard

include_str

Open MaulingMonkey opened this issue 5 years ago • 4 comments

Saw your blog post on HN. Cool stuff! Just wanted to point out you could replace this:

https://github.com/janiorca/sphere_dance/blob/b9c10f31701a540230a028a8499a7fd3c432df8d/src/shaders.rs#L1-L5

With something like:

pub static frag_shader_src : &'static str = include_str("../shader_code.h");

include_str! evauates at build time, and is part of core, so it should just work (tm). Just wanted to point this out before you write a PR for shader_minifier 👍. There's also include_bytes! if you want a u8 array instead, or include! if you want rust code.

MaulingMonkey avatar Jul 06 '20 01:07 MaulingMonkey

~~From my testing include_str! requires you to have the final shader file alongside the executable so that is not a good option for standalone executables.~~

Above is correct, I think I must have had a different issue in my program at the time.

TimvanScherpenzeel avatar Sep 14 '20 20:09 TimvanScherpenzeel

I suspect you've confused include_str! with std::fs::read_to_string. include_str does not require you keep the file:

C:\local\example>echo fn main() { println!("{}", include_str!("test.txt")); } > main.rs
C:\local\example>echo Hello, world! > test.txt
C:\local\example>rustc main.rs -o main.exe
C:\local\example>del test.txt
C:\local\example>type test.txt
The system cannot find the file specified.
C:\local\example>type main.rs
fn main() { println!("{}", include_str!("test.txt")); } 
C:\local\example>main.exe
Hello, world! 

MaulingMonkey avatar Sep 14 '20 21:09 MaulingMonkey

You can also see the resulting string inlined into your executable: image ("Hello, world! \r\n")

MaulingMonkey avatar Sep 14 '20 21:09 MaulingMonkey

You are correct, updated my comment above to avoid confusion.

TimvanScherpenzeel avatar Sep 15 '20 07:09 TimvanScherpenzeel