Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile time file path check which never fails? #67

Open
pickfire opened this issue Nov 12, 2021 · 4 comments
Open

Compile time file path check which never fails? #67

pickfire opened this issue Nov 12, 2021 · 4 comments

Comments

@pickfire
Copy link

pickfire commented Nov 12, 2021

And the query could be zero cost?

@Michael-F-Bryan
Copy link
Owner

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
Copy link
Author

pickfire commented Nov 12, 2021

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
Copy link
Owner

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
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants