Michael-F-Bryan / include_dir

The logical evolution of the include_str macro for embedding a directory tree into your binary.
https://michael-f-bryan.github.io/include_dir
MIT License
319 stars 36 forks source link

Compile time file path check which never fails? #67

Open pickfire opened 2 years ago

pickfire commented 2 years ago

And the query could be zero cost?

Michael-F-Bryan commented 2 years ago

Can you elaborate on what you are asking for? Maybe also provide some examples and why you believe include_dir would be a good place for this feature.

pickfire commented 2 years ago

For example,

use include_dir::{include_dir, Dir};

static PROJECT_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR");

// of course, you can retrieve a file by its full path
let lib_rs = PROJECT_DIR.get_file("src/lib.rs"); // no unwrap needed, cost removed if possible as well maybe with `const`?

Since we should already know if the file exists at compile time it would be better if we get compile time error that file does not exists rather than runtime error. Means that it is checked during compile time, also some costs for searching the file using string should be removed.

Michael-F-Bryan commented 2 years ago

The only way to guarantee this will succeed at compile time is if get_file() (and every function it calls) is made a const fn and you use const LIB_RS: &File<'_> = ... instead of let.

This sort of compile-time validation means we can't have things like compression because they will generally require allocations. There's also no point decompressing something at compile time because that'll just store the decompressed data in your variable.

Also, how would you deal with things like a web server which is serving static assets that were compiled into the binary? Your get_file() function doesn't return a Result/Option, so the server would need to panic at runtime if an unknown asset was requested.

pickfire commented 2 years ago

This sort of compile-time validation means we can't have things like compression because they will generally require allocations. There's also no point decompressing something at compile time because that'll just store the decompressed data in your variable.

But what if you store a token to the compressed data? But the data is still decompressed in runtime? But yeah, I don't know if it is possible to do this or not.

Also, how would you deal with things like a web server which is serving static assets that were compiled into the binary? Your get_file() function doesn't return a Result/Option, so the server would need to panic at runtime if an unknown asset was requested.

That was what I thought about which is similar to actix-web extractor. Just because some of the types is not available at compile time, everything is checked in runtime. What if there is a separation between two of them, one for those that want to check during compile time and one that can be used to dynamically checked?