Code Monkey home page Code Monkey logo

Comments (7)

kyp44 avatar kyp44 commented on June 27, 2024 2

Yeah the interpolate-folder-path feature totally solved this for me. Maybe I should have completely read the docs before submitting and issue here!

from rust-embed.

kyp44 avatar kyp44 commented on June 27, 2024 1

Yeah, I definitely see that now, I've written some proc macros myself and had not taken into account how they actually work. In particular, I imagine that a const value could be used in the debug case where the directory is searched at runtime since presumably the macro produces code in the host program that does this and thus would have access to a const identifier. However, in the release case I see that this would not be possible since the directory is read at compile time instead.

I did a quick test and verified that you are correct in that neither include_bytes nor include_str work with const identifiers, and explicitly say that they require a string literal.

I tried another approach described in a reddit post I just made here, but it also didn’t work. I’ll also note that the env macro approach does work for include_bytes and include_str. If you’re as stumped as I am, we’ll see if anyone in my reddit post has any ideas for workarounds.

from rust-embed.

AzureMarker avatar AzureMarker commented on June 27, 2024

The value needs to be used during the proc macro execution, which doesn't have access to the rest of your program. Proc macros receive the input as tokens, so we can't resolve the value of a variable, even if it's const.

Unfortunately this means this feature is impossible.

from rust-embed.

AzureMarker avatar AzureMarker commented on June 27, 2024

Thinking a little more on this, it might work if include_bytes supports it, but I don't think it does.

from rust-embed.

AzureMarker avatar AzureMarker commented on June 27, 2024

You can actually include whatever you want in attributes, it's just that right now the library expects a string. Whatever you write there will still just be unevaluated tokens to the macro though. The proc macro does read some metadata about the file (ex. hash, last modified time), so it does want the real path during its execution, but besides that it would technically be possible to accept an expression that resolves at compile time (ex. env macro) to a string that include_bytes would accept.

from rust-embed.

kyp44 avatar kyp44 commented on June 27, 2024

Yeah, based on my experience, I assumed that the proc macro itself is specifically looking for a string literal. I'm guessing that the syntax tree for the attribute could just be the macro expression, which does exist in syn for example. However, unless there's a a way inside the proc macro to actually evaluate the env macro I don't think it's possible to do this. It sounds like maybe this is just not possible.

Although, given that include_bytes does the same kind of things at compile time, it must be possible unless it's using some special black magic. I might look into how this is accomplished.

EDIT: Ok I quickly looked into this and include_bytes is a macro_rules! macro that basically does nothing with a comment that this is "compiler built-in", so evidently it is actually a compiler feature. This is exactly what I meant when I referred to special black magic.

from rust-embed.

AzureMarker avatar AzureMarker commented on June 27, 2024

If you use the interpolate-folder-path feature you can use environment variables in the folder string, but that doesn't help much since now you'd need to set an env variable in addition to the const.

from rust-embed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.